Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enabled for sources #1314

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/cmp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
17 changes: 17 additions & 0 deletions lua/cmp/source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/cmp/types/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down