Add is_insert_mode utility
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 = {}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user