diff options
author | Folke Lemaitre <folke.lemaitre@gmail.com> | 2023-06-05 01:45:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-04 16:45:01 -0700 |
commit | 5282d3299c9b1b07f3e02a9014bc2632cf3b4fed (patch) | |
tree | 88a2894d6fa3c7432373e789d8a80d069ced0e0f /runtime/lua/vim/lsp/util.lua | |
parent | 67827edeef5ce3718c40c83ccca07dd1854a0f16 (diff) | |
download | rneovim-5282d3299c9b1b07f3e02a9014bc2632cf3b4fed.tar.gz rneovim-5282d3299c9b1b07f3e02a9014bc2632cf3b4fed.tar.bz2 rneovim-5282d3299c9b1b07f3e02a9014bc2632cf3b4fed.zip |
fix(lsp): restore marks after apply_text_edits() #14630
PROBLEM:
Whenever any text edits are applied to the buffer, the `marks` part of those
lines will be lost. This is mostly problematic for code formatters that format
the whole buffer like `prettier`, `luafmt`, ...
When doing atomic changes inside a vim doc, vim keeps track of those changes and
can update the positions of marks accordingly, but in this case we have a whole
doc that changed. There's no simple way to update the positions of all marks
from the previous document state to the new document state.
SOLUTION:
* save marks right before `nvim_buf_set_lines` is called inside `apply_text_edits`
* check if any marks were lost after doing `nvim_buf_set_lines`
* restore those marks to the previous positions
TEST CASE:
* have a formatter enabled
* open any file
* create a couple of marks
* indent the whole file to the right
* save the file
Before this change: all marks will be removed.
After this change: they will be preserved.
Fixes #14307
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 53f8dba814..ba8c72128e 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -451,6 +451,14 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) } end)() + -- save and restore local marks since they get deleted by nvim_buf_set_lines + local marks = {} + for _, m in pairs(vim.fn.getmarklist(bufnr or vim.api.nvim_get_current_buf())) do + if m.mark:match("^'[a-z]$") then + marks[m.mark:sub(2, 2)] = { m.pos[2], m.pos[3] - 1 } -- api-indexed + end + end + -- Apply text edits. local is_cursor_fixed = false local has_eol_text_edit = false @@ -518,6 +526,20 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) local max = api.nvim_buf_line_count(bufnr) + -- no need to restore marks that still exist + for _, m in pairs(vim.fn.getmarklist(bufnr or vim.api.nvim_get_current_buf())) do + marks[m.mark:sub(2, 2)] = nil + end + -- restore marks + for mark, pos in pairs(marks) do + if pos then + -- make sure we don't go out of bounds + pos[1] = math.min(pos[1], max) + pos[2] = math.min(pos[2], #(get_line(bufnr, pos[1] - 1) or '')) + vim.api.nvim_buf_set_mark(bufnr or 0, mark, pos[1], pos[2], {}) + end + end + -- Apply fixed cursor position. if is_cursor_fixed then local is_valid_cursor = true |