Add trigger character check after confirmation

This commit is contained in:
hrsh7th
2021-08-04 01:42:06 +09:00
parent d23d3533cf
commit e62a03c5cc
5 changed files with 44 additions and 20 deletions

View File

@@ -18,7 +18,6 @@ local cache = require('cmp.utils.cache')
---@field public cursor_line string
---@field public cursor_after_line string
---@field public cursor_before_line string
---@field public before_char string
local context = {}
---Create new empty context
@@ -58,7 +57,6 @@ context.new = function(prev_context, option)
self.cursor_line = vim.api.nvim_get_current_line()
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.before_char = string.sub(self.cursor_line, self.cursor.col - 1, self.cursor.col - 1)
return self
end

View File

@@ -71,9 +71,18 @@ core.on_keymap = function(keys, fallback)
if not e then
return fallback()
end
return core.confirm(e, {
local pre = core.get_context()
core.confirm(e, {
behavior = c.behavior,
})
}, function()
local new = core.get_context({ reason = types.cmp.ContextReason.TriggerOnly })
if new:changed(pre) then
core.complete(new)
else
core.reset()
end
end)
return
end
--Commit character. NOTE: This has a lot of cmp specific implementation to make more user-friendly.
@@ -88,6 +97,8 @@ core.on_keymap = function(keys, fallback)
local word = e:get_word()
if string.sub(ctx.cursor_before_line, -#word, ctx.cursor.col - 1) == word and is_printable then
fallback()
else
core.reset()
end
end)
end
@@ -233,7 +244,6 @@ core.confirm = vim.schedule_wrap(function(e, option, callback)
-- execute
e:execute(function()
core.reset()
if callback then
callback()
end

View File

@@ -195,24 +195,40 @@ source.complete = function(self, ctx, callback)
self:reset()
end
local before_char = string.sub(ctx.cursor_before_line, -1)
local before_char_iw = string.match(ctx.cursor_before_line, '(.)%s*$') or before_char
local completion_context
if ctx:get_reason() == types.cmp.ContextReason.Manual then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.Invoked,
}
elseif vim.tbl_contains(self:get_trigger_characters(), ctx.before_char) then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerCharacter,
triggerCharacter = ctx.before_char,
}
elseif c.completion.keyword_length <= (ctx.cursor.col - offset) and self.offset ~= offset then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.Invoked,
}
elseif self.incomplete and offset ~= ctx.cursor.col then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerForIncompleteCompletions,
}
else
if vim.tbl_contains(self:get_trigger_characters(), before_char) then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerCharacter,
triggerCharacter = before_char,
}
elseif vim.tbl_contains(self:get_trigger_characters(), before_char_iw) then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerCharacter,
triggerCharacter = before_char_iw,
}
else
if ctx:get_reason() == types.cmp.ContextReason.Auto then
if c.completion.keyword_length <= (ctx.cursor.col - offset) and self.offset ~= offset then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.Invoked,
}
elseif self.incomplete and offset ~= ctx.cursor.col then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerForIncompleteCompletions,
}
end
else
self:reset()
end
end
end
if not completion_context then
debug.log('skip empty context', self.name, self.id)

View File

@@ -9,6 +9,7 @@ cmp.ConfirmBehavior.Replace = 'replace'
cmp.ContextReason = {}
cmp.ContextReason.Auto = 'auto'
cmp.ContextReason.Manual = 'manual'
cmp.ContextReason.TriggerOnly = 'triggerOnly'
cmp.ContextReason.None = 'none'
---@alias cmp.TriggerEvent "'InsertEnter'" | "'TextChanged'"