diff options
| author | geril07 <62308020+geril07@users.noreply.github.com> | 2026-04-23 19:01:44 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-04-23 16:01:44 +0000 |
| commit | 790a8be5f306a28ca8e96c2ae3fa3b465ae3718f (patch) | |
| tree | b1dfbfdcefbc5465def286bc1240134bc6b53e44 /runtime/lua/vim/lsp/util.lua | |
| parent | f8c94bb8cf470ff875ac6dc2e962cfb4b9cef0c2 (diff) | |
fix(lsp): malformed edit if apply_text_edits() is called twice #34954
Problem:
Use vim.lsp.util.apply_text_edits to re-apply the same textedit causes
an incorrect edit, because apply_text_edits silently modifies the
parameter.
Solution:
- Avoid changing `text_edit._index`.
- Document this fun feature.
Helped-by: Riley Bruins <ribru17@hotmail.com>
Helped-by: Yi Ming <ofseed@foxmail.com>
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
| -rw-r--r-- | runtime/lua/vim/lsp/util.lua | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 9231976c58..33d1b80371 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -291,7 +291,9 @@ local function get_line_byte_from_position(bufnr, position, position_encoding) return col end ---- Applies a list of text edits to a buffer. +--- Applies a list of text edits to a buffer. Note: this mutates `text_edits` (sorts in-place and +--- adds `_index` fields). +--- ---@param text_edits (lsp.TextEdit|lsp.AnnotatedTextEdit)[] ---@param bufnr integer Buffer id ---@param position_encoding 'utf-8'|'utf-16'|'utf-32' @@ -320,7 +322,10 @@ function M.apply_text_edits(text_edits, bufnr, position_encoding, change_annotat -- Fix reversed range and indexing each text_edits for index, text_edit in ipairs(text_edits) do --- @cast text_edit lsp.TextEdit|{_index: integer} - text_edit._index = index + -- XXX: Preserve existing _index to avoid surprises if the same edit is reapplied. #39344 + if text_edit._index == nil then + text_edit._index = index + end if text_edit.range.start.line > text_edit.range['end'].line |
