1# Ruby
2
3Ruby support is available through the [Ruby extension](https://github.com/zed-extensions/ruby).
4
5- Tree-sitters:
6 - [tree-sitter-ruby](https://github.com/tree-sitter/tree-sitter-ruby)
7 - [tree-sitter-embedded-template](https://github.com/tree-sitter/tree-sitter-embedded-template)
8- Language Servers:
9 - [ruby-lsp](https://github.com/Shopify/ruby-lsp)
10 - [solargraph](https://github.com/castwide/solargraph)
11 - [rubocop](https://github.com/rubocop/rubocop)
12 - [Herb](https://herb-tools.dev)
13- Debug Adapter: [`rdbg`](https://github.com/ruby/debug)
14
15The Ruby extension also provides support for ERB files.
16
17## Language Servers
18
19There are multiple language servers available for Ruby. Zed supports the two following:
20
21- [solargraph](https://github.com/castwide/solargraph)
22- [ruby-lsp](https://github.com/Shopify/ruby-lsp)
23
24They both have an overlapping feature set of autocomplete, diagnostics, code actions, etc. and it's up to you to decide which one you want to use. Note that you can't use both at the same time.
25
26In addition to these two language servers, Zed also supports:
27
28- [rubocop](https://github.com/rubocop/rubocop) which is a static code analyzer and linter for Ruby. Under the hood, it's also used by Zed as a language server, but its functionality is complimentary to that of solargraph and ruby-lsp.
29- [sorbet](https://sorbet.org/) which is a static type checker for Ruby with a custom gradual type system.
30- [steep](https://github.com/soutaro/steep) which is a static type checker for Ruby that leverages Ruby Signature (RBS).
31- [Herb](https://herb-tools.dev) which is a language server for ERB files.
32
33When configuring a language server, it helps to open the LSP Logs window using the 'dev: Open Language Server Logs' command. You can then choose the corresponding language instance to see any logged information.
34
35## Configuring a language server
36
37The [Ruby extension](https://github.com/zed-extensions/ruby) offers both `solargraph` and `ruby-lsp` language server support.
38
39### Language Server Activation
40
41For all supported Ruby language servers (`solargraph`, `ruby-lsp`, `rubocop`, `sorbet`, and `steep`), the Ruby extension follows this activation sequence:
42
431. If the language server is found in your project's `Gemfile`, it will be used through `bundle exec`.
442. If not found in the `Gemfile`, the Ruby extension will look for the executable in your system `PATH`.
453. If the language server is not found in either location, the Ruby extension will automatically install it as a global gem (note: this will not install to your current Ruby gemset).
46
47You can skip step 1 and force using the system executable by setting `use_bundler` to `false` in your settings:
48
49```json [settings]
50{
51 "lsp": {
52 "<SERVER_NAME>": {
53 "settings": {
54 "use_bundler": false
55 }
56 }
57 }
58}
59```
60
61### Using `solargraph`
62
63`solargraph` is enabled by default in the Ruby extension.
64
65### Using `ruby-lsp`
66
67To switch to `ruby-lsp`, add the following to your `settings.json`:
68
69```json [settings]
70{
71 "languages": {
72 "Ruby": {
73 "language_servers": ["ruby-lsp", "!solargraph", "!rubocop", "..."]
74 },
75 // Enable herb and ruby-lsp for *.html.erb files
76 "HTML+ERB": {
77 "language_servers": ["herb", "ruby-lsp", "..."]
78 },
79 // Enable ruby-lsp for *.js.erb files
80 "JS+ERB": {
81 "language_servers": ["ruby-lsp", "..."]
82 },
83 // Enable ruby-lsp for *.yaml.erb files
84 "YAML+ERB": {
85 "language_servers": ["ruby-lsp", "..."]
86 }
87 }
88}
89```
90
91That disables `solargraph` and `rubocop` and enables `ruby-lsp`.
92
93### Using `rubocop`
94
95The Ruby extension also provides support for `rubocop` language server for offense detection and autocorrection.
96
97To enable it, add the following to your `settings.json`:
98
99```json [settings]
100{
101 "languages": {
102 "Ruby": {
103 "language_servers": ["ruby-lsp", "rubocop", "!solargraph", "..."]
104 }
105 }
106}
107```
108
109Or, conversely, you can disable `ruby-lsp` and enable `solargraph` and `rubocop` by adding the following to your `settings.json`:
110
111```json [settings]
112{
113 "languages": {
114 "Ruby": {
115 "language_servers": ["solargraph", "rubocop", "!ruby-lsp", "..."]
116 }
117 }
118}
119```
120
121## Setting up `solargraph`
122
123Solargraph has formatting and diagnostics disabled by default. We can tell Zed to enable them by adding the following to your `settings.json`:
124
125```json [settings]
126{
127 "lsp": {
128 "solargraph": {
129 "initialization_options": {
130 "diagnostics": true,
131 "formatting": true
132 }
133 }
134 }
135}
136```
137
138### Configuration
139
140Solargraph reads its configuration from a file called `.solargraph.yml` in the root of your project. For more information about this file, see the [Solargraph configuration documentation](https://solargraph.org/guides/configuration).
141
142## Setting up `ruby-lsp`
143
144You can pass Ruby LSP configuration to `initialization_options`, e.g.
145
146```json [settings]
147{
148 "languages": {
149 "Ruby": {
150 "language_servers": ["ruby-lsp", "!solargraph", "..."]
151 }
152 },
153 "lsp": {
154 "ruby-lsp": {
155 "initialization_options": {
156 "enabledFeatures": {
157 // "someFeature": false
158 }
159 }
160 }
161 }
162}
163```
164
165For full configuration options, see the [Ruby LSP website](https://shopify.github.io/ruby-lsp/editors.html).
166
167LSP `settings` and `initialization_options` can also be project-specific. For example to use [standardrb/standard](https://github.com/standardrb/standard) as a formatter and linter for a particular project, add this to a `.zed/settings.json` inside your project repo:
168
169```json [settings]
170{
171 "lsp": {
172 "ruby-lsp": {
173 "initialization_options": {
174 "formatter": "standard",
175 "linters": ["standard"]
176 }
177 }
178 }
179}
180```
181
182## Setting up `rubocop` LSP
183
184Rubocop has unsafe autocorrection disabled by default. We can tell Zed to enable it by adding the following to your `settings.json`:
185
186```json [settings]
187{
188 "languages": {
189 "Ruby": {
190 // Use ruby-lsp as the primary language server and rubocop as the secondary.
191 "language_servers": ["ruby-lsp", "rubocop", "!solargraph", "..."]
192 }
193 },
194 "lsp": {
195 "rubocop": {
196 "initialization_options": {
197 "safeAutocorrect": false
198 }
199 },
200 "ruby-lsp": {
201 "initialization_options": {
202 "enabledFeatures": {
203 "diagnostics": false
204 }
205 }
206 }
207 }
208}
209```
210
211## Setting up Sorbet
212
213[Sorbet](https://sorbet.org/) is a popular static type checker for Ruby that includes a language server.
214
215To enable Sorbet, add `\"sorbet\"` to the `language_servers` list for Ruby in your `settings.json`. You may want to disable other language servers if Sorbet is intended to be your primary LSP, or if you plan to use it alongside another LSP for specific features like type checking.
216
217```json [settings]
218{
219 "languages": {
220 "Ruby": {
221 "language_servers": [
222 "ruby-lsp",
223 "sorbet",
224 "!rubocop",
225 "!solargraph",
226 "..."
227 ]
228 }
229 }
230}
231```
232
233For all aspects of installing Sorbet, setting it up in your project, and configuring its behavior, please refer to the [official Sorbet documentation](https://sorbet.org/docs/overview).
234
235## Setting up Steep
236
237[Steep](https://github.com/soutaro/steep) is a static type checker for Ruby that uses RBS files to define types.
238
239To enable Steep, add `\"steep\"` to the `language_servers` list for Ruby in your `settings.json`. You may need to adjust the order or disable other LSPs depending on your desired setup.
240
241```json [settings]
242{
243 "languages": {
244 "Ruby": {
245 "language_servers": [
246 "ruby-lsp",
247 "steep",
248 "!solargraph",
249 "!rubocop",
250 "..."
251 ]
252 }
253 }
254}
255```
256
257## Setting up Herb
258
259`Herb` is enabled by default for the `HTML+ERB` language.
260
261## Using the Tailwind CSS Language Server with Ruby
262
263To get all the features (autocomplete, linting, etc.) from the [Tailwind CSS language server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in Ruby/ERB files, you need to configure the language server so that it knows about where to look for CSS classes by adding the following to your `settings.json`:
264
265```json [settings]
266{
267 "lsp": {
268 "tailwindcss-language-server": {
269 "settings": {
270 "experimental": {
271 "classRegex": ["\\bclass:\\s*['\"]([^'\"]*)['\"]"]
272 }
273 }
274 }
275 }
276}
277```
278
279With these settings, you will get completions for Tailwind CSS classes in HTML attributes inside ERB files and inside Ruby/ERB strings that are coming after a `class:` key. Examples:
280
281```rb
282# Ruby file:
283def method
284 div(class: "pl-2 <completion here>") do
285 p(class: "mt-2 <completion here>") { "Hello World" }
286 end
287end
288
289# ERB file:
290<%= link_to "Hello", "/hello", class: "pl-2 <completion here>" %>
291<a href="/hello" class="pl-2 <completion here>">Hello</a>
292```
293
294## Running tests
295
296To run tests in your Ruby project, you can set up custom tasks in your local `.zed/tasks.json` configuration file. These tasks can be defined to work with different test frameworks like Minitest, RSpec, quickdraw, and tldr. Below are some examples of how to set up these tasks to run your tests from within your editor.
297
298### Minitest with Rails
299
300```json [tasks]
301[
302 {
303 "label": "test $ZED_RELATIVE_FILE -n /$ZED_CUSTOM_RUBY_TEST_NAME/",
304 "command": "bin/rails",
305 "args": [
306 "test",
307 "$ZED_RELATIVE_FILE",
308 "-n",
309 "\"$ZED_CUSTOM_RUBY_TEST_NAME\""
310 ],
311 "cwd": "$ZED_WORKTREE_ROOT",
312 "tags": ["ruby-test"]
313 }
314]
315```
316
317### Minitest
318
319Plain minitest does not support running tests by line number, only by name, so we need to use `$ZED_CUSTOM_RUBY_TEST_NAME` instead:
320
321```json [tasks]
322[
323 {
324 "label": "-Itest $ZED_RELATIVE_FILE -n /$ZED_CUSTOM_RUBY_TEST_NAME/",
325 "command": "bundle",
326 "args": [
327 "exec",
328 "ruby",
329 "-Itest",
330 "$ZED_RELATIVE_FILE",
331 "-n",
332 "\"$ZED_CUSTOM_RUBY_TEST_NAME\""
333 ],
334 "cwd": "$ZED_WORKTREE_ROOT",
335 "tags": ["ruby-test"]
336 }
337]
338```
339
340### RSpec
341
342```json [tasks]
343[
344 {
345 "label": "test $ZED_RELATIVE_FILE:$ZED_ROW",
346 "command": "bundle",
347 "args": ["exec", "rspec", "\"$ZED_RELATIVE_FILE:$ZED_ROW\""],
348 "cwd": "$ZED_WORKTREE_ROOT",
349 "tags": ["ruby-test"]
350 }
351]
352```
353
354Similar task syntax can be used for other test frameworks such as `quickdraw` or `tldr`.
355
356## Debugging
357
358The Ruby extension provides a debug adapter for debugging Ruby code. Zed's name for the adapter (in the UI and `debug.json`) is `rdbg`, and under the hood, it uses the [`debug`](https://github.com/ruby/debug) gem. The extension uses the [same activation logic](#language-server-activation) as the language servers.
359
360### Examples
361
362#### Debug a Ruby script
363
364```json [debug]
365[
366 {
367 "label": "Debug current file",
368 "adapter": "rdbg",
369 "request": "launch",
370 "script": "$ZED_FILE",
371 "cwd": "$ZED_WORKTREE_ROOT"
372 }
373]
374```
375
376#### Debug Rails server
377
378```json [debug]
379[
380 {
381 "label": "Debug Rails server",
382 "adapter": "rdbg",
383 "request": "launch",
384 "command": "./bin/rails",
385 "args": ["server"],
386 "cwd": "$ZED_WORKTREE_ROOT",
387 "env": {
388 "RUBY_DEBUG_OPEN": "true"
389 }
390 }
391]
392```
393
394## Formatters
395
396### `erb-formatter`
397
398To format ERB templates, you can use the `erb-formatter` formatter. This formatter uses the [`erb-formatter`](https://rubygems.org/gems/erb-formatter) gem to format ERB templates.
399
400```json [settings]
401{
402 "HTML+ERB": {
403 "formatter": {
404 "external": {
405 "command": "erb-formatter",
406 "arguments": ["--stdin-filename", "{buffer_path}"]
407 }
408 }
409 }
410}
411```