Skip to content

Commit

Permalink
fix(lsp): fix cursor row after textEdits (neovim#16038)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th authored Oct 18, 2021
1 parent bcc9ba5 commit bd2f61c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 6 additions & 4 deletions runtime/lua/vim/lsp/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,12 @@ function M.apply_text_edits(text_edits, bufnr)
end

if is_cursor_fixed then
vim.api.nvim_win_set_cursor(0, {
cursor.row + 1,
math.min(cursor.col, #(vim.api.nvim_buf_get_lines(bufnr, cursor.row, cursor.row + 1, false)[1] or ''))
})
local is_valid_cursor = true
is_valid_cursor = is_valid_cursor and cursor.row < vim.api.nvim_buf_line_count(bufnr)
is_valid_cursor = is_valid_cursor and cursor.col <= #(vim.api.nvim_buf_get_lines(bufnr, cursor.row, cursor.row + 1, false)[1] or '')
if is_valid_cursor then
vim.api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col })
end
end

-- Remove final line if needed
Expand Down
18 changes: 16 additions & 2 deletions test/functional/plugin/lsp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1164,10 +1164,11 @@ describe('LSP', function()
eq({ 2, 6 }, funcs.nvim_win_get_cursor(0))
end)

it('fix the cursor to the valid column if the content was removed', function()
it('fix the cursor to the valid col if the content was removed', function()
funcs.nvim_win_set_cursor(0, { 2, 6 })
local edits = {
make_edit(1, 0, 1, 19, '')
make_edit(1, 0, 1, 6, ''),
make_edit(1, 6, 1, 19, '')
}
exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1)
eq({
Expand All @@ -1180,6 +1181,19 @@ describe('LSP', function()
eq({ 2, 0 }, funcs.nvim_win_get_cursor(0))
end)

it('fix the cursor to the valid row if the content was removed', function()
funcs.nvim_win_set_cursor(0, { 2, 6 })
local edits = {
make_edit(1, 0, 1, 6, ''),
make_edit(0, 18, 5, 0, '')
}
exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1)
eq({
'First line of text';
}, buf_lines(1))
eq({ 1, 6 }, funcs.nvim_win_get_cursor(0))
end)

it('fix the cursor row', function()
funcs.nvim_win_set_cursor(0, { 3, 0 })
local edits = {
Expand Down

0 comments on commit bd2f61c

Please sign in to comment.