neoclide / coc.nvim

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
Other
24.16k stars 954 forks source link

Activate CoC extension based on current file type #5072

Closed jdandrea closed 1 week ago

jdandrea commented 1 week ago

The Problem

There are some CoC extensions I want to be active only for specific file types.

A Possible Solution

Perhaps allow for configuration within coc-settings.json. (Unless this sort of thing already exists and I'm just missing it.)

For example, to set coc-vale as active only when editing AsciiDoc or Markdown files:

{
  "extensions": {
    "coc-vale": {
      "filetypes": ["adoc", "md"]
    }
  }
}

Alternatives

Here's another way to do it, within Neovim rc:

augroup toggleCocExtensions
    autocmd!
    autocmd BufEnter * call CocActionAsync('deactivateExtension', 'coc-vale') 
    autocmd BufEnter *.adoc call CocActionAsync('activeExtension', 'coc-vale') 
    autocmd BufLeave *.adoc call CocActionAsync('deactivateExtension', 'coc-vale') 
augroup END

... except this is more bespoke and can lead to errors within Neovim like [coc.nvim]: Error on notification "deactivateExtension": TypeError: Cannot read properties of undefined (reading 'dispose')

fannheyward commented 1 week ago

coc.nvim already supported to active extensions on filetype with activationEvents, for example coc-json will be only activated on json and jsonc files.

https://github.com/neoclide/coc-json/blob/06ea3ace3d0c0a8deaa70942d63345718f029612/package.json#L17-L21

The coc-vale extension declares activationEvents to *, this makes coc.nvim activate it on every filetypes.

https://github.com/szymonkaliski/coc-vale/blob/741c5eb4da09b7dd1f1a4cab8a9326e4becb92d7/package.json#L43-L45