Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding providers add-hoc after blink.cmp has been setup #475

Closed
abeldekat opened this issue Dec 9, 2024 · 4 comments · Fixed by #465
Closed

Allow adding providers add-hoc after blink.cmp has been setup #475

abeldekat opened this issue Dec 9, 2024 · 4 comments · Fixed by #465
Labels
feature New feature or request sources Specific source provider or the system as a whole
Milestone

Comments

@abeldekat
Copy link
Contributor

Feature Description

Thanks for the plugin!

My config uses mini.deps and has support for sql, using vim-dadbod-completion.

However, I don't need it that often. As such, I only load sql plugins on a key command that is available when the filetype of the buffer is sql.

Using nvim-cmp, I could add a new source dynamically:

local has_nvim_cmp, cmp = pcall(require, "cmp")
if has_nvim_cmp then cmp.setup.buffer({ sources = { { name = "vim-dadbod-completion" } } }) 

I did not discover such an option in blink.cmp, but it is possible:

    --
    -- HACK: There is no public method to add a provider once blink has been setup:
    --
    local new_provider_in_config = { name = "Dadbod", module = "vim_dadbod_completion.blink" }
    local new_provider_id = "dadbod"

    local config = require("blink.cmp.config")
    -- Add to config, otherwise internal checks fail:
    -- HACK: With this hack, do not configure enabled_providers as function in blink setup!
    ---@diagnostic disable-next-line: param-type-mismatch -- enabled_providers, function or string...
    table.insert(config.sources.completion.enabled_providers, new_provider_id)
    -- HACK: Adding to config directly bypasses validation for new_provider_in_config!
    config.sources.providers[new_provider_id] = new_provider_in_config
    -- Add to internal providers:
    local sources = require("blink.cmp.sources.lib")
    local provider = require("blink.cmp.sources.lib.provider")
    -- Line taken from blink.cmp.sources.lib.init, get_enabled_providers:
    -- ...initialize the provider if it hasn't been initialized yet...
    sources.providers[new_provider_id] = provider.new(new_provider_id, new_provider_in_config)

    -- Now the public function can be used. Only dadbod will be relaoded
    require("blink-cmp").reload(new_provider_id)

The difference in loading between nvim-cmp and blink.cmp is currently also observable in the sql extra in LazyVim. Without the blink.cmp extra, vim-dadbod-completion is only added when the type of the buffer is sql. When the blink.cmp extra is activated, vim-dadbod-completion is always added when blink.cmp activates.

Would you be willing to add a public function to blink.cmp that registers and activates a new provider? If so, I would gladly submit a PR. I think the code above would be of use in the implementation.

Related:

@abeldekat abeldekat added the feature New feature or request label Dec 9, 2024
@Saghen Saghen added this to the Sources v2 milestone Dec 9, 2024
@Saghen Saghen added the sources Specific source provider or the system as a whole label Dec 9, 2024
@Saghen
Copy link
Owner

Saghen commented Dec 9, 2024

Yeah, makes sense, I'll add it as part of sources v2

@Saghen Saghen mentioned this issue Dec 9, 2024
9 tasks
@abeldekat
Copy link
Contributor Author

Thanks!

@Saghen Saghen closed this as completed in 7ff28da Dec 10, 2024
Saghen added a commit that referenced this issue Dec 10, 2024
Large rewrite of how sources are handled, adding support for async providers/timeouts, tree based fallbacks, dynamically adding sources and some other goodies

Closes #386
Closes #219
Closes #328
Closes #331
Closes #312
Closes #454
Closes #444
Closes #372
Closes #475
Saghen added a commit that referenced this issue Dec 11, 2024
Large rewrite of how sources are handled, adding support for async providers/timeouts, tree based fallbacks, dynamically adding sources and some other goodies

Closes #386
Closes #219
Closes #328
Closes #331
Closes #312
Closes #454
Closes #444
Closes #372
Closes #475
@Saghen
Copy link
Owner

Saghen commented Dec 11, 2024

You should be able to use require('blink.cmp').add_provider(id, config) to add providers at runtime now. There's also a new sources.per_filetype that could be useful for enabling just dadbod in sql. Lmk if you run into any issues

@abeldekat
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request sources Specific source provider or the system as a whole
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants