Skip to content

Commit 1e1900b

Browse files
authored
Preserve 'buflisted' state when applying LSP text edits (#2141)
1 parent c273707 commit 1e1900b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lua/cmp/core.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,11 @@ core.confirm = function(self, e, option, callback)
433433
return
434434
end
435435
vim.cmd([[silent! undojoin]])
436-
vim.lsp.util.apply_text_edits(text_edits, ctx.bufnr, e.source:get_position_encoding_kind())
436+
api.apply_text_edits(text_edits, ctx.bufnr, e.source:get_position_encoding_kind())
437437
end)
438438
else
439439
vim.cmd([[silent! undojoin]])
440-
vim.lsp.util.apply_text_edits(e.completion_item.additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
440+
api.apply_text_edits(e.completion_item.additionalTextEdits, ctx.bufnr, e.source:get_position_encoding_kind())
441441
end
442442
end)
443443
feedkeys.call('', 'n', function()
@@ -486,7 +486,7 @@ core.confirm = function(self, e, option, callback)
486486
if is_snippet then
487487
completion_item.textEdit.newText = ''
488488
end
489-
vim.lsp.util.apply_text_edits({ completion_item.textEdit }, ctx.bufnr, 'utf-8')
489+
api.apply_text_edits({ completion_item.textEdit }, ctx.bufnr, 'utf-8')
490490

491491
local texts = vim.split(completion_item.textEdit.newText, '\n')
492492
vim.api.nvim_win_set_cursor(0, {

lua/cmp/utils/api.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,15 @@ api.get_cursor_before_line = function()
6767
return string.sub(api.get_current_line(), 1, cursor[2])
6868
end
6969

70+
--- Applies a list of text edits to a buffer. Preserves 'buflisted' state.
71+
---@param text_edits lsp.TextEdit[]
72+
---@param bufnr integer Buffer id
73+
---@param position_encoding 'utf-8'|'utf-16'|'utf-32'
74+
api.apply_text_edits = function(text_edits, bufnr, position_encoding)
75+
-- preserve 'buflisted' state because vim.lsp.util.apply_text_edits forces it to true
76+
local prev_buflisted = vim.bo[bufnr].buflisted
77+
vim.lsp.util.apply_text_edits(text_edits, bufnr, position_encoding)
78+
vim.bo[bufnr].buflisted = prev_buflisted
79+
end
80+
7081
return api

0 commit comments

Comments
 (0)