Add is_insert_mode utility

This commit is contained in:
hrsh7th
2021-09-30 21:06:32 +09:00
parent a63a1a23e9
commit af70f40d2e
3 changed files with 12 additions and 9 deletions

View File

@@ -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)

View File

@@ -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 = {}

View File

@@ -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