Skip to content

Commit c104663

Browse files
committed
feat: enabled function for sources
closes #208
1 parent b7d1233 commit c104663

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

lua/blink/cmp/sources/lib/init.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function sources.get_enabled_providers(context)
4949
--- @type table<string, blink.cmp.SourceProvider>
5050
local providers = {}
5151
for key, provider in pairs(sources.providers) do
52-
if provider.config.enabled(context) and vim.tbl_contains(mode_providers, key) then providers[key] = provider end
52+
if provider:enabled(context) and vim.tbl_contains(mode_providers, key) then providers[key] = provider end
5353
end
5454
return providers
5555
end
@@ -144,6 +144,7 @@ end
144144
--- Resolve ---
145145

146146
function sources.resolve(item)
147+
--- @type blink.cmp.SourceProvider?
147148
local item_source = nil
148149
for _, source in pairs(sources.providers) do
149150
if source.id == item.source_id then

lua/blink/cmp/sources/lib/provider/init.lua

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
--- @field resolve_tasks table<blink.cmp.CompletionItem, blink.cmp.Task>
99
---
1010
--- @field new fun(id: string, config: blink.cmp.SourceProviderConfig): blink.cmp.SourceProvider
11+
--- @field enabled fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context): boolean
1112
--- @field get_trigger_characters fun(self: blink.cmp.SourceProvider): string[]
1213
--- @field get_completions fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[]): blink.cmp.Task
1314
--- @field should_show_items fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[], response: blink.cmp.CompletionResponse): boolean
@@ -41,6 +42,15 @@ function source.new(id, config)
4142
return self
4243
end
4344

45+
function source:enabled(context)
46+
-- user defined
47+
if not self.config.enabled(context) then return false end
48+
49+
-- source defined
50+
if self.module.enabled == nil then return true end
51+
return self.module:enabled(context)
52+
end
53+
4454
--- Completion ---
4555

4656
function source:get_trigger_characters()
@@ -58,7 +68,10 @@ function source:get_completions(context, enabled_sources)
5868
end
5969

6070
return async.task
61-
.new(function(resolve) return self.module:get_completions(context, resolve) end)
71+
.new(function(resolve)
72+
if self.module.get_completions == nil then return resolve() end
73+
return self.module:get_completions(context, resolve)
74+
end)
6275
:map(function(response)
6376
if response == nil then response = { is_incomplete_forward = true, is_incomplete_backward = true, items = {} } end
6477
response.context = context

lua/blink/cmp/sources/lib/types.lua

+18-16
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,23 @@
1010

1111
--- @class blink.cmp.Source
1212
--- @field new fun(config: blink.cmp.SourceProviderConfig): blink.cmp.Source
13-
--- @field get_trigger_characters (fun(self: blink.cmp.Source): string[]) | nil
14-
--- @field get_completions fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)): (fun(): nil) | nil
15-
--- @field filter_completions (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]) | nil
16-
--- @field should_show_completions (fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean) | nil
17-
--- @field resolve (fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)) | nil
18-
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.Source): string[]
19-
--- @field get_signature_help fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
20-
--- @field reload (fun(self: blink.cmp.Source): nil) | nil
13+
--- @field enabled? fun(self: blink.cmp.Source, context: blink.cmp.Context): boolean
14+
--- @field get_trigger_characters? (fun(self: blink.cmp.Source): string[]) | nil
15+
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)): (fun(): nil) | nil
16+
--- @field filter_completions? (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]) | nil
17+
--- @field should_show_completions? (fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean) | nil
18+
--- @field resolve? (fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)) | nil
19+
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
20+
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
21+
--- @field reload? (fun(self: blink.cmp.Source): nil) | nil
2122

2223
--- @class blink.cmp.SourceOverride
23-
--- @field get_trigger_characters fun(self: blink.cmp.Source): string[]
24-
--- @field get_completions fun(self: blink.cmp.Source, context: blink.cmp.Context): blink.cmp.Task
25-
--- @field filter_completions fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
26-
--- @field should_show_completions fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
27-
--- @field resolve fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): blink.cmp.Task
28-
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.Source): string[]
29-
--- @field get_signature_help fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
30-
--- @field reload (fun(self: blink.cmp.Source): nil) | nil
24+
--- @field enabled? fun(self: blink.cmp.Source, context: blink.cmp.Context): boolean
25+
--- @field get_trigger_characters? fun(self: blink.cmp.Source): string[]
26+
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context): blink.cmp.Task
27+
--- @field filter_completions? fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
28+
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
29+
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): blink.cmp.Task
30+
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
31+
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
32+
--- @field reload? (fun(self: blink.cmp.Source): nil) | nil

0 commit comments

Comments
 (0)