Add is_insert_mode utility
This commit is contained in:
@@ -65,12 +65,6 @@ context.new = function(prev_context, option)
|
|||||||
return self
|
return self
|
||||||
end
|
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 context creation reason.
|
||||||
---@return cmp.ContextReason
|
---@return cmp.ContextReason
|
||||||
context.get_reason = function(self)
|
context.get_reason = function(self)
|
||||||
|
|||||||
@@ -85,6 +85,9 @@ core.register_source = function(s)
|
|||||||
core.sources_by_name[s.name] = {}
|
core.sources_by_name[s.name] = {}
|
||||||
end
|
end
|
||||||
table.insert(core.sources_by_name[s.name], s)
|
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
|
end
|
||||||
|
|
||||||
---Unregister source
|
---Unregister source
|
||||||
@@ -253,7 +256,7 @@ end
|
|||||||
---Invoke completion
|
---Invoke completion
|
||||||
---@param ctx cmp.Context
|
---@param ctx cmp.Context
|
||||||
core.complete = function(ctx)
|
core.complete = function(ctx)
|
||||||
if ctx:invalid() then
|
if not misc.is_insert_mode() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -278,10 +281,10 @@ end
|
|||||||
|
|
||||||
---Update completion menu
|
---Update completion menu
|
||||||
core.filter = async.throttle(function()
|
core.filter = async.throttle(function()
|
||||||
local ctx = core.get_context()
|
if not misc.is_insert_mode() then
|
||||||
if ctx:invalid() then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local ctx = core.get_context()
|
||||||
|
|
||||||
-- To wait for processing source for that's timeout.
|
-- To wait for processing source for that's timeout.
|
||||||
local sources = {}
|
local sources = {}
|
||||||
|
|||||||
@@ -22,6 +22,12 @@ misc.get_cursor_before_line = function()
|
|||||||
return string.sub(vim.api.nvim_get_current_line(), 1, cursor[2])
|
return string.sub(vim.api.nvim_get_current_line(), 1, cursor[2])
|
||||||
end
|
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
|
---Merge two tables recursively
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param v1 T
|
---@param v1 T
|
||||||
|
|||||||
Reference in New Issue
Block a user