fix: honor a separator at index 0 in MaskedInput word navigation - #6646
Open
mvanhorn wants to merge 1 commit into
Open
fix: honor a separator at index 0 in MaskedInput word navigation#6646mvanhorn wants to merge 1 commit into
mvanhorn wants to merge 1 commit into
Conversation
_Template.prev_separator_position() used range(position - 1, 0, -1), whose exclusive stop of 0 never visited index 0, and action_cursor_left_word / action_delete_left_word then treated a returned index of 0 as falsy (if position: / position or 0). Together these broke ctrl+left and ctrl+backspace on templates that start with a separator (e.g. "-NNN-NNN"): the cursor landed on the separator instead of just after it. Scan the full prefix and mirror the is-None handling that action_cursor_right_word already uses, so an index-0 separator is honored. Closes Textualize#6641
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On a
MaskedInputwhose template starts with a separator (e.g."-NNN-NNN"),ctrl+left(action_cursor_left_word) andctrl+backspace(action_delete_left_word) land the cursor on the leading separator instead of just after it.Closes #6641.
Why this matters
Word-wise navigation and deletion are core editing affordances, and they silently misbehave for any mask that opens with a separator (phone numbers, dates written
-NNN…, etc.). The cursor stops on a read-only separator cell, so the next keystroke behaves unexpectedly — a small but confusing correctness bug in a widely-used widget.Root cause
Two compounding issues, both around the index-0 separator:
_Template.prev_separator_position()scanned withrange(position - 1, 0, -1). The stop value0is exclusive, so index 0 was never checked — a leading separator was never found.action_cursor_left_word(if position:/self.cursor_position = position or 0) andaction_delete_left_word(if position:) treat a returned index of0as falsy, skipping the+ 1that moves the cursor past the separator.So the reporter's suggested one-line change to (1) alone is not enough; both spots have to stop treating a valid index of
0as "no separator".Fix
prev_separator_position(range(position - 1, -1, -1)).is Nonehandling thataction_cursor_right_wordalready uses, so an index-0 separator is honored.Testing
Added
test_left_word_actions_with_leading_separator, which reproduces the issue's scenario and also coversaction_delete_left_word. It fails onmainand passes with this change; the rest oftests/test_masked_input.pycontinues to pass (14 → 15). Verified against the issue's exact reproducer (ctrl+leftnow yields5then1).AI was used for assistance.