From 891bb0e6ce513dc885cebe995918e5db4e69174a Mon Sep 17 00:00:00 2001 From: Lars Debor <23101388+1DIce@users.noreply.github.com> Date: Sun, 12 Apr 2026 17:46:24 +0200 Subject: [PATCH] fix(lsp): show_document can't position cursor past EOL in insert-mode #38566 Problem: vim.lsp.util.show_document insert mode is unable to set the cursor after the target character position if the target character is at end of line. Solution: Move cursor after the target character (in append position) in this case. --- runtime/lua/vim/lsp/util.lua | 13 +++++++++++++ test/functional/plugin/lsp_spec.lua | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 2c174c0dd4..71bc86d8c2 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -1044,6 +1044,19 @@ function M.show_document(location, position_encoding, opts) -- Open folds under the cursor vim.cmd('normal! zv') end) + + -- nvim_win_set_cursor clamps to last char at EOL. In insert mode the cursor + -- should be past the last char (append position). + if vim.api.nvim_get_mode().mode == 'i' then + local line = api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or '' + if col >= #line then + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes('', true, false, true), + 'n', + false + ) + end + end end return true diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index e1a6f385de..d85273e199 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -3574,6 +3574,13 @@ describe('LSP', function() local pos = show_document(location(0, 9, 0, 9), true, true) eq(1, pos.line) eq(10, pos.col) + + -- expectation: Cursor is placed past EOL (append position) in insert mode + n.feed('I') + pos = show_document(location(0, 16, 0, 16), true, true) + eq(1, pos.line) + eq(17, pos.col) + eq('i', api.nvim_get_mode().mode) end) it('jumps to a Location if focus is true via handler', function()