@rafaelgss tech blog

Node.js Core - Neovim setup

Frequently, I’m asked about my setup when working on the Node.js codebase. To save everyone’s time, I wrote this quick blog post.

It’s important to mention all recommendations are personal preferences, you can achieve the same behaviour using different tools.

Currently, I’m a Linux Mint user, but likely this tutorial will work for any Unix-based system. These are the pre-requisites:

  1. Neovim + coc.nvim
  2. Node.js (git clone [email protected]:nodejs/node.git)
  3. ccls

In case you are looking for a Neovim configuration, you can use mine.

Enabling ccls LSP

Once you have coc.nvim installed, you should be able to run :CoCConfig to open the coc-settings.json.

This file handles all the CoC configurations. In this section, we’re configuring only the language server. For detailed information, check the coc documentation.

Include the following languageserver option to this file:

{
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "filetypes": [
        "c",
        "cpp",
        "objc",
        "objcpp",
        "cc"
      ],
      "rootPatterns": [
        "compile_commands.json",
        ".ccls",
        ".root",
        ".git/"
      ],
      "initializationOptions": {
        "cacheDirectory": "/tmp/ccls"
      }
    },
  }
}

This configuration will enable the ccls LSP(Language Service Provider) over Node.js C++ Files. Note that rootPatterns array contains compile_commands.json. This is the compilation database the that will be generated in the next step.

Generating Node.js Compilation Database

Building Node.js for the first time is not a fast operation. Personally, I suggest using:

To reduce the build time.

A compilation database is a JSON file named compile_commands.json that consists of an array of command objects where each object specifies a way in which a translation unit is compiled into the project. In other terms, The LSP needs a file that indicates all the includes and references each file has in order to provide actions such as go-to-definition, and go-to-implementation.

To generate this file you need to include the -C argument to the configure command.

$ ./configure -C
# you can pass any further flag
$ ./configure -C --debug-node

$ make -j6

It will generate a compile_commands.json under out/Release/ or out/Debug/ (depending on the compilation method). Create a symbolic link pointing the Node.js root folder

ln -s ./out/Release/compile_commands.json .

After that, restart your nvim and watch the magic happen.

The first CCLS Run may take a while

Don’t worry if your machine starts to work heavy on the ccls. Dashboards similar to the below image are expected:

htop showing ccls using all the CPU's

That’s just the price you pay for a simple go-to.

References