diff --git a/runtime/lua/vim/lsp/inline_completion.lua b/runtime/lua/vim/lsp/inline_completion.lua index d016f678ea..bd7b771d52 100644 --- a/runtime/lua/vim/lsp/inline_completion.lua +++ b/runtime/lua/vim/lsp/inline_completion.lua @@ -342,6 +342,10 @@ function Completor:accept(item) if type(insert_text) == 'string' then if item.range then local start_row, start_col, end_row, end_col = item.range:to_extmark() + -- The line may have shrunk past the range since the item arrived. + local end_line = api.nvim_buf_get_lines(self.bufnr, end_row, end_row + 1, true)[1] + end_col = math.min(end_col, #end_line) + local lines = vim.split(insert_text, '\n') api.nvim_buf_set_text(self.bufnr, start_row, start_col, end_row, end_col, lines) local win = api.nvim_get_current_win() diff --git a/test/functional/plugin/lsp/inline_completion_spec.lua b/test/functional/plugin/lsp/inline_completion_spec.lua index 529f0b4126..5383480437 100644 --- a/test/functional/plugin/lsp/inline_completion_spec.lua +++ b/test/functional/plugin/lsp/inline_completion_spec.lua @@ -280,6 +280,19 @@ describe('vim.lsp.inline_completion', function() }, }, result) end) + + it('accepts an item after the line shrank past its range', function() + feed('i') + screen:expect({ grid = grid_with_candidates }) + exec_lua(function() + -- Shrink the line the item's range covers, as backspacing would. + vim.api.nvim_buf_set_text(0, 0, 18, 0, 20, {}) + vim.lsp.inline_completion.get() + end) + n.poke_eventloop() + feed('') + screen:expect({ grid = grid_applied_candidates }) + end) end) describe('select()', function()