Improve expansion and dot-repeatable
This commit is contained in:
@@ -14,7 +14,7 @@ local cache = require('cmp.utils.cache')
|
||||
---@field public time number
|
||||
---@field public mode string
|
||||
---@field public bufnr number
|
||||
---@field public cursor vim.Position
|
||||
---@field public cursor vim.Position|lsp.Position
|
||||
---@field public cursor_line string
|
||||
---@field public cursor_after_line string
|
||||
---@field public cursor_before_line string
|
||||
@@ -51,10 +51,12 @@ context.new = function(prev_context, option)
|
||||
self.time = vim.loop.now()
|
||||
self.mode = vim.api.nvim_get_mode().mode
|
||||
self.bufnr = vim.api.nvim_get_current_buf()
|
||||
self.cursor_line = vim.api.nvim_get_current_line()
|
||||
self.cursor = {}
|
||||
self.cursor.row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
self.cursor.col = vim.api.nvim_win_get_cursor(0)[2] + 1
|
||||
self.cursor_line = vim.api.nvim_get_current_line()
|
||||
self.cursor.line = self.cursor.row - 1
|
||||
self.cursor.character = vim.str_utfindex(self.cursor_line, self.cursor.col - 1)
|
||||
self.cursor_before_line = string.sub(self.cursor_line, 1, self.cursor.col - 1)
|
||||
self.cursor_after_line = string.sub(self.cursor_line, self.cursor.col)
|
||||
return self
|
||||
|
||||
@@ -192,11 +192,18 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
|
||||
debug.log('entry.confirm', e:get_completion_item())
|
||||
|
||||
local ctx = context.new()
|
||||
local restore_text = string.sub(ctx.cursor_line, e.context.cursor.col, ctx.cursor.col - 1)
|
||||
local restore_keys = string.rep('<BS>', vim.fn.strchars(restore_text))
|
||||
keymap.feedkeys(
|
||||
'<C-g>U' .. restore_keys,
|
||||
'n',
|
||||
vim.schedule_wrap(function()
|
||||
--@see https://github.com/microsoft/vscode/blob/main/src/vs/editor/contrib/suggest/suggestController.ts#L334
|
||||
local pre = context.new()
|
||||
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
|
||||
local new = context.new(pre)
|
||||
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
|
||||
@@ -217,9 +224,10 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
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
|
||||
@@ -235,48 +243,35 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
|
||||
completion_item.textEdit.range = e:get_insert_range()
|
||||
end
|
||||
|
||||
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) > 0 then
|
||||
vim.fn['cmp#apply_text_edits'](pre.bufnr, e:get_completion_item().additionalTextEdits)
|
||||
end
|
||||
local is_snippet = vim.lsp.util.parse_snippet(completion_item.textEdit.newText) ~= completion_item.textEdit.newText
|
||||
|
||||
-- First, emulates vim's `<C-y>` behavior and then confirms LSP functionalities.
|
||||
local range = types.lsp.Range.to_vim(pre.bufnr, completion_item.textEdit.range)
|
||||
local before_text = string.sub(pre.cursor_line, range.start.col, pre.cursor.col - 1)
|
||||
local after_text = string.sub(pre.cursor_line, pre.cursor.col, pre.cursor.col + (range['end'].col - e.context.cursor.col) - 1)
|
||||
local before_len = vim.fn.strchars(before_text)
|
||||
local after_len = vim.fn.strchars(after_text)
|
||||
local keys = '<C-g>u' .. string.rep('<C-g>U<Right>', after_len) .. string.rep('<BS>', before_len + after_len)
|
||||
if not completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet then
|
||||
keys = keys .. completion_item.textEdit.newText
|
||||
else
|
||||
keys = keys .. e:get_word()
|
||||
local keys = ''
|
||||
if completion_item.textEdit.range['end'].character > e.context.cursor.character then
|
||||
keys = keys .. string.rep('<C-g>U<Right><BS>', completion_item.textEdit.range['end'].character - e.context.cursor.character)
|
||||
end
|
||||
if e.context.cursor.character > completion_item.textEdit.range.start.character then
|
||||
keys = keys .. string.rep('<BS>', e.context.cursor.character - completion_item.textEdit.range.start.character)
|
||||
end
|
||||
if not is_snippet then
|
||||
keys = keys .. '<C-g>u' .. completion_item.textEdit.newText
|
||||
end
|
||||
keymap.feedkeys(
|
||||
keys,
|
||||
'n',
|
||||
vim.schedule_wrap(function()
|
||||
local execute = function()
|
||||
if is_snippet then
|
||||
config.get().snippet.expand({
|
||||
body = completion_item.textEdit.newText,
|
||||
insert_text_mode = completion_item.insertTextMode,
|
||||
})
|
||||
end
|
||||
e:execute(function()
|
||||
if callback then
|
||||
callback()
|
||||
end
|
||||
end)
|
||||
end
|
||||
if completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet then
|
||||
keymap.feedkeys(
|
||||
'<C-g>U' .. string.rep('<BS>', vim.fn.strchars(e:get_word())),
|
||||
'n',
|
||||
vim.schedule_wrap(function()
|
||||
config.get().snippet.expand({
|
||||
body = completion_item.textEdit.newText,
|
||||
insert_text_mode = completion_item.insertTextMode,
|
||||
})
|
||||
execute()
|
||||
end)
|
||||
)
|
||||
else
|
||||
execute()
|
||||
end
|
||||
end)
|
||||
)
|
||||
end)
|
||||
|
||||
@@ -4,13 +4,15 @@ local pattern = require 'cmp.utils.pattern'
|
||||
local str = {}
|
||||
|
||||
local INVALID_CHARS = {}
|
||||
INVALID_CHARS[string.byte("'")] = true
|
||||
INVALID_CHARS[string.byte('"')] = true
|
||||
INVALID_CHARS[string.byte('=')] = true
|
||||
INVALID_CHARS[string.byte('$')] = true
|
||||
INVALID_CHARS[string.byte('(')] = true
|
||||
INVALID_CHARS[string.byte('[')] = true
|
||||
INVALID_CHARS[string.byte('"')] = true
|
||||
INVALID_CHARS[string.byte("'")] = true
|
||||
INVALID_CHARS[string.byte("\n")] = true
|
||||
INVALID_CHARS[string.byte(' ')] = true
|
||||
INVALID_CHARS[string.byte('\t')] = true
|
||||
INVALID_CHARS[string.byte('\n')] = true
|
||||
|
||||
local PAIR_CHARS = {}
|
||||
PAIR_CHARS[string.byte('[')] = string.byte(']')
|
||||
|
||||
Reference in New Issue
Block a user