Ruby IntelliSense on VS Code with Solargraph

Diego Leite
4 min readNov 3, 2021

In this article, I will explore some features of Solargraph on VS Code and how to configure it in your development environment.

Before we start I will explain some features of Solargraph, and after following these steps below, I will show how to configure it on VS Code.

What is Solargraph?

Solargraph is a language server that provides IntelliSense, code completion, and inline documentation for Ruby.

What are the benefits of using Solargraph?

If you worked with any IDE you’ve noted that the behavior when you are coding on them instead of coding on a generic notepad, features like code completion, type checking, and code navigation makes our lives easier. Solargraph goes through this way and tries brought to an IDE-like experience to VS Code.

Solargraph in action on VS Code

Who knows me know that I like to code in TypeScript, and the way TypeScript IntelliSense works on VS Code, then I searched for a tool that has the same behavior in VS Code in Ruby projects, amid the searching I’ve found Solargraph gem.

Let’s get started!

First of all, make sure if you have the Solargraph gem has been installed, or if you didn’t it follow the steps below.

Install the gem from the command line:

$ gem install solargraph

Or add it to your Gemfile:

gem 'solargraph', group: :development

In the next step, you will install the Solargraph plugin on your VSCode, install it using the link below.

Make sure if you have followed all of the last steps to start to configure the solargraph on your project.

Inside of your VSCode settings.json file, I recommend that you insert these lines below:

// settings.json
{
"solargraph.transport": "stdio",
"solargraph.formatting": true,
"solargraph.diagnostics": true
}

Run the command below to generate the .solargraph.yml file on the root of your project.

$ solargraph config

Ok, now you have the .solargraph.yml file on the root of your project with this content below.

# .solargraph.yml---
include:
- "**/*.rb"
exclude:
- spec/**/*
- test/**/*
- vendor/**/*
- ".bundle/**/*"
require: []
domains: []
reporters:
- rubocop
- require_not_found
formatter:
rubocop:
cops: safe
except: []
only: []
extra_args: []
require_paths: []
plugins: []
max_files: 5000

include

A list of directory globs to include in Solargraph’s code maps. The default configuration includes all .rb files in the folder.

exclude

A list of directory globs to exclude from Solargraph’s code maps. The default configuration excludes the spec, test, and vendor folders.

require

Note: Consider adding requires with a YARD @!parse directive instead of using this configuration setting.

Use require to add required paths that are not explicitly defined in code.

Example:

require:
- sinatra/base

Solargraph will act as if a file in the project contained the line require 'sinatra/base'.

domains

Solargraph will use the designated classes and modules as contexts in which the project’s scripts will run. For example: if one of the domains is Class<Sinatra::Base>, the Sinatra DSL will be included in suggestions. (Whether you need to specify the domain inside Class<> depends on how the library is implemented.)

reporters

Solargraph provides diagnostics reporters to send diagnostics to the client. The reporters section selects
which reporters the language server should use:

reporters:
- rubocop
- require_not_found

rubocop enables RuboCop linting. Its rules can be configured in a .rubocop.yml file.

require_not_found highlights require calls where Solargraph could not resolve a required path. Note that this error does not necessarily mean that the path is incorrect; only that Solargraph was unable to recognize it.

typecheck will report problems with type tags. See Type Checking for more information.

You can use all! to tell the language server to use all available reporters.

Run solargraph reporters for a complete list of available reporters and their descriptions.

I recommend that you use those reporters on your solargraph config file:

# .solargraph.yml...
reporters:
- rubocop
- require_not_found
- typecheck:strict
...

I’ve created a class using YARD for this example to show you the solargraph type checking in use on VS Code, you can see the code below.

Hands-on

Here below we can see how the type checking works when we instantiate the Car class.

If you want to see more or try to test it, I’ve created a GitHub repository with these Solargraph configurations.

diegomelo182/solargraph-example (github.com)

That’s all folks!!!

My name is Diego Melo, I’m a developer, Brazilian and I hope this content was helpful for you.

--

--