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

@@ -12,7 +12,7 @@ test:
.PHONY: pre-commit .PHONY: pre-commit
pre-commit: pre-commit:
stylua --check --glob lua/**/*.lua -- lua stylua --glob lua/**/*.lua -- lua
luacheck lua luacheck lua
vusted lua vusted lua

View File

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

View File

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