LSP (Language Server Protocol)

The built-in language server was introduced in RuboCop 1.53. This experimental feature has been under consideration for a while.

The Language Server Protocol is the modern standard for providing cross-editor support for various programming languages.

This feature enables extremely fast interactions through the LSP.

Offense detection and autocorrection are performed in real-time by editors and IDEs using the language server. The Server Mode is primarily used to speed up RuboCop runs in the terminal. Therefore, if you want real-time feedback from RuboCop in your editor or IDE, opting to use this language server instead of the server mode will not only provide a fast and efficient solution, but also offer a straightforward setup for integration.

Examples of LSP Client

Here are examples of LSP client configurations.

VS Code

vscode-rubocop integrates RuboCop into VS Code.

You can install this VS Code extension from the Visual Studio Marketplace.

Emacs (Eglot)

Eglot is a client for Language Server Protocol servers on Emacs.

Add the following to your Emacs configuration file (e.g. ~/.emacs.d/init.el):

(require 'eglot)

(add-to-list 'eglot-server-programs '(ruby-mode . ("bundle" "exec" "rubocop" "--lsp")))
(add-hook 'ruby-mode-hook 'eglot-ensure)

Below is an example of additional setting for autocorrecting on save:

(add-hook 'ruby-mode-hook (lambda () (add-hook 'before-save-hook 'eglot-format-buffer nil 'local)))

If you run into problems, first use "M-x eglot-reconnect" to reconnect to the language server.

See Eglot’s official documentation for more information.

Vim and Neovim (coc.nvim)

coc.nvim is an extension host for Vim and Neovim, powered by Node.js. It allows the loading of extensions similar to VSCode and provides hosting for language servers.

Add the following to your coc.nvim configuration file. For example, in Vim, it would be ~/.vim/coc-settings.json, and in Neovim, it would be ~/.config/nvim/coc-settings.json:

{
  "languageserver": {
    "rubocop": {
      "command": "bundle",
      "args" : ["exec", "rubocop", "--lsp"],
      "filetypes": ["ruby"],
      "rootPatterns": [".git", "Gemfile"],
      "requireRootPattern": true
    }
  }
}

Below is an example of additional setting for autocorrecting on save:

{
  "coc.preferences.formatOnSave": true
}

See coc.nvim’s official documentation for more information.

Neovim (nvim-lspconfig)

nvim-lspconfig provides quickstart configs for Neovim’s LSP.

Add the following to your nvim-lspconfig configuration file (e.g. ~/.config/nvim/init.lua):

vim.opt.signcolumn = "yes"
vim.api.nvim_create_autocmd("FileType", {
  pattern = "ruby",
  callback = function()
    vim.lsp.start {
      name = "rubocop",
      cmd = { "bundle", "exec", "rubocop", "--lsp" },
    }
  end,
})

Below is an example of additional setting for autocorrecting on save:

vim.api.nvim_create_autocmd("BufWritePre", {
  pattern = "ruby",
  callback = function()
    vim.lsp.buf.format()
  end,
})

See nvim-lspconfig’s official documentation for more information.

Autocorrection

The language server supports textDocument/formatting method and is autocorrectable. The autocorrection is safe by default (rubocop -a).

LSP client can switch to unsafe autocorrection (rubocop -A) by passing the following safeAutocorrect parameter in the initialize request.

{
  "jsonrpc": "2.0",
  "id": 42,
  "method": "initialize",
  "params": {
    "initializationOptions": {
      "safeAutocorrect": false
    }
  }
}

For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

The safeAutocorrect parameter was introduced in RuboCop 1.54.

Lint Mode

LSP client can run lint cops by passing the following lintMode parameter in the initialize request if you only want to enable the feature as a linter like ruby -w:

{
  "jsonrpc": "2.0",
  "id": 42,
  "method": "initialize",
  "params": {
    "initializationOptions": {
      "lintMode": true
    }
  }
}

Furthermore, enabling autocorrect in a LSP client at the time of saving equates to the effect of rubocop -l option.

For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

The lintMode parameter was introduced in RuboCop 1.55.

Layout Mode

LSP client can run layout cops by passing the following layoutMode parameter in the initialize request if you only want to enable the feature as a formatter:

{
  "jsonrpc": "2.0",
  "id": 42,
  "method": "initialize",
  "params": {
    "initializationOptions": {
      "layoutMode": true
    }
  }
}

Furthermore, enabling autocorrect in a LSP client at the time of saving equates to the effect of rubocop -x option.

For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

The layoutMode parameter was introduced in RuboCop 1.55.

Run as a Language Server

Run rubocop --lsp command from LSP client.

When the language server is started, the command displays the language server’s PID:

$ ps aux | grep rubocop
user             17414   0.0  0.2  5557716 144376   ??  Ss    4:48PM   0:02.13 /Users/user/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bin/rubocop --lsp
rubocop --lsp is for starting LSP client, so users don’t manually execute it.