Skip to content

Commit 8091a73

Browse files
committed
Refactor cursor movement logic in Input widget to handle selection state in manner similar to VSCode, browsers, etc.
If there's a selection and the cursor left/right keybinding is pressed (without shift held), the cursor will move to the corresponding "edge" of the selection.
1 parent 5cb6cd0 commit 8091a73

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/textual/widgets/_input.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,27 +761,33 @@ def action_cursor_left(self, select: bool = False) -> None:
761761
Args:
762762
select: If `True`, select the text to the left of the cursor.
763763
"""
764+
start, end = self.selection
764765
if select:
765-
start, end = self.selection
766766
self.selection = Selection(start, end - 1)
767767
else:
768-
self.cursor_position -= 1
768+
if self.selection.is_empty:
769+
self.cursor_position -= 1
770+
else:
771+
self.cursor_position = min(start, end)
769772

770773
def action_cursor_right(self, select: bool = False) -> None:
771774
"""Accept an auto-completion or move the cursor one position to the right.
772775
773776
Args:
774777
select: If `True`, select the text to the right of the cursor.
775778
"""
779+
start, end = self.selection
776780
if select:
777-
start, end = self.selection
778781
self.selection = Selection(start, end + 1)
779782
else:
780783
if self._cursor_at_end and self._suggestion:
781784
self.value = self._suggestion
782785
self.cursor_position = len(self.value)
783786
else:
784-
self.cursor_position += 1
787+
if self.selection.is_empty:
788+
self.cursor_position += 1
789+
else:
790+
self.cursor_position = max(start, end)
785791

786792
def action_home(self, select: bool = False) -> None:
787793
"""Move the cursor to the start of the input.

0 commit comments

Comments
 (0)