Accept auto-suggestions with 'l' in Vi navigation mode - #2099
Open
hikmetba-bit wants to merge 1 commit into
Open
hikmetba-bit wants to merge 1 commit into
hikmetba-bit wants to merge 1 commit into
Conversation
Fixes prompt-toolkit#2075. The right arrow key (and c-f/c-e) already accept an available fish-style suggestion when the cursor is at the end of the buffer. This adds the same behavior for 'l' in Vi navigation mode, matching what Fish itself does. Vi navigation mode needed its own "at the end" condition: `Document.is_cursor_at_the_end` requires cursor_position == len(text), but navigation mode never lets the cursor move past the last character, so that condition can never be true there for a non-empty buffer. The equivalent state in navigation mode is the cursor sitting on the last character. That also means the accept handler can no longer assume the cursor is already at the true end before inserting: in navigation mode it's one position short (on the last character, not after it), so inserting there would splice the suggestion in before that character instead of appending it. The handler now moves the cursor to the end first (a no-op for the pre-existing emacs/insert-mode case, where it's already there). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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
Fixes #2075.
The right arrow key (and
c-f/c-e) already accept an available fish-style auto-suggestion when the cursor is at the end of the buffer. This adds the same behavior forlin Vi navigation mode, matching what Fish itself does (as requested in the issue, referencingpgcli's use of this library).Why this needed more than copying the
rightbindinglin insert mode must keep inserting a literallcharacter, so the new binding is scoped tovi_navigation_mode— unlikeright/c-f/c-e, which aren't printable characters and so are safe to bind unconditionally.More subtly, the existing
suggestion_availablecondition checksDocument.is_cursor_at_the_end(cursor_position == len(text)), but Vi navigation mode never lets the cursor move past the last character — so that condition can never be true there for a non-empty buffer. I added a navigation-mode-specific condition where "at the end" means sitting on the last character instead.That, in turn, means the accept handler could no longer assume the cursor was already at the true end before calling
insert_text: in navigation mode it's one position short (on the last character, not after it), so inserting there spliced the suggestion in before that character instead of appending it (verified this with a test before fixing it — typing "hello" with a "hello world" suggestion produced "hell worldo"). The handler now moves the cursor tolen(text)first, which is a no-op for the pre-existing emacs/insert-mode case where it's already there.Test plan
test_vi_accept_suggestion_with_lintests/test_cli.py. It can't reuse the existing_feed_cli_with_inputhelper: suggestions are computed by a background asyncio task scheduled on text changes, so the accept key has to be sent as a separate pipe write, after yielding to the event loop, or the suggestion is never computed in time — this also affects the pre-existingright-arrow acceptance path, which I confirmed independently still works once given that same timing.laccepting the suggestion when the cursor is on the last character, andlkeeping its normal cursor-move meaning when away from it (via0l).pytest tests/test_cli.py tests/test_key_binding.py: 39 passed, 2 pre-existing failures (test_emacs_history_bindings,test_emacs_reverse_search) confirmed unrelated by reproducing them on a clean checkout before this change.🤖 Generated with Claude Code