diff --git a/doc/cmp.txt b/doc/cmp.txt index 9df889078..416597e8a 100644 --- a/doc/cmp.txt +++ b/doc/cmp.txt @@ -540,6 +540,11 @@ sources[n].max_item_count~ `number` The source-specific item count. + *cmp-config.sources[n].enabled* +sources[n].enabled~ + `boolean | fun(ctx: cmp.Source.context): boolean` + Whether this source should be enabled. + *cmp-config.sources[n].group_index* sources[n].group_index~ `number` diff --git a/lua/cmp/source.lua b/lua/cmp/source.lua index 4d7ee940b..87654276c 100644 --- a/lua/cmp/source.lua +++ b/lua/cmp/source.lua @@ -80,6 +80,19 @@ source.get_fetching_time = function(self) return 100 * 1000 -- return pseudo time if source isn't fetching. end +---Return whether this source is enabled +---@return boolean +source.enabled = function(self) + local _e = self:get_source_config().enabled + if type(_e) == 'boolean' then + return _e + elseif type(_e) == 'function' then + return _e(self.context) + else + return true + end +end + ---Return filtered entries ---@param ctx cmp.Context ---@return cmp.Entry[] @@ -88,6 +101,10 @@ source.get_entries = function(self, ctx) return {} end + if not self:enabled() then + return {} + end + local target_entries = (function() local key = { 'get_entries', self.revision } for i = ctx.cursor.col, self.offset, -1 do diff --git a/lua/cmp/types/cmp.lua b/lua/cmp/types/cmp.lua index 5ff3bb610..bb7d0b230 100644 --- a/lua/cmp/types/cmp.lua +++ b/lua/cmp/types/cmp.lua @@ -148,6 +148,7 @@ cmp.ItemField = { ---@class cmp.SourceConfig ---@field public name string +---@field public enabled nil|boolean|function(ctx: cmp.Source.context): boolean ---@field public option table|nil ---@field public priority integer|nil ---@field public trigger_characters string[]|nil