add cmp.get_registered_sources and CmpRegisterSource /CmpUnregisterSource autocmd.

This commit is contained in:
hrsh7th
2024-12-10 12:04:15 +09:00
parent ca4d3330d3
commit 1d57252744
3 changed files with 39 additions and 1 deletions

View File

@@ -154,6 +154,9 @@ NOTE: `<Cmd>lua require('cmp').complete()<CR>` can be used to call these functio
See |getcmdtype()|. See |getcmdtype()|.
NOTE: nvim-cmp does not support the `=` command type. NOTE: nvim-cmp does not support the `=` command type.
*cmp.get_registered_sources* ()
Get all registered sources.
*cmp.visible* () *cmp.visible* ()
Return a boolean showing whether the completion menu is visible or not. Return a boolean showing whether the completion menu is visible or not.
@@ -427,6 +430,12 @@ autocommands for the User event with the following patterns:
*CmpReady* *CmpReady*
Invoked when nvim-cmp gets sourced from `plugin/cmp.lua`. Invoked when nvim-cmp gets sourced from `plugin/cmp.lua`.
*CmpRegisterSource*
Invoke when source was registered.
*CmpUnregisterSource*
Invoke when source was un-registered.
============================================================================== ==============================================================================
Config *cmp-config* Config *cmp-config*

View File

@@ -47,8 +47,11 @@ end
---Unregister source ---Unregister source
---@param source_id integer ---@param source_id integer
---@return cmp.Source?
core.unregister_source = function(self, source_id) core.unregister_source = function(self, source_id)
local s = self.sources[source_id]
self.sources[source_id] = nil self.sources[source_id] = nil
return s
end end
---Get new context ---Get new context
@@ -105,6 +108,12 @@ core.get_sources = function(self, filter)
return sources return sources
end end
---Return registered sources.
---@return cmp.Source[]
core.get_registered_sources = function(self)
return self.sources
end
---Keypress handler ---Keypress handler
core.on_keymap = function(self, keys, fallback) core.on_keymap = function(self, keys, fallback)
local mode = api.get_mode() local mode = api.get_mode()

View File

@@ -54,13 +54,33 @@ end
cmp.register_source = function(name, s) cmp.register_source = function(name, s)
local src = source.new(name, s) local src = source.new(name, s)
cmp.core:register_source(src) cmp.core:register_source(src)
vim.api.nvim_exec_autocmds('User', {
pattern = 'CmpRegisterSource',
data = {
source_id = src.id
}
})
return src.id return src.id
end end
---Unregister completion source ---Unregister completion source
---@param id integer ---@param id integer
cmp.unregister_source = function(id) cmp.unregister_source = function(id)
cmp.core:unregister_source(id) local s = cmp.core:unregister_source(id)
if s then
vim.api.nvim_exec_autocmds('User', {
pattern = 'CmpUnregisterSource',
data = {
source_id = id
}
})
end
end
---Get registered sources.
---@return cmp.Source[]
cmp.get_registered_sources = function()
return cmp.core:get_registered_sources()
end end
---Get current configuration. ---Get current configuration.