Skip to content

Commit

Permalink
feat: enabled function for sources
Browse files Browse the repository at this point in the history
closes #208
  • Loading branch information
Saghen committed Oct 29, 2024
1 parent b7d1233 commit c104663
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lua/blink/cmp/sources/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function sources.get_enabled_providers(context)
--- @type table<string, blink.cmp.SourceProvider>
local providers = {}
for key, provider in pairs(sources.providers) do
if provider.config.enabled(context) and vim.tbl_contains(mode_providers, key) then providers[key] = provider end
if provider:enabled(context) and vim.tbl_contains(mode_providers, key) then providers[key] = provider end
end
return providers
end
Expand Down Expand Up @@ -144,6 +144,7 @@ end
--- Resolve ---

function sources.resolve(item)
--- @type blink.cmp.SourceProvider?
local item_source = nil
for _, source in pairs(sources.providers) do
if source.id == item.source_id then
Expand Down
15 changes: 14 additions & 1 deletion lua/blink/cmp/sources/lib/provider/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
--- @field resolve_tasks table<blink.cmp.CompletionItem, blink.cmp.Task>
---
--- @field new fun(id: string, config: blink.cmp.SourceProviderConfig): blink.cmp.SourceProvider
--- @field enabled fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context): boolean
--- @field get_trigger_characters fun(self: blink.cmp.SourceProvider): string[]
--- @field get_completions fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[]): blink.cmp.Task
--- @field should_show_items fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[], response: blink.cmp.CompletionResponse): boolean
Expand Down Expand Up @@ -41,6 +42,15 @@ function source.new(id, config)
return self
end

function source:enabled(context)
-- user defined
if not self.config.enabled(context) then return false end

-- source defined
if self.module.enabled == nil then return true end
return self.module:enabled(context)
end

--- Completion ---

function source:get_trigger_characters()
Expand All @@ -58,7 +68,10 @@ function source:get_completions(context, enabled_sources)
end

return async.task
.new(function(resolve) return self.module:get_completions(context, resolve) end)
.new(function(resolve)
if self.module.get_completions == nil then return resolve() end
return self.module:get_completions(context, resolve)
end)
:map(function(response)
if response == nil then response = { is_incomplete_forward = true, is_incomplete_backward = true, items = {} } end
response.context = context
Expand Down
34 changes: 18 additions & 16 deletions lua/blink/cmp/sources/lib/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@

--- @class blink.cmp.Source
--- @field new fun(config: blink.cmp.SourceProviderConfig): blink.cmp.Source
--- @field get_trigger_characters (fun(self: blink.cmp.Source): string[]) | nil
--- @field get_completions fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)): (fun(): nil) | nil
--- @field filter_completions (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]) | nil
--- @field should_show_completions (fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean) | nil
--- @field resolve (fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)) | nil
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.Source): string[]
--- @field get_signature_help fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
--- @field reload (fun(self: blink.cmp.Source): nil) | nil
--- @field enabled? fun(self: blink.cmp.Source, context: blink.cmp.Context): boolean
--- @field get_trigger_characters? (fun(self: blink.cmp.Source): string[]) | nil
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)): (fun(): nil) | nil
--- @field filter_completions? (fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]) | nil
--- @field should_show_completions? (fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean) | nil
--- @field resolve? (fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)) | nil
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
--- @field reload? (fun(self: blink.cmp.Source): nil) | nil

--- @class blink.cmp.SourceOverride
--- @field get_trigger_characters fun(self: blink.cmp.Source): string[]
--- @field get_completions fun(self: blink.cmp.Source, context: blink.cmp.Context): blink.cmp.Task
--- @field filter_completions fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
--- @field should_show_completions fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
--- @field resolve fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): blink.cmp.Task
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.Source): string[]
--- @field get_signature_help fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
--- @field reload (fun(self: blink.cmp.Source): nil) | nil
--- @field enabled? fun(self: blink.cmp.Source, context: blink.cmp.Context): boolean
--- @field get_trigger_characters? fun(self: blink.cmp.Source): string[]
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context): blink.cmp.Task
--- @field filter_completions? fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): blink.cmp.Task
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
--- @field reload? (fun(self: blink.cmp.Source): nil) | nil

0 comments on commit c104663

Please sign in to comment.