Check buftype is prompt

Improve incomplete handling
This commit is contained in:
hrsh7th
2021-08-10 16:57:54 +09:00
parent 35d04be45e
commit 22ec3ad442
4 changed files with 41 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ local float = require('cmp.float')
local types = require('cmp.types')
local config = require('cmp.config')
local autocmd = require('cmp.autocmd')
local check = require('cmp.utils.check')
---@class cmp.MenuOption
---@field on_select fun(e: cmp.Entry)
@@ -67,11 +68,7 @@ end
---@param ctx cmp.Context
---@param sources cmp.Source[]
---@return cmp.Menu
menu.update = function(self, ctx, sources)
if not (ctx.mode == 'i' or ctx.mode == 'ic') then
return
end
menu.update = check.wrap(function(self, ctx, sources)
local entries = {}
local entry_map = {}
@@ -139,15 +136,11 @@ menu.update = function(self, ctx, sources)
self.preselect = preselect
self.context = ctx
self:show()
end
end)
---Restore previous menu
---@param ctx cmp.Context
menu.restore = function(self, ctx)
if not (ctx.mode == 'i' or ctx.mode == 'ic') then
return
end
menu.restore = check.wrap(function(self, ctx)
if not ctx.pumvisible then
if #self.items > 0 then
if self.offset <= ctx.cursor.col then
@@ -156,7 +149,7 @@ menu.restore = function(self, ctx)
end
end
end
end
end)
---Show completion item
menu.show = function(self)

View File

@@ -187,12 +187,9 @@ end
---@param callback function
---@return boolean Return true if not trigger completion.
source.complete = function(self, ctx, callback)
local c = config.get()
local offset = ctx:get_offset(self:get_keyword_pattern())
if ctx.cursor.col <= offset then
self:reset()
end
local c = config.get()
local before_char = string.sub(ctx.cursor_before_line, -1)
local before_char_iw = string.match(ctx.cursor_before_line, '(.)%s*$') or before_char
@@ -222,27 +219,32 @@ source.complete = function(self, ctx, callback)
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.request_offset ~= offset then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.Invoked,
}
elseif self.incomplete then
elseif ctx:get_reason() ~= types.cmp.ContextReason.TriggerOnly then
if c.completion.keyword_length <= (ctx.cursor.col - offset) then
if self.incomplete and self.context.cursor.col ~= ctx.cursor.col then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.TriggerForIncompleteCompletions,
}
elseif self.request_offset ~= offset then
completion_context = {
triggerKind = types.lsp.CompletionTriggerKind.Invoked,
}
end
else
self:reset()
end
else
self:reset()
end
end
if ctx.cursor.col <= offset then
self:reset()
end
if not completion_context then
debug.log('skip empty context', self.name, self.id)
if ctx:get_reason() == types.cmp.ContextReason.TriggerOnly then
self:reset()
end
debug.log('skip completion', self.name, self.id)
return
end

18
lua/cmp/utils/check.lua Normal file
View File

@@ -0,0 +1,18 @@
local check = {}
check.ok = function()
local ng = false
ng = ng or vim.api.nvim_buf_get_option(0, 'buftype') == 'prompt'
ng = ng or string.sub(vim.api.nvim_get_mode().mode, 1, 1) ~= 'i'
return not ng
end
check.wrap = function(callback)
return function(...)
if check.ok() then
callback(...)
end
end
end
return check