Skip to content

Commit

Permalink
feat: validate provider config when added at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 10, 2024
1 parent 299fc53 commit 3aaab03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lua/blink/cmp/config/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,26 @@ function sources.validate(config)
validate('sources.completion', {
enabled_providers = { config.completion.enabled_providers, { 'table', 'function' } },
})
for key, provider in pairs(config.providers) do
validate('sources.providers.' .. key, {
name = { provider.name, 'string' },
module = { provider.module, 'string' },
enabled = { provider.enabled, { 'boolean', 'function' }, true },
opts = { provider.opts, 'table', true },
transform_items = { provider.transform_items, 'function', true },
should_show_items = { provider.should_show_items, { 'boolean', 'function' }, true },
max_items = { provider.max_items, { 'number', 'function' }, true },
min_keyword_length = { provider.min_keyword_length, { 'number', 'function' }, true },
fallbacks = { provider.fallback_for, { 'table', 'function' }, true },
score_offset = { provider.score_offset, { 'number', 'function' }, true },
deduplicate = { provider.deduplicate, 'table', true },
override = { provider.override, 'table', true },
})
for id, provider in pairs(config.providers) do
sources.validate_provider(id, provider)
end
end

function sources.validate_provider(id, provider)
validate('sources.providers.' .. id, {
name = { provider.name, 'string' },
module = { provider.module, 'string' },
enabled = { provider.enabled, { 'boolean', 'function' }, true },
opts = { provider.opts, 'table', true },
transform_items = { provider.transform_items, 'function', true },
should_show_items = { provider.should_show_items, { 'boolean', 'function' }, true },
max_items = { provider.max_items, { 'number', 'function' }, true },
min_keyword_length = { provider.min_keyword_length, { 'number', 'function' }, true },
fallbacks = { provider.fallback_for, { 'table', 'function' }, true },
score_offset = { provider.score_offset, { 'number', 'function' }, true },
deduplicate = { provider.deduplicate, 'table', true },
override = { provider.override, 'table', true },
})
end

return sources
1 change: 1 addition & 0 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ end
function cmp.add_provider(id, provider_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
end

Expand Down

0 comments on commit 3aaab03

Please sign in to comment.