Use apply_text_edits to avoid making the unexpected indentation by '<CR>'
This commit is contained in:
175
lua/cmp/core.lua
175
lua/cmp/core.lua
@@ -304,113 +304,98 @@ core.confirm = function(self, e, option, callback)
|
|||||||
debug.log('entry.confirm', e:get_completion_item())
|
debug.log('entry.confirm', e:get_completion_item())
|
||||||
|
|
||||||
local release = self:suspend()
|
local release = self:suspend()
|
||||||
local ctx = self:get_context()
|
|
||||||
|
|
||||||
-- Close menus.
|
-- Close menus.
|
||||||
self.view:close()
|
self.view:close()
|
||||||
|
|
||||||
-- Simulate `<C-y>` behavior.
|
-- Simulate `<C-y>` behavior.
|
||||||
local confirm = {}
|
async.step(function(next)
|
||||||
table.insert(confirm, keymap.backspace(ctx.cursor.character - misc.to_utfindex(e.context.cursor_before_line, e:get_offset())))
|
local ctx = context.new()
|
||||||
table.insert(confirm, e:get_word())
|
local confirm = {}
|
||||||
feedkeys.call(table.concat(confirm, ''), 'nt', function()
|
table.insert(confirm, keymap.backspace(ctx.cursor.character - misc.to_utfindex(e.context.cursor_before_line, e:get_offset())))
|
||||||
-- Restore to the requested state.
|
table.insert(confirm, e:get_word())
|
||||||
|
feedkeys.call(table.concat(confirm, ''), 'nt', next)
|
||||||
|
|
||||||
|
-- Restore to the requested state.
|
||||||
|
end, function(next)
|
||||||
local restore = {}
|
local restore = {}
|
||||||
table.insert(restore, keymap.backspace(vim.str_utfindex(e:get_word())))
|
table.insert(restore, keymap.backspace(vim.str_utfindex(e:get_word())))
|
||||||
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
||||||
feedkeys.call(table.concat(restore, ''), 'n', function()
|
feedkeys.call(table.concat(restore, ''), 'n', next)
|
||||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
|
||||||
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
|
||||||
local pre = context.new()
|
|
||||||
e:resolve(function()
|
|
||||||
local new = context.new()
|
|
||||||
local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {}
|
|
||||||
if #text_edits == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local has_cursor_line_text_edit = (function()
|
-- Async additionalTextEdits @see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||||
local minrow = math.min(pre.cursor.row, new.cursor.row)
|
end, function(next)
|
||||||
local maxrow = math.max(pre.cursor.row, new.cursor.row)
|
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
||||||
for _, te in ipairs(text_edits) do
|
local pre = context.new()
|
||||||
local srow = te.range.start.line + 1
|
e:resolve(function()
|
||||||
local erow = te.range['end'].line + 1
|
local new = context.new()
|
||||||
if srow <= minrow and maxrow <= erow then
|
local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {}
|
||||||
return true
|
if #text_edits == 0 then
|
||||||
end
|
return
|
||||||
end
|
|
||||||
return false
|
|
||||||
end)()
|
|
||||||
if has_cursor_line_text_edit then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits)
|
|
||||||
end)
|
|
||||||
else
|
|
||||||
vim.fn['cmp#apply_text_edits'](ctx.bufnr, e:get_completion_item().additionalTextEdits)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Prepare completion item for confirmation
|
|
||||||
local completion_item = misc.copy(e:get_completion_item())
|
|
||||||
if not misc.safe(completion_item.textEdit) then
|
|
||||||
completion_item.textEdit = {}
|
|
||||||
completion_item.textEdit.newText = misc.safe(completion_item.insertText) or completion_item.word or completion_item.label
|
|
||||||
end
|
|
||||||
local behavior = option.behavior or config.get().confirmation.default_behavior
|
|
||||||
if behavior == types.cmp.ConfirmBehavior.Replace then
|
|
||||||
completion_item.textEdit.range = e:get_replace_range()
|
|
||||||
else
|
|
||||||
completion_item.textEdit.range = e:get_insert_range()
|
|
||||||
end
|
|
||||||
|
|
||||||
local keys = {}
|
|
||||||
if e.context.cursor.character < completion_item.textEdit.range['end'].character then
|
|
||||||
table.insert(keys, keymap.t(string.rep('<Del>', completion_item.textEdit.range['end'].character - e.context.cursor.character)))
|
|
||||||
end
|
|
||||||
if completion_item.textEdit.range.start.character < e.context.cursor.character then
|
|
||||||
table.insert(keys, keymap.backspace(e.context.cursor.character - completion_item.textEdit.range.start.character))
|
|
||||||
end
|
|
||||||
table.insert(keys, keymap.undobreak())
|
|
||||||
|
|
||||||
local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
|
|
||||||
if is_snippet then
|
|
||||||
table.insert(keys, e:get_word())
|
|
||||||
else
|
|
||||||
table.insert(keys, completion_item.textEdit.newText)
|
|
||||||
end
|
|
||||||
|
|
||||||
feedkeys.call(table.concat(keys, ''), 'n', function()
|
|
||||||
if is_snippet then
|
|
||||||
-- remove snippet prefix without changing `dot` register.
|
|
||||||
local snippet_ctx = context.new()
|
|
||||||
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { {
|
|
||||||
range = {
|
|
||||||
start = {
|
|
||||||
line = snippet_ctx.cursor.line,
|
|
||||||
character = snippet_ctx.cursor.character - vim.str_utfindex(e:get_word()),
|
|
||||||
},
|
|
||||||
['end'] = snippet_ctx.cursor,
|
|
||||||
},
|
|
||||||
newText = '',
|
|
||||||
} })
|
|
||||||
config.get().snippet.expand({
|
|
||||||
body = completion_item.textEdit.newText,
|
|
||||||
insert_text_mode = completion_item.insertTextMode,
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
e:execute(vim.schedule_wrap(function()
|
|
||||||
release()
|
local has_cursor_line_text_edit = (function()
|
||||||
self.event:emit('confirm_done', e)
|
local minrow = math.min(pre.cursor.row, new.cursor.row)
|
||||||
--For backward compatibility
|
local maxrow = math.max(pre.cursor.row, new.cursor.row)
|
||||||
if config.get().event.on_confirm_done then
|
for _, te in ipairs(text_edits) do
|
||||||
config.get().event.on_confirm_done(e)
|
local srow = te.range.start.line + 1
|
||||||
|
local erow = te.range['end'].line + 1
|
||||||
|
if srow <= minrow and maxrow <= erow then
|
||||||
|
return true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if callback then
|
return false
|
||||||
callback()
|
end)()
|
||||||
end
|
if has_cursor_line_text_edit then
|
||||||
end))
|
return
|
||||||
|
end
|
||||||
|
vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits)
|
||||||
end)
|
end)
|
||||||
end)
|
else
|
||||||
|
vim.fn['cmp#apply_text_edits'](vim.api.nvim_get_current_buf(), e:get_completion_item().additionalTextEdits)
|
||||||
|
end
|
||||||
|
next()
|
||||||
|
|
||||||
|
-- Expand completion item
|
||||||
|
end, function(next)
|
||||||
|
local ctx = context.new()
|
||||||
|
local completion_item = misc.copy(e:get_completion_item())
|
||||||
|
if not misc.safe(completion_item.textEdit) then
|
||||||
|
completion_item.textEdit = {}
|
||||||
|
completion_item.textEdit.newText = misc.safe(completion_item.insertText) or completion_item.word or completion_item.label
|
||||||
|
end
|
||||||
|
local behavior = option.behavior or config.get().confirmation.default_behavior
|
||||||
|
if behavior == types.cmp.ConfirmBehavior.Replace then
|
||||||
|
completion_item.textEdit.range = e:get_replace_range()
|
||||||
|
else
|
||||||
|
completion_item.textEdit.range = e:get_insert_range()
|
||||||
|
end
|
||||||
|
|
||||||
|
local is_snippet = completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet
|
||||||
|
local new_text = completion_item.textEdit.newText
|
||||||
|
completion_item.textEdit.range.start.line = ctx.cursor.line
|
||||||
|
completion_item.textEdit.range.start.character = ctx.cursor.character - (e.context.cursor.character - completion_item.textEdit.range.start.character)
|
||||||
|
completion_item.textEdit.range['end'].line = ctx.cursor.line
|
||||||
|
completion_item.textEdit.range['end'].character = ctx.cursor.character + (completion_item.textEdit.range['end'].character - e.context.cursor.character)
|
||||||
|
if is_snippet then
|
||||||
|
completion_item.textEdit.newText = ''
|
||||||
|
end
|
||||||
|
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit })
|
||||||
|
if is_snippet then
|
||||||
|
config.get().snippet.expand({
|
||||||
|
body = new_text,
|
||||||
|
insert_text_mode = completion_item.insertTextMode,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
next()
|
||||||
|
end, function()
|
||||||
|
e:execute(vim.schedule_wrap(function()
|
||||||
|
release()
|
||||||
|
self.event:emit('confirm_done', e)
|
||||||
|
if callback then
|
||||||
|
callback()
|
||||||
|
end
|
||||||
|
end))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,18 @@ async.throttle = function(fn, timeout)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Control async tasks.
|
||||||
|
async.step = function(...)
|
||||||
|
local tasks = { ... }
|
||||||
|
local next
|
||||||
|
next = function()
|
||||||
|
if #tasks > 0 then
|
||||||
|
table.remove(tasks, 1)(next)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.remove(tasks, 1)(next)
|
||||||
|
end
|
||||||
|
|
||||||
---Timeout callback function
|
---Timeout callback function
|
||||||
---@param fn function
|
---@param fn function
|
||||||
---@param timeout number
|
---@param timeout number
|
||||||
|
|||||||
@@ -40,4 +40,30 @@ describe('utils.async', function()
|
|||||||
end)
|
end)
|
||||||
assert.is.truthy(math.abs(vim.loop.now() - now) < 10)
|
assert.is.truthy(math.abs(vim.loop.now() - now) < 10)
|
||||||
end)
|
end)
|
||||||
|
it('step', function()
|
||||||
|
local done = false
|
||||||
|
local step = {}
|
||||||
|
async.step(function(next)
|
||||||
|
vim.defer_fn(function()
|
||||||
|
table.insert(step, 1)
|
||||||
|
next()
|
||||||
|
end, 10)
|
||||||
|
end, function(next)
|
||||||
|
vim.defer_fn(function()
|
||||||
|
table.insert(step, 2)
|
||||||
|
next()
|
||||||
|
end, 10)
|
||||||
|
end, function(next)
|
||||||
|
vim.defer_fn(function()
|
||||||
|
table.insert(step, 3)
|
||||||
|
next()
|
||||||
|
end, 10)
|
||||||
|
end, function()
|
||||||
|
done = true
|
||||||
|
end)
|
||||||
|
vim.wait(1000, function()
|
||||||
|
return done
|
||||||
|
end)
|
||||||
|
assert.are.same(step, { 1, 2, 3 })
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user