Improve expansion and dot-repeatable

This commit is contained in:
hrsh7th
2021-08-06 22:38:40 +09:00
parent ef27b622a7
commit 7e097da01f
3 changed files with 78 additions and 79 deletions

View File

@@ -14,7 +14,7 @@ local cache = require('cmp.utils.cache')
---@field public time number ---@field public time number
---@field public mode string ---@field public mode string
---@field public bufnr number ---@field public bufnr number
---@field public cursor vim.Position ---@field public cursor vim.Position|lsp.Position
---@field public cursor_line string ---@field public cursor_line string
---@field public cursor_after_line string ---@field public cursor_after_line string
---@field public cursor_before_line string ---@field public cursor_before_line string
@@ -51,10 +51,12 @@ context.new = function(prev_context, option)
self.time = vim.loop.now() self.time = vim.loop.now()
self.mode = vim.api.nvim_get_mode().mode self.mode = vim.api.nvim_get_mode().mode
self.bufnr = vim.api.nvim_get_current_buf() self.bufnr = vim.api.nvim_get_current_buf()
self.cursor_line = vim.api.nvim_get_current_line()
self.cursor = {} self.cursor = {}
self.cursor.row = vim.api.nvim_win_get_cursor(0)[1] 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.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_before_line = string.sub(self.cursor_line, 1, self.cursor.col - 1)
self.cursor_after_line = string.sub(self.cursor_line, self.cursor.col) self.cursor_after_line = string.sub(self.cursor_line, self.cursor.col)
return self return self

View File

@@ -192,11 +192,18 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
debug.log('entry.confirm', e:get_completion_item()) 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 --@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 if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) == 0 then
local new = context.new(pre) local pre = context.new()
e:resolve(function() e:resolve(function()
local new = context.new()
local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {} local text_edits = misc.safe(e:get_completion_item().additionalTextEdits) or {}
if #text_edits == 0 then if #text_edits == 0 then
return return
@@ -217,9 +224,10 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
if has_cursor_line_text_edit then if has_cursor_line_text_edit then
return return
end end
vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits) vim.fn['cmp#apply_text_edits'](new.bufnr, text_edits)
end) end)
else
vim.fn['cmp#apply_text_edits'](ctx.bufnr, e:get_completion_item().additionalTextEdits)
end end
-- Prepare completion item for confirmation -- 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() completion_item.textEdit.range = e:get_insert_range()
end end
if #(misc.safe(e:get_completion_item().additionalTextEdits) or {}) > 0 then local is_snippet = vim.lsp.util.parse_snippet(completion_item.textEdit.newText) ~= completion_item.textEdit.newText
vim.fn['cmp#apply_text_edits'](pre.bufnr, e:get_completion_item().additionalTextEdits)
end
-- First, emulates vim's `<C-y>` behavior and then confirms LSP functionalities. local keys = ''
local range = types.lsp.Range.to_vim(pre.bufnr, completion_item.textEdit.range) if completion_item.textEdit.range['end'].character > e.context.cursor.character then
local before_text = string.sub(pre.cursor_line, range.start.col, pre.cursor.col - 1) keys = keys .. string.rep('<C-g>U<Right><BS>', completion_item.textEdit.range['end'].character - e.context.cursor.character)
local after_text = string.sub(pre.cursor_line, pre.cursor.col, pre.cursor.col + (range['end'].col - e.context.cursor.col) - 1) end
local before_len = vim.fn.strchars(before_text) if e.context.cursor.character > completion_item.textEdit.range.start.character then
local after_len = vim.fn.strchars(after_text) keys = keys .. string.rep('<BS>', e.context.cursor.character - completion_item.textEdit.range.start.character)
local keys = '<C-g>u' .. string.rep('<C-g>U<Right>', after_len) .. string.rep('<BS>', before_len + after_len) end
if not completion_item.insertTextFormat == types.lsp.InsertTextFormat.Snippet then if not is_snippet then
keys = keys .. completion_item.textEdit.newText keys = keys .. '<C-g>u' .. completion_item.textEdit.newText
else
keys = keys .. e:get_word()
end end
keymap.feedkeys( keymap.feedkeys(
keys, keys,
'n', 'n',
vim.schedule_wrap(function() 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() e:execute(function()
if callback then if callback then
callback() callback()
end end
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) end)
) )
else
execute()
end
end) end)
) )
end) end)

View File

@@ -4,13 +4,15 @@ local pattern = require 'cmp.utils.pattern'
local str = {} local str = {}
local INVALID_CHARS = {} 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('[')] = 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('\t')] = true
INVALID_CHARS[string.byte("\n")] = true INVALID_CHARS[string.byte('\n')] = true
local PAIR_CHARS = {} local PAIR_CHARS = {}
PAIR_CHARS[string.byte('[')] = string.byte(']') PAIR_CHARS[string.byte('[')] = string.byte(']')