From 1d572527440e6a9a5294055e37d288140271ac63 Mon Sep 17 00:00:00 2001 From: hrsh7th <629908+hrsh7th@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:04:15 +0900 Subject: [PATCH] add `cmp.get_registered_sources` and `CmpRegisterSource` /`CmpUnregisterSource` autocmd. --- doc/cmp.txt | 9 +++++++++ lua/cmp/core.lua | 9 +++++++++ lua/cmp/init.lua | 22 +++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/doc/cmp.txt b/doc/cmp.txt index 45ed3a1..e16bbd9 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -154,6 +154,9 @@ NOTE: `lua require('cmp').complete()` 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. @@ -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* diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index faa1fd3..f213014 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -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 @@ -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() diff --git a/lua/cmp/init.lua b/lua/cmp/init.lua index 6867679..7a1a7c8 100644 --- a/lua/cmp/init.lua +++ b/lua/cmp/init.lua @@ -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.