Use apply_text_edits to avoid making the unexpected indentation by '<CR>'
This commit is contained in:
@@ -304,22 +304,27 @@ core.confirm = function(self, e, option, callback)
|
||||
debug.log('entry.confirm', e:get_completion_item())
|
||||
|
||||
local release = self:suspend()
|
||||
local ctx = self:get_context()
|
||||
|
||||
-- Close menus.
|
||||
self.view:close()
|
||||
|
||||
-- Simulate `<C-y>` behavior.
|
||||
async.step(function(next)
|
||||
local ctx = context.new()
|
||||
local confirm = {}
|
||||
table.insert(confirm, keymap.backspace(ctx.cursor.character - misc.to_utfindex(e.context.cursor_before_line, e:get_offset())))
|
||||
table.insert(confirm, e:get_word())
|
||||
feedkeys.call(table.concat(confirm, ''), 'nt', function()
|
||||
feedkeys.call(table.concat(confirm, ''), 'nt', next)
|
||||
|
||||
-- Restore to the requested state.
|
||||
end, function(next)
|
||||
local restore = {}
|
||||
table.insert(restore, keymap.backspace(vim.str_utfindex(e:get_word())))
|
||||
table.insert(restore, string.sub(e.context.cursor_before_line, e:get_offset()))
|
||||
feedkeys.call(table.concat(restore, ''), 'n', function()
|
||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||
feedkeys.call(table.concat(restore, ''), 'n', next)
|
||||
|
||||
-- Async additionalTextEdits @see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||
end, function(next)
|
||||
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
||||
local pre = context.new()
|
||||
e:resolve(function()
|
||||
@@ -347,10 +352,13 @@ core.confirm = function(self, e, option, callback)
|
||||
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)
|
||||
vim.fn['cmp#apply_text_edits'](vim.api.nvim_get_current_buf(), e:get_completion_item().additionalTextEdits)
|
||||
end
|
||||
next()
|
||||
|
||||
-- Prepare completion item for confirmation
|
||||
-- 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 = {}
|
||||
@@ -363,55 +371,32 @@ core.confirm = function(self, e, option, callback)
|
||||
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
|
||||
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
|
||||
table.insert(keys, e:get_word())
|
||||
else
|
||||
table.insert(keys, completion_item.textEdit.newText)
|
||||
completion_item.textEdit.newText = ''
|
||||
end
|
||||
|
||||
feedkeys.call(table.concat(keys, ''), 'n', function()
|
||||
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit })
|
||||
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,
|
||||
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)
|
||||
--For backward compatibility
|
||||
if config.get().event.on_confirm_done then
|
||||
config.get().event.on_confirm_done(e)
|
||||
end
|
||||
if callback then
|
||||
callback()
|
||||
end
|
||||
end))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
---Reset current completion state
|
||||
|
||||
@@ -35,6 +35,18 @@ async.throttle = function(fn, timeout)
|
||||
})
|
||||
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
|
||||
---@param fn function
|
||||
---@param timeout number
|
||||
|
||||
@@ -40,4 +40,30 @@ describe('utils.async', function()
|
||||
end)
|
||||
assert.is.truthy(math.abs(vim.loop.now() - now) < 10)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user