Skip to content

Commit

Permalink
fix(entry/get_replace_range): handle TextEdit.range
Browse files Browse the repository at this point in the history
fixes cmp.mapping.confirm when ConfirmBehavior is set to Replace
  • Loading branch information
lvimuser committed Aug 20, 2022
1 parent b1ebdb0 commit 9766b16
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lua/cmp/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ entry.get_offset = function(self)
return self.cache:ensure({ 'get_offset', self.resolved_completion_item and 1 or 0 }, function()
local offset = self.source_offset
if misc.safe(self:get_completion_item().textEdit) then
local range = misc.safe(self:get_completion_item().textEdit.insert) or
misc.safe(self:get_completion_item().textEdit.range)
local range = misc.safe(self:get_completion_item().textEdit.insert) or misc.safe(self:get_completion_item().textEdit.range)
if range then
local c = misc.to_vimindex(self.context.cursor_line, range.start.character)
for idx = c, self.source_offset do
Expand Down Expand Up @@ -135,8 +134,7 @@ end
entry.get_overwrite = function(self)
return self.cache:ensure({ 'get_overwrite', self.resolved_completion_item and 1 or 0 }, function()
if misc.safe(self:get_completion_item().textEdit) then
local r = misc.safe(self:get_completion_item().textEdit.insert) or
misc.safe(self:get_completion_item().textEdit.range)
local r = misc.safe(self:get_completion_item().textEdit.insert) or misc.safe(self:get_completion_item().textEdit.range)
if r then
local s = misc.to_vimindex(self.context.cursor_line, r.start.character)
local e = misc.to_vimindex(self.context.cursor_line, r['end'].character)
Expand Down Expand Up @@ -188,8 +186,7 @@ end
---Return the item is deprecated or not.
---@return boolean
entry.is_deprecated = function(self)
return self:get_completion_item().deprecated or
vim.tbl_contains(self:get_completion_item().tags or {}, types.lsp.CompletionItemTag.Deprecated)
return self:get_completion_item().deprecated or vim.tbl_contains(self:get_completion_item().tags or {}, types.lsp.CompletionItemTag.Deprecated)
end

---Return view information.
Expand All @@ -213,8 +210,7 @@ entry.get_view = function(self, suggest_offset, entries_buf)
view.kind.text = item.kind or ''
view.kind.bytes = #view.kind.text
view.kind.width = vim.fn.strdisplaywidth(view.kind.text)
view.kind.hl_group = item.kind_hl_group or ('CmpItemKind' .. (types.lsp.CompletionItemKind[self:get_kind()] or '')
)
view.kind.hl_group = item.kind_hl_group or ('CmpItemKind' .. (types.lsp.CompletionItemKind[self:get_kind()] or ''))
view.menu = {}
view.menu.text = item.menu or ''
view.menu.bytes = #view.menu.text
Expand Down Expand Up @@ -319,8 +315,7 @@ entry.get_insert_range = function(self)
insert_range = {
start = {
line = self.context.cursor.row - 1,
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()),
self.source_insert_range.start.character),
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_insert_range.start.character),
},
['end'] = self.source_insert_range['end'],
}
Expand All @@ -333,14 +328,17 @@ end
entry.get_replace_range = function(self)
return self.cache:ensure({ 'get_replace_range', self.resolved_completion_item and 1 or 0 }, function()
local replace_range
if misc.safe(self:get_completion_item().textEdit) and misc.safe(self:get_completion_item().textEdit.replace) then
replace_range = self:get_completion_item().textEdit.replace
if misc.safe(self:get_completion_item().textEdit) then
if misc.safe(self:get_completion_item().textEdit.replace) then
replace_range = self:get_completion_item().textEdit.replace
else
replace_range = self:get_completion_item().textEdit.range
end
else
replace_range = {
start = {
line = self.source_replace_range.start.line,
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()),
self.source_replace_range.start.character),
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_replace_range.start.character),
},
['end'] = self.source_replace_range['end'],
}
Expand Down

0 comments on commit 9766b16

Please sign in to comment.