From 22ec3ad442462294b464e4418126ffdecd0cafe0 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Tue, 10 Aug 2021 16:57:54 +0900 Subject: [PATCH] Check buftype is prompt Improve incomplete handling --- Makefile | 2 +- lua/cmp/menu.lua | 17 +++++------------ lua/cmp/source.lua | 32 +++++++++++++++++--------------- lua/cmp/utils/check.lua | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 lua/cmp/utils/check.lua diff --git a/Makefile b/Makefile index 900f10f..9a69720 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ test: .PHONY: pre-commit pre-commit: - stylua --check --glob lua/**/*.lua -- lua + stylua --glob lua/**/*.lua -- lua luacheck lua vusted lua diff --git a/lua/cmp/menu.lua b/lua/cmp/menu.lua index 71f2f0d..22fdc83 100644 --- a/lua/cmp/menu.lua +++ b/lua/cmp/menu.lua @@ -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) diff --git a/lua/cmp/source.lua b/lua/cmp/source.lua index 379d8b1..d57feb9 100644 --- a/lua/cmp/source.lua +++ b/lua/cmp/source.lua @@ -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 diff --git a/lua/cmp/utils/check.lua b/lua/cmp/utils/check.lua new file mode 100644 index 0000000..e5a9cbe --- /dev/null +++ b/lua/cmp/utils/check.lua @@ -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