Skip to content

Commit

Permalink
add cmp.get_registered_sources and CmpRegisterSource /`CmpUnregis…
Browse files Browse the repository at this point in the history
…terSource` autocmd.
  • Loading branch information
hrsh7th committed Dec 10, 2024
1 parent ca4d333 commit 1d57252
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions doc/cmp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ NOTE: `<Cmd>lua require('cmp').complete()<CR>` can be used to call these functio
See |getcmdtype()|.
NOTE: nvim-cmp does not support the `=` command type.

*cmp.get_registered_sources* ()
Get all registered sources.

*cmp.visible* ()
Return a boolean showing whether the completion menu is visible or not.

Expand Down Expand Up @@ -427,6 +430,12 @@ autocommands for the User event with the following patterns:
*CmpReady*
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*

Expand Down
9 changes: 9 additions & 0 deletions lua/cmp/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ end

---Unregister source
---@param source_id integer
---@return cmp.Source?
core.unregister_source = function(self, source_id)
local s = self.sources[source_id]
self.sources[source_id] = nil
return s
end

---Get new context
Expand Down Expand Up @@ -105,6 +108,12 @@ core.get_sources = function(self, filter)
return sources
end

---Return registered sources.
---@return cmp.Source[]
core.get_registered_sources = function(self)
return self.sources
end

---Keypress handler
core.on_keymap = function(self, keys, fallback)
local mode = api.get_mode()
Expand Down
22 changes: 21 additions & 1 deletion lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,33 @@ end
cmp.register_source = function(name, s)
local src = source.new(name, s)
cmp.core:register_source(src)
vim.api.nvim_exec_autocmds('User', {
pattern = 'CmpRegisterSource',
data = {
source_id = src.id
}
})
return src.id
end

---Unregister completion source
---@param id integer
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

---Get current configuration.
Expand Down

0 comments on commit 1d57252

Please sign in to comment.