hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
8.14k stars 405 forks source link

Don't rely on `after/plugin` for registering sources #2021

Open mrcjkb opened 3 months ago

mrcjkb commented 3 months ago

FAQ

Announcement

Minimal reproducible full config

Description

Most completion sources rely on after/plugin to register themselves, and the impact on startup time can be quite heavy (especially when a lot of sources are installed). For this reason, it can be desirable to install the plugins as opt plugins and lazy-load them (e.g. on an InsertEnter event).

But nvim-cmp sources rely on after/plugin to register themselves (for lack of an API). Because Neovim doesn't source after scripts once the startup initialisation has completed, the sources won't register themselves and nvim-cmp will error when trying to pass them in via the setup function.

Steps to reproduce

Expected behavior

No error. nvim-cmp and the LSP source configured and loaded on InsertEnter

Actual behavior

nvim-cmp errors when calling setup, due to the missing source.

Additional context

A potential solution would be to execute a :h User autocommand with the pattern CmpRegisterSourcePre at the start of the setup call, or in the main module that loads the setup function. Sources could create an autocommand with that pattern to ensure they are registered at the appropriate time.

mrcjkb commented 3 months ago

If you're open to the proposed solution, I'd be happy to contribute :smile:

iguanacucumber commented 1 month ago

hi i maintain a fork of nvim-cmp, magazine.nvim, and this looks like something interesting to implement

hrsh7th commented 2 weeks ago

Is there a way to detect when something has been added to the runtimepath, rather than using packadd?

I'd like to be able to detect when something has been added to the runtimepath as well.

Shougo commented 2 weeks ago

nvim-cmp can cache runtimepath and check it when on completion events.

Or you can use OptionSet autocmd.