Skip to content

Commit

Permalink
feat: support triggering with list of providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 9, 2024
1 parent 67ec497 commit 2cf0744
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
13 changes: 11 additions & 2 deletions lua/blink/cmp/completion/trigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
--- @field line string
--- @field bounds blink.cmp.ContextBounds
--- @field trigger { kind: number, character: string | nil }
--- @field enabled_providers string[]

--- @class blink.cmp.CompletionTrigger
--- @field buffer_events blink.cmp.BufferEvents
Expand All @@ -28,7 +29,7 @@
--- @field is_trigger_character fun(char: string, is_retrigger?: boolean): boolean
--- @field suppress_events_for_callback fun(cb: fun())
--- @field show_if_on_trigger_character fun(opts?: { is_accept?: boolean })
--- @field show fun(opts?: { trigger_character?: string, force?: boolean, send_upstream?: boolean })
--- @field show fun(opts?: { trigger_character?: string, force?: boolean, send_upstream?: boolean, enabled_providers?: string[] })
--- @field hide fun()
--- @field within_query_bounds fun(cursor: number[]): boolean
--- @field get_context_bounds fun(regex: string): blink.cmp.ContextBounds
Expand Down Expand Up @@ -165,7 +166,14 @@ function trigger.show(opts)
end

-- update context
if trigger.context == nil then trigger.current_context_id = trigger.current_context_id + 1 end
if trigger.context == nil or opts.enabled_providers ~= nil then
trigger.current_context_id = trigger.current_context_id + 1
end

local enabled_providers = opts.enabled_providers
or (trigger.context and trigger.context.enabled_providers)
or require('blink.cmp.sources.lib').get_enabled_provider_ids()

trigger.context = {
id = trigger.current_context_id,
bufnr = vim.api.nvim_get_current_buf(),
Expand All @@ -177,6 +185,7 @@ function trigger.show(opts)
or vim.lsp.protocol.CompletionTriggerKind.Invoked,
character = opts.trigger_character,
},
enabled_providers = enabled_providers,
}

if opts.send_upstream ~= false then trigger.show_emitter:emit({ context = trigger.context }) end
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/config/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
--- end
--- end
--- ```
--- @field enabled_providers string[] | fun(ctx?: blink.cmp.Context): string[]
--- @field enabled_providers string[] | fun(): string[]

--- @class blink.cmp.SourceProviderConfig
--- @field name? string
Expand Down
9 changes: 6 additions & 3 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ end

------- Public API -------

function cmp.show()
if require('blink.cmp.completion.windows.menu').win:is_open() then return end
--- @params opts? { enabled_providers?: string[] }
function cmp.show(opts)
if require('blink.cmp.completion.windows.menu').win:is_open() and not (opts and opts.enabled_providers) then
return
end

vim.schedule(function()
require('blink.cmp.completion.windows.menu').auto_show = true
require('blink.cmp.completion.trigger').show({ force = true })
require('blink.cmp.completion.trigger').show({ force = true, enabled_providers = opts and opts.enabled_providers })
end)
return true
end
Expand Down
4 changes: 1 addition & 3 deletions lua/blink/cmp/sources/lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ end

--- Limits the number of items per source as configured
function sources.apply_max_items_for_completions(context, items)
local enabled_sources = sources.get_enabled_providers(context)

-- get the configured max items for each source
local total_items_for_sources = {}
local max_items_for_sources = {}
for id, source in pairs(sources.providers) do
max_items_for_sources[id] = source.config.max_items(context, enabled_sources, items)
max_items_for_sources[id] = source.config.max_items(context, items)
total_items_for_sources[id] = 0
end

Expand Down
3 changes: 1 addition & 2 deletions lua/blink/cmp/sources/lib/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ local tree = {}
--- @param all_sources blink.cmp.SourceProvider[]
function tree.new(context, all_sources)
-- only include enabled sources for the given context
local global_source_ids = require('blink.cmp.sources.lib').get_enabled_provider_ids(context)
local sources = vim.tbl_filter(
function(source) return vim.tbl_contains(global_source_ids, source.id) and source:enabled(context) end,
function(source) return vim.tbl_contains(context.enabled_providers, source.id) and source:enabled(context) end,
all_sources
)
local source_ids = vim.tbl_map(function(source) return source.id end, sources)
Expand Down

0 comments on commit 2cf0744

Please sign in to comment.