diff options
| author | Luuk van Baal <luukvbaal@gmail.com> | 2025-04-22 21:35:10 +0200 |
|---|---|---|
| committer | luukvbaal <luukvbaal@gmail.com> | 2025-04-23 10:58:22 +0200 |
| commit | 019b2050e13583f8a26dc5dc1c7e20ca07bb51e5 (patch) | |
| tree | 6011c3f5d7ef7e2d4ee5657d9aaa42ac197a6741 /runtime/lua/vim | |
| parent | 8d68dbf906f9559dfd6e8cb1dbc1bb26fffca74d (diff) | |
| download | rneovim-019b2050e13583f8a26dc5dc1c7e20ca07bb51e5.tar.gz rneovim-019b2050e13583f8a26dc5dc1c7e20ca07bb51e5.tar.bz2 rneovim-019b2050e13583f8a26dc5dc1c7e20ca07bb51e5.zip | |
fix(snippet): use <cmd>call cursor() for visual range
Problem: Change applied in d3e495ce uses a byte-offset where a virtual
column is expected.
Solution: Set the cursor directly through a <Cmd> mapping, while making
sure the commands are ordered correctly by adding them to the
type-ahead buffer.
Diffstat (limited to 'runtime/lua/vim')
| -rw-r--r-- | runtime/lua/vim/snippet.lua | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/runtime/lua/vim/snippet.lua b/runtime/lua/vim/snippet.lua index e84a9e376a..0d7dd6a034 100644 --- a/runtime/lua/vim/snippet.lua +++ b/runtime/lua/vim/snippet.lua @@ -278,15 +278,6 @@ local function select_tabstop(tabstop) vim.api.nvim_feedkeys(keys, 'n', true) end - --- NOTE: We don't use `vim.api.nvim_win_set_cursor` here because it causes the cursor to end - --- at the end of the selection instead of the start. - --- - --- @param row integer - --- @param col integer - local function move_cursor_to(row, col) - feedkeys(string.format('%sG%s|', row, col)) - end - local range = tabstop:get_range() local mode = vim.fn.mode() @@ -311,13 +302,16 @@ local function select_tabstop(tabstop) end else -- Else, select the tabstop's text. - if mode ~= 'n' then - feedkeys('<Esc>') - end - move_cursor_to(range[1] + 1, range[2] + 1) - feedkeys('v') - move_cursor_to(range[3] + 1, range[4]) - feedkeys('o<c-g><c-r>_') + -- Need this exact order so cannot mix regular API calls with feedkeys, which + -- are not executed immediately. Use <Cmd> to set the cursor position. + local keys = { + mode ~= 'n' and '<Esc>' or '', + ('<Cmd>call cursor(%s,%s)<CR>'):format(range[1] + 1, range[2] + 1), + 'v', + ('<Cmd>call cursor(%s,%s)<CR>'):format(range[3] + 1, range[4]), + 'o<c-g><c-r>_', + } + feedkeys(table.concat(keys)) end end |