Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/textual/widgets/_masked_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def prev_separator_position(self, position: int | None = None) -> int | None:
"""
if position is None:
position = self.input.cursor_position
for index in range(position - 1, 0, -1):
for index in range(position - 1, -1, -1):
if _CharFlags.SEPARATOR in self.template[index].flags:
return index
else:
Expand Down Expand Up @@ -636,9 +636,9 @@ def action_cursor_left_word(self) -> None:
position = self._template.prev_separator_position(self.cursor_position - 1)
else:
position = self._template.prev_separator_position()
if position:
if position is not None:
position += 1
self.cursor_position = position or 0
self.cursor_position = position if position is not None else 0

def action_cursor_right_word(self) -> None:
"""Move the cursor right next to the next separator. If no next
Expand Down Expand Up @@ -683,7 +683,7 @@ def action_delete_left_word(self) -> None:
position = self._template.prev_separator_position(self.cursor_position - 1)
else:
position = self._template.prev_separator_position()
if position:
if position is not None:
position += 1
else:
position = 0
Expand Down
13 changes: 13 additions & 0 deletions tests/test_masked_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ async def test_digits_required():
await pilot.press("a", "1")
assert input.value == "1"
assert not input.is_valid


async def test_cursor_left_word_leading_separator():
"""Ctrl+Left must not ignore a separator at index 0."""
app = InputApp("-NNN-NNN")
async with app.run_test():
inp = app.query_one(MaskedInput)
inp.value = "-ABC-DEF"
inp.cursor_position = 8
inp.action_cursor_left_word()
assert inp.cursor_position == 5
inp.action_cursor_left_word()
assert inp.cursor_position == 1