This file contains community driven instructions on how to set up the Ruby LSP in editors other than VS Code. For VS Code, use the official Ruby LSP extension.
Note
Some Ruby LSP features may be unavailable or limited due to incomplete implementations of the Language Server Protocol, such as dynamic feature registration, or file watching.
If you need to select particular features to enable or disable, see
vscode/package.json
for the full list.
IMPORTANT NOTE FOR ALL EDITORS
The command to launch the language server might depend on which editor and version manager combination you are using. In order to work properly, the Ruby LSP must be launched with the Ruby version being used by the project you are working on and with the correct Bundler environment set.
If you normally launch your editor from the terminal in a shell session where the Ruby environment is already activated,
then you can probably just use ruby-lsp
as the command.
If you're seeing issues related to not finding the right gems or not being able to locate the ruby-lsp
executable,
then you may need to ensure that the environment is properly configured by the version manager before you try to run the
ruby-lsp
executable. How to do this will depend on which version manager you use. Here are some examples:
If your version manager exposes a command to run an executable within the context of the current Ruby, use that:
mise x -- ruby-lsp
shadowenv exec -- ruby-lsp
If your version manager creates gem executable shims that perform the automatic version switching, then use those:
~/.rbenv/shims/ruby-lsp
~/.asdf/shims/ruby-lsp
If your version manager doesn't provide either of those, then activate the environment and run the executable:
chruby $(cat .ruby-version) && ruby-lsp
These strategies will ensure that the ruby-lsp
executable is invoked with the correct Ruby version, GEM_HOME
and
GEM_PATH
, which are necessary for proper integration with your project.
Eglot runs solargraph server by default. To set it up with ruby-lsp you need to put that in your init file:
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs '((ruby-mode ruby-ts-mode) "ruby-lsp")))
When you run eglot
command it will run ruby-lsp
process for you.
Note: Ensure that you are using Neovim 0.10 or newer.
The nvim-lspconfig plugin has support for Ruby LSP.
You can use mason.nvim, along with mason-lspconfig.nvim:
local capabilities = vim.lsp.protocol.make_client_capabilities()
local mason_lspconfig = require("mason-lspconfig")
local servers = {
ruby_lsp = {},
}
mason_lspconfig.setup {
ensure_installed = vim.tbl_keys(servers),
}
mason_lspconfig.setup_handlers {
function(server_name)
require("lspconfig")[server_name].setup {
capabilities = capabilities,
on_attach = on_attach,
settings = servers[server_name],
filetypes = (servers[server_name] or {}).filetypes,
}
end
}
rubyLsp/workspace/dependencies
is a custom method currently supported only in the VS Code plugin.
The following snippet adds ShowRubyDeps
command to show dependencies in the quickfix list.
local function add_ruby_deps_command(client, bufnr)
vim.api.nvim_buf_create_user_command(bufnr, "ShowRubyDeps", function(opts)
local params = vim.lsp.util.make_text_document_params()
local showAll = opts.args == "all"
client.request("rubyLsp/workspace/dependencies", params, function(error, result)
if error then
print("Error showing deps: " .. error)
return
end
local qf_list = {}
for _, item in ipairs(result) do
if showAll or item.dependency then
table.insert(qf_list, {
text = string.format("%s (%s) - %s", item.name, item.version, item.dependency),
filename = item.path
})
end
end
vim.fn.setqflist(qf_list)
vim.cmd('copen')
end, bufnr)
end,
{nargs = "?", complete = function() return {"all"} end})
end
require("lspconfig").ruby_lsp.setup({
on_attach = function(client, buffer)
add_ruby_deps_command(client, buffer)
end,
})
As of v12.33.0, Ruby LSP is the default LSP for Ruby and no configuration should be needed, other than ensuring your Ruby version manager is set up as described above.
To configure the Ruby LSP using LSP for Sublime Text, add the following configuration to your LSP client configuration:
"clients": {
"ruby-lsp": {
"enabled": true,
"command": [
"ruby-lsp"
],
"selector": "source.ruby",
"initializationOptions": {
"enabledFeatures": {
"diagnostics": false
},
"experimentalFeaturesEnabled": true
}
}
}
Restart LSP or Sublime Text and ruby-lsp
will automatically activate when opening ruby files.
Zed has added support for Ruby LSP as a alternative language server in version v0.0.2 of the Ruby extension.
See zed-industries/zed#4834 for discussion of the limitations.
You can use the Ruby LSP with RubyMine (or IntelliJ IDEA Ultimate) through the following plugin.
Note that there might be overlapping functionality when using it with RubyMine, given that the IDE provides similar features as the ones coming from the Ruby LSP.
To configure indexing, pass a JSON hash as part of the initialization options for your editor, for example:
{
"indexing": {
"excludedPatterns": ["**/test/**.rb"],
"includedPatterns": ["**/bin/**"],
"excludedGems": ["rubocop", "rubocop-performance"],
"includedPatterns": ["rake"],
"excludedMagicComments": ["compiled:true"]
}
}