Skip to content

Using coc extensions

Klaas Pieter Annema edited this page Jan 14, 2020 · 65 revisions

Why are coc extensions needed?

The main reason is that some language servers provided by the community behave badly compared to the extensions of VSCode. Coc extensions can be forked from VSCode extensions providing a better user experience.

Compared to configured language servers, extensions have more features.

  • Extensions can contribute properties to the schema coc-settings.json, like in VSCode you can write the configuration with completion and validation support when you have coc-json installed.

    Screen Shot 2019-06-26 at 3 22 05 PM
  • Extensions can contribute commands (like VSCode), you can use the coc commands in different ways:

    • Use the command :CocList commands to open the command list and choose one you need.

      screen shot 2018-09-07 at 4 53 12 pm
    • Use :CocCommand with <tab> for command line completion.

    • An example config to use the custom command Tsc for tsserver.watchBuild:

      command! -nargs=0 Tsc    :CocCommand tsserver.watchBuild
  • Extensions can contribute json schemas (same as VSCode)

  • Extensions can contribute snippets that can be loaded by coc-snippets extension.

  • Extensions can specify more client options, like fileEvents to watch files (requires watchman installed), and middleware which can be used to fix results that return from the language server.

Differences between coc extensions and VSCode extensions

  • Coc extensions use coc.nvim as a dependency instead of VSCode
  • Coc extensions support language server features by using the API from coc.nvim instead of vscode-languageclient which can only be used with VSCode.
  • Coc extensions support some features of VSCode extensions:
    • activate and deactivate api.
    • activationEvents in package.json.
    • Configuration support: contributes.configuration in package.json.
    • Commands support: contributes.commands.
    • JSON validation support via JSON Schema: contributes.jsonValidation.
    • Snippets support.

Manage coc extensions

Single file extensions

Coc.nvim will try to load javascript files in the folder $VIMCONFIG/coc-extensions, each javascript file should be extension of coc.nvim.

An example coc extension that convert character to unicode code point at cursor position.

const { commands, workspace } = require('coc.nvim')

exports.activate = context => {
  let { nvim } = workspace
  context.subscriptions.push(commands.registerCommand('code.convertCodePoint', async () => {
    let [pos, line] = await nvim.eval('[coc#util#cursor(), getline(".")]')
    let curr = pos[1] == 0 ? '' : line.slice(pos[1], pos[1] + 1)
    let code = curr.codePointAt(0)
    let str = code.toString(16)
    str = str.length == 4 ? str : '0'.repeat(4 - str.length) + str
    let result = `${line.slice(0, pos[1])}${'\\u' + str}${line.slice(pos[1] + 1)}`
    await nvim.call('setline', ['.', result])
  }))
}

Note: it's not possible to use package.json to add additional contributions for single file extensions.

Install extensions

Using :CocInstall:

:CocInstall coc-json coc-css

One or more extension names can be provided.

The extension name can also be the url of a git repository, like: https://github.com/andys8/vscode-jest-snippets which could be accepted by yarn install.

Extensions will be loaded and activated after the install succeeds.

Note you can add extension names to the g:coc_global_extensions variable, and coc will install the missing extensions for you on server start.

To install extensions with shell script, use command like:

# install coc-json & coc-html and exit
vim -c 'CocInstall -sync coc-json coc-html|q'

Use vim's plugin manager for coc extension

Starting from recent master of coc.nvim, you can manage coc extension by using a plugin manager for vim, like vim-plug, coc will try to load coc extensions from your &rtp

Example for coc-tsserver:

Plug 'neoclide/coc-tsserver', {'do': 'yarn install --frozen-lockfile'}

After adding this to your vimrc run PlugInstall. This has the limitation that you can't uninstall the extension by using :CocUninstall and that automatic update support is not available.

Update extensions

You don't need to update coc extensions manually, coc detects acceptable new version of installed extension everyday (by default) the first time it starts. When it finds a new version of an extension, it will update it for you automatically.

To disable automatic updates, change the setting: coc.preferences.extensionUpdateCheck to "never".

Use the command :CocUpdate or :CocUpdateSync to update all extensions to the latest version.

To upgrade extensions with shell script, use command like:

vim -c 'CocUpdateSync|q'

Uninstall coc extension

:CocUninstall coc-css

Manage extensions with CocList

Run command:

CocList extensions

to open CocList buffer, which looks like:

screen shot 2018-09-10 at 10 28 06 pm
  • ? means invalid extension
  • * means extension is activated
  • + means extension is loaded
  • - means extension is disabled

Supported actions (hit tab to activate action menu):

  • toggle default action. activate/deactivate selected extension(s).
  • enable: enable selected extension(s).
  • disable: disable selected extension(s).
  • reload: reload selected extension(s).
  • uninstall: remove selected extension(s).
  • lock: toggle lock of extension, locked extension won't be updated by :CocUpdate

Debug coc extension

If an extension throws uncaught errors, you can get the error message by: :messages.

For extensions using a language server, you can use the output channel. Check out https://github.com/neoclide/coc.nvim/wiki/Debug-language-server#using-output-channel.

If the extension is using stdio to write messages, you can get the output from the log file of coc, the log file can be found by command: :CocOpenLog in your vim. Use $NVIM_COC_LOG_FILE environment variable to use fixed filename for the log file.

The default log level is info. To get the debug information, set the NVIM_COC_LOG_LEVEL environment variable by the command: export NVIM_COC_LOG_LEVEL=debug.

You can also use Chrome to debug extensions, checkout https://github.com/neoclide/coc.nvim/wiki/Debug-coc.nvim.

Implemented coc extensions

You can find available coc extensions by searching coc.nvim on npm, or use coc-marketplace, which can search and install extensions in coc.nvim directly.

Tips: use :CocConfig to edit the configuration file. Completion & validation are supported after coc-json is installed.

You can’t perform that action at this time.