Skip to content

Accept auto-suggestions with 'l' in Vi navigation mode - #2099

Open
hikmetba-bit wants to merge 1 commit into
prompt-toolkit:mainfrom
hikmetba-bit:feature/vi-l-accepts-suggestion-2075
Open

hikmetba-bit wants to merge 1 commit into
prompt-toolkit:mainfrom
hikmetba-bit:feature/vi-l-accepts-suggestion-2075

Conversation

@hikmetba-bit

Copy link
Copy Markdown

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 for l in Vi navigation mode, matching what Fish itself does (as requested in the issue, referencing pgcli's use of this library).

Why this needed more than copying the right binding

l in insert mode must keep inserting a literal l character, so the new binding is scoped to vi_navigation_mode — unlike right/c-f/c-e, which aren't printable characters and so are safe to bind unconditionally.

More subtly, the existing suggestion_available condition checks Document.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 to len(text) first, which is a no-op for the pre-existing emacs/insert-mode case where it's already there.

Test plan

  • Added test_vi_accept_suggestion_with_l in tests/test_cli.py. It can't reuse the existing _feed_cli_with_input helper: 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-existing right-arrow acceptance path, which I confirmed independently still works once given that same timing.
  • The test covers both: l accepting the suggestion when the cursor is on the last character, and l keeping its normal cursor-move meaning when away from it (via 0l).
  • Ran 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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Complete suggestions using "l" in Vi mode

1 participant