From af70f40d2eb6db2121536c8df2e114af759df537 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Thu, 30 Sep 2021 21:06:32 +0900 Subject: [PATCH] Add is_insert_mode utility --- lua/cmp/context.lua | 6 ------ lua/cmp/core.lua | 9 ++++++--- lua/cmp/utils/misc.lua | 6 ++++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lua/cmp/context.lua b/lua/cmp/context.lua index 9eaa7d6..c7de4e5 100644 --- a/lua/cmp/context.lua +++ b/lua/cmp/context.lua @@ -65,12 +65,6 @@ context.new = function(prev_context, option) return self end ----Return the context is invalid or not. ----@return boolean -context.invalid = function(self) - return string.sub(self.mode, 1, 1) ~= 'i' -end - ---Return context creation reason. ---@return cmp.ContextReason context.get_reason = function(self) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index f8d5996..67a5346 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -85,6 +85,9 @@ core.register_source = function(s) core.sources_by_name[s.name] = {} end table.insert(core.sources_by_name[s.name], s) + if misc.is_insert_mode() then + core.complete(core.get_context({ reason = types.cmp.ContextReason.Auto })) + end end ---Unregister source @@ -253,7 +256,7 @@ end ---Invoke completion ---@param ctx cmp.Context core.complete = function(ctx) - if ctx:invalid() then + if not misc.is_insert_mode() then return end @@ -278,10 +281,10 @@ end ---Update completion menu core.filter = async.throttle(function() - local ctx = core.get_context() - if ctx:invalid() then + if not misc.is_insert_mode() then return end + local ctx = core.get_context() -- To wait for processing source for that's timeout. local sources = {} diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index cb3ebf6..244005d 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -22,6 +22,12 @@ misc.get_cursor_before_line = function() return string.sub(vim.api.nvim_get_current_line(), 1, cursor[2]) end +---Return current mode is insert-mode or not. +---@return boolean +misc.is_insert_mode = function() + return string.sub(vim.api.nvim_get_mode().mode, 1, 1) == 'i' +end + ---Merge two tables recursively ---@generic T ---@param v1 T