fix: preserve queued keys at picker launch (#2274) (#2625)

Ensure that any keystrokes that are queued at picker launch are
processed only after the picker's mode (`insert` or `normal`) has been
chosen, preserving their intended meaning.

Previously the picker's mode was set by simulating keystrokes via
`nvim_feedkeys(simulated_keypresses, "n")`. In the absence of queued
keystrokes, this works fine; but if the user is able to queue keystrokes
before the call to `nvim_feedkeys()`, those queued keystrokes are
processed before the simulated keystrokes that change the picker's mode.
Because of this unexpected ordering, the user's queued keystrokes may
appear to be ignored or may cause the picker to start in the wrong mode.

For example, consider the below normal-mode mapping:
```vim
:nnoremap <space>ff :Telescope find_files<CR>
```

Upon launching the picker via `<space>ff`, Neovim is already in normal
mode. To switch to insert mode in the picker, Telescope previously used
a call to `nvim_feedkeys("A", "n")`, simulating a keypress of `A` to
enter insert mode at the end of the current line.  This `A` would not be
processed until all previously queued user keystrokes have been
processed, causing issues.

In real-world use, problems occur when the user types `<space>ff`
followed quickly by characters intended as fuzzy match text.  This can
be demonstrated using `nvim_feedkeys()` as shown below.

```vim
:call nvim_feedkeys("\<space>ff" . "apple")
```

The user intended to search for `apple`, but the `a` is misinterpreted
as a request to enter insert mode at end of line, after which `pple` is
inserted; subsequently, Telescope's simulated `A` is then appended,
resulting in a search string of `ppleA`.

To ensure that Telescope's simulated keypresses are processed first, an
additional `i` flag is now passed to `nvim_feedkeys()`, causing the
simulated keypresses to be inserted at the start of the typeahead buffer
ahead of any user keystrokes.

Fixes #2274.
This commit is contained in:
Michael Henry
2023-07-29 16:48:13 -04:00
committed by GitHub
parent 22735947d8
commit b6fccfb0f7

View File

@@ -439,7 +439,7 @@ function Picker:find()
-- always fully retrigger insert mode: required for going from one picker to next -- always fully retrigger insert mode: required for going from one picker to next
keys = mode ~= "n" and "<ESC>A" or "A" keys = mode ~= "n" and "<ESC>A" or "A"
end end
a.nvim_feedkeys(a.nvim_replace_termcodes(keys, true, false, true), "n", true) a.nvim_feedkeys(a.nvim_replace_termcodes(keys, true, false, true), "ni", true)
else else
utils.notify( utils.notify(
"pickers.find", "pickers.find",