diff options
author | bfredl <bjorn.linse@gmail.com> | 2023-09-11 10:45:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-11 10:45:45 +0200 |
commit | 616a9bac32d37c2141f2cba9a0717ba67a041f80 (patch) | |
tree | 2e3f45d19925dcc90c49fdd00f4721a909cb7d85 /runtime | |
parent | 60e5d0fbcc8dfb942480aa2d9e4b45f4a255d952 (diff) | |
parent | d22172f36bbe147f3aa6b76a1c43ae445f481c2e (diff) | |
download | rneovim-616a9bac32d37c2141f2cba9a0717ba67a041f80.tar.gz rneovim-616a9bac32d37c2141f2cba9a0717ba67a041f80.tar.bz2 rneovim-616a9bac32d37c2141f2cba9a0717ba67a041f80.zip |
Merge pull request #24901 from faergeek/more-intuitive-cursor-update
fix(api): more intuitive cursor updates in nvim_buf_set_text
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/doc/api.txt | 3 | ||||
-rw-r--r-- | runtime/lua/vim/_meta/api.lua | 1 | ||||
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 42 |
3 files changed, 4 insertions, 42 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index 7dd760b6a5..c0bea52513 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -2438,6 +2438,8 @@ nvim_buf_set_text({buffer}, {start_row}, {start_col}, {end_row}, {end_col}, Prefer |nvim_buf_set_lines()| if you are only adding or deleting entire lines. + Prefer |nvim_put()| if you want to insert text at the cursor position. + Attributes: ~ not allowed when |textlock| is active @@ -2451,6 +2453,7 @@ nvim_buf_set_text({buffer}, {start_row}, {start_col}, {end_row}, {end_col}, See also: ~ • |nvim_buf_set_lines()| + • |nvim_put()| nvim_buf_set_var({buffer}, {name}, {value}) *nvim_buf_set_var()* Sets a buffer-scoped (b:) variable diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index c46b604b90..6c4e6c04d9 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -621,6 +621,7 @@ function vim.api.nvim_buf_set_option(buffer, name, value) end --- range, use `replacement = {}`. --- Prefer `nvim_buf_set_lines()` if you are only adding or deleting entire --- lines. +--- Prefer `nvim_put()` if you want to insert text at the cursor position. --- --- @param buffer integer Buffer handle, or 0 for current buffer --- @param start_row integer First line index diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index e76fd15612..54721865b7 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -454,23 +454,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) end end) - -- Some LSP servers are depending on the VSCode behavior. - -- The VSCode will re-locate the cursor position after applying TextEdit so we also do it. - local is_current_buf = api.nvim_get_current_buf() == bufnr or bufnr == 0 - local cursor = (function() - if not is_current_buf then - return { - row = -1, - col = -1, - } - end - local cursor = api.nvim_win_get_cursor(0) - return { - row = cursor[1] - 1, - col = cursor[2], - } - 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 @@ -480,7 +463,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) end -- Apply text edits. - local is_cursor_fixed = false local has_eol_text_edit = false for _, text_edit in ipairs(text_edits) do -- Normalize line ending @@ -527,20 +509,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) e.end_col = math.min(last_line_len, e.end_col) api.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text) - - -- Fix cursor position. - local row_count = (e.end_row - e.start_row) + 1 - if e.end_row < cursor.row then - cursor.row = cursor.row + (#e.text - row_count) - is_cursor_fixed = true - elseif e.end_row == cursor.row and e.end_col <= cursor.col then - cursor.row = cursor.row + (#e.text - row_count) - cursor.col = #e.text[#e.text] + (cursor.col - e.end_col) - if #e.text == 1 then - cursor.col = cursor.col + e.start_col - end - is_cursor_fixed = true - end end end @@ -560,16 +528,6 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding) end end - -- Apply fixed cursor position. - if is_cursor_fixed then - local is_valid_cursor = true - is_valid_cursor = is_valid_cursor and cursor.row < max - is_valid_cursor = is_valid_cursor and cursor.col <= #(get_line(bufnr, cursor.row) or '') - if is_valid_cursor then - api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col }) - end - end - -- Remove final line if needed local fix_eol = has_eol_text_edit fix_eol = fix_eol and (vim.bo[bufnr].eol or (vim.bo[bufnr].fixeol and not vim.bo[bufnr].binary)) |