@@ -624,10 +624,6 @@ def handle_multi_cursor_keypress(self, event: QKeyEvent):
624
624
ctrl = event .modifiers () & Qt .KeyboardModifier .ControlModifier
625
625
alt = event .modifiers () & Qt .KeyboardModifier .AltModifier
626
626
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)
631
627
# ---- handle insert
632
628
if key == Qt .Key .Key_Insert and not (ctrl or alt or shift ):
633
629
self .overwrite_mode = not self .overwrite_mode
@@ -705,7 +701,41 @@ def handle_multi_cursor_keypress(self, event: QKeyEvent):
705
701
else :
706
702
self .fix_indent (comment_or_string = cmt_or_str ,
707
703
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 )
709
739
# ---- use default handler for cursor (text)
710
740
else :
711
741
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):
714
744
cursor .verticalMovementX () == - 1 ):
715
745
# Builtin handler somehow does not set verticalMovementX
716
746
# 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?
718
749
x = self .cursorRect (cursor ).x ()
719
750
cursor .setVerticalMovementX (x )
720
751
self .setTextCursor (cursor )
0 commit comments