Skip to content

Commit 3a80f3b

Browse files
authored
implement smart backspace
copied impl from single key handler (should key handling funcs be merged at some point?)
1 parent d57f2f3 commit 3a80f3b

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

spyder/plugins/editor/widgets/codeeditor/codeeditor.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,6 @@ def handle_multi_cursor_keypress(self, event: QKeyEvent):
624624
ctrl = event.modifiers() & Qt.KeyboardModifier.ControlModifier
625625
alt = event.modifiers() & Qt.KeyboardModifier.AltModifier
626626
shift = event.modifiers() & Qt.KeyboardModifier.ShiftModifier
627-
# TODO handle other keys?
628-
# TODO handle sig_key_pressed for each cursor?
629-
# (maybe not: extra extensions add complexity.
630-
# Keep multi-cursor simpler)
631627
# ---- handle insert
632628
if key == Qt.Key.Key_Insert and not (ctrl or alt or shift):
633629
self.overwrite_mode = not self.overwrite_mode
@@ -705,7 +701,41 @@ def handle_multi_cursor_keypress(self, event: QKeyEvent):
705701
else:
706702
self.fix_indent(comment_or_string=cmt_or_str,
707703
cur_indent=cur_indent)
708-
704+
# ---- intelligent backspace handling
705+
elif key == Qt.Key_Backspace and not shift and not ctrl:
706+
increasing_position = False
707+
if self.has_selected_text() or not self.intelligent_backspace:
708+
self._handle_keypress_event(event)
709+
else:
710+
leading_text = self.get_text('sol', 'cursor')
711+
leading_length = len(leading_text)
712+
trailing_spaces = (
713+
leading_length - len(leading_text.rstrip())
714+
)
715+
trailing_text = self.get_text('cursor', 'eol')
716+
matches = ('()', '[]', '{}', '\'\'', '""')
717+
if (
718+
not leading_text.strip() and
719+
(leading_length > len(self.indent_chars))
720+
):
721+
if leading_length % len(self.indent_chars) == 0:
722+
self.unindent()
723+
else:
724+
self._handle_keypress_event(event)
725+
elif trailing_spaces and not trailing_text.strip():
726+
self.remove_suffix(leading_text[-trailing_spaces:])
727+
elif (
728+
leading_text and
729+
trailing_text and
730+
(leading_text[-1] + trailing_text[0] in matches)
731+
):
732+
cursor = self.textCursor()
733+
cursor.movePosition(QTextCursor.PreviousCharacter)
734+
cursor.movePosition(QTextCursor.NextCharacter,
735+
QTextCursor.KeepAnchor, 2)
736+
cursor.removeSelectedText()
737+
else:
738+
self._handle_keypress_event(event)
709739
# ---- use default handler for cursor (text)
710740
else:
711741
if key in (Qt.Key.Key_Up, Qt.Key.Key_Left, Qt.Key.Key_Home):
@@ -714,7 +744,8 @@ def handle_multi_cursor_keypress(self, event: QKeyEvent):
714744
cursor.verticalMovementX() == -1):
715745
# Builtin handler somehow does not set verticalMovementX
716746
# when moving up and down (but works fine for single
717-
# cursor somehow) # TODO why
747+
# cursor somehow)
748+
# TODO why? Am I forgetting something?
718749
x = self.cursorRect(cursor).x()
719750
cursor.setVerticalMovementX(x)
720751
self.setTextCursor(cursor)

0 commit comments

Comments
 (0)