Skip to content

Commit

Permalink
feat: add add_filetype_source API
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Jan 17, 2025
1 parent cee556e commit 40d0ce0
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,30 @@ function cmp.get_lsp_capabilities(override, include_nvim_defaults)
end

--- Add a new source provider at runtime
--- @param id string
--- @param provider_config blink.cmp.SourceProviderConfig
function cmp.add_provider(id, provider_config)
--- @param source_id string
--- @param source_config blink.cmp.SourceProviderConfig
function cmp.add_provider(source_id, source_config)
local config = require('blink.cmp.config')
assert(config.sources.providers[id] == nil, 'Provider with id ' .. id .. ' already exists')
require('blink.cmp.config.sources').validate_provider(id, provider_config)
config.sources.providers[id] = provider_config
assert(config.sources.providers[source_id] == nil, 'Provider with id ' .. source_id .. ' already exists')
require('blink.cmp.config.sources').validate_provider(source_id, source_config)
config.sources.providers[source_id] = source_config
end

--- Adds a source to the list of enable sources for a given filetype
--- @param filetype string
--- @param source string
function cmp.add_filetype_source(filetype, source)
local config = require('blink.cmp.config')

assert(
type(config.sources.per_filetype[filetype]) ~= 'function',
'Sources for filetype "' .. filetype .. '" has been set to a function, so we cannot add a source to it'
)
if config.sources.per_filetype[filetype] == nil then config.sources.per_filetype[filetype] = {} end

local sources = config.sources.per_filetype[filetype]
--- @cast sources string[]
table.insert(sources, source)
end

return cmp

3 comments on commit 40d0ce0

@xdevfaheem
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you give a short description or doc to better understand this? did we get to choose set of source based on the file type? if so, didn't we already could do it with dynamic default source providers?

@Saghen
Copy link
Owner Author

@Saghen Saghen commented on 40d0ce0 Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for plugins like codecompanion to set their own sources up automatically. I'll include docs later

@xdevfaheem
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it thanks!

Please sign in to comment.