Skip to content

Commit

Permalink
Add mouseDoubleClickEvent to BaseEditMixin for number selection
Browse files Browse the repository at this point in the history
Improve double click to select functionality from default to allow selecting floating point numbers, etc by parsing the line with the tokenize stdlib module and selecting entire NUMBER tokens.
  • Loading branch information
athompson673 authored Oct 25, 2024
1 parent 5f4c5cd commit 5f056c6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion spyder/widgets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
"""

# Standard library imports
from io import StringIO
import os
import os.path as osp
import re
import sre_constants
import sys
import textwrap
from token import NUMBER
from tokenize import generate_tokens

# Third party imports
from packaging.version import parse
from qtpy import QT_VERSION
from qtpy.QtCore import QPoint, QRegularExpression, Qt, QUrl
from qtpy.QtGui import (
QDesktopServices, QFontMetrics, QTextCursor, QTextDocument)
from qtpy.QtWidgets import QApplication
from qtpy.QtWidgets import QApplication, QPlainTextEdit, QTextEdit
from spyder_kernels.utils.dochelpers import (getargspecfromtext, getobj,
getsignaturefromtext)

Expand Down Expand Up @@ -1498,6 +1501,27 @@ def _enter_array(self, inline):
if self.sig_text_was_inserted is not None:
self.sig_text_was_inserted.emit()
cursor.endEditBlock()

# ---- Qt methods
def mouseDoubleClickEvent(self, event):
"""select NUMBER tokens to select numeric literals on double click"""
cur = self.cursorForPosition(event.pos())
block = cur.block()
text = block.text()
pos = block.position()
pos_in_block = cur.positionInBlock()
for t_type, _, start, end, _ in generate_tokens(StringIO(text).read):
if t_type == NUMBER and start[1] <= pos_in_block <= end[1]:
cur.setPosition(pos + start[1])
cur.setPosition(pos + end[1], QTextCursor.MoveMode.KeepAnchor)
self.setTextCursor(cur)
return
elif start[1] > pos_in_block:
break
if isinstance(self, QPlainTextEdit):
QPlainTextEdit.mouseDoubleClickEvent(self, event)
elif isinstance(self, QTextEdit):
QTextEdit.mouseDoubleClickEvent(self, event)


class TracebackLinksMixin(object):
Expand Down

0 comments on commit 5f056c6

Please sign in to comment.