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 }
76}
77```
78
79That disables `solargraph` and `rubocop` and enables `ruby-lsp`.
80
81### Using `rubocop`
82
83The Ruby extension also provides support for `rubocop` language server for offense detection and autocorrection.
84
85To enable it, add the following to your `settings.json`:
86
87```json [settings]
88{
89 "languages": {
90 "Ruby": {
91 "language_servers": ["ruby-lsp", "rubocop", "!solargraph", "..."]
92 }
93 }
94}
95```
96
97Or, conversely, you can disable `ruby-lsp` and enable `solargraph` and `rubocop` by adding the following to your `settings.json`:
98
99```json [settings]
100{
101 "languages": {
102 "Ruby": {
103 "language_servers": ["solargraph", "rubocop", "!ruby-lsp", "..."]
104 }
105 }
106}
107```
108
109## Setting up `solargraph`
110
111Solargraph has formatting and diagnostics disabled by default. We can tell Zed to enable them by adding the following to your `settings.json`:
112
113```json [settings]
114{
115 "lsp": {
116 "solargraph": {
117 "initialization_options": {
118 "diagnostics": true,
119 "formatting": true
120 }
121 }
122 }
123}
124```
125
126### Configuration
127
128Solargraph 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).
129
130## Setting up `ruby-lsp`
131
132You can pass Ruby LSP configuration to `initialization_options`, e.g.
133
134```json [settings]
135{
136 "languages": {
137 "Ruby": {
138 "language_servers": ["ruby-lsp", "!solargraph", "..."]
139 }
140 },
141 "lsp": {
142 "ruby-lsp": {
143 "initialization_options": {
144 "enabledFeatures": {
145 // "someFeature": false
146 }
147 }
148 }
149 }
150}
151```
152
153LSP `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:
154
155```json [settings]
156{
157 "lsp": {
158 "ruby-lsp": {
159 "initialization_options": {
160 "formatter": "standard",
161 "linters": ["standard"]
162 }
163 }
164 }
165}
166```
167
168## Setting up `rubocop` LSP
169
170Rubocop has unsafe autocorrection disabled by default. We can tell Zed to enable it by adding the following to your `settings.json`:
171
172```json [settings]
173{
174 "languages": {
175 "Ruby": {
176 // Use ruby-lsp as the primary language server and rubocop as the secondary.
177 "language_servers": ["ruby-lsp", "rubocop", "!solargraph", "..."]
178 }
179 },
180 "lsp": {
181 "rubocop": {
182 "initialization_options": {
183 "safeAutocorrect": false
184 }
185 },
186 "ruby-lsp": {
187 "initialization_options": {
188 "enabledFeatures": {
189 "diagnostics": false
190 }
191 }
192 }
193 }
194}
195```
196
197## Setting up Sorbet
198
199[Sorbet](https://sorbet.org/) is a popular static type checker for Ruby that includes a language server.
200
201To 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.
202
203```json [settings]
204{
205 "languages": {
206 "Ruby": {
207 "language_servers": [
208 "ruby-lsp",
209 "sorbet",
210 "!rubocop",
211 "!solargraph",
212 "..."
213 ]
214 }
215 }
216}
217```
218
219For 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).
220
221## Setting up Steep
222
223[Steep](https://github.com/soutaro/steep) is a static type checker for Ruby that uses RBS files to define types.
224
225To 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.
226
227```json [settings]
228{
229 "languages": {
230 "Ruby": {
231 "language_servers": [
232 "ruby-lsp",
233 "steep",
234 "!solargraph",
235 "!rubocop",
236 "..."
237 ]
238 }
239 }
240}
241```
242
243## Setting up Herb
244
245`Herb` is enabled by default for the `HTML+ERB` language.
246
247## Using the Tailwind CSS Language Server with Ruby
248
249It's possible to use the [Tailwind CSS Language Server](https://github.com/tailwindlabs/tailwindcss-intellisense/tree/HEAD/packages/tailwindcss-language-server#readme) in Ruby and ERB files.
250
251In order to do that, you need to configure the language server so that it knows about where to look for CSS classes in Ruby/ERB files by adding the following to your `settings.json`:
252
253```json [settings]
254{
255 "languages": {
256 "Ruby": {
257 "language_servers": ["tailwindcss-language-server", "..."]
258 }
259 },
260 "lsp": {
261 "tailwindcss-language-server": {
262 "settings": {
263 "experimental": {
264 "classRegex": ["\\bclass:\\s*['\"]([^'\"]*)['\"]"]
265 }
266 }
267 }
268 }
269}
270```
271
272With 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:
273
274```rb
275# Ruby file:
276def method
277 div(class: "pl-2 <completion here>") do
278 p(class: "mt-2 <completion here>") { "Hello World" }
279 end
280end
281
282# ERB file:
283<%= link_to "Hello", "/hello", class: "pl-2 <completion here>" %>
284<a href="/hello" class="pl-2 <completion here>">Hello</a>
285```
286
287## Running tests
288
289To 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.
290
291### Minitest with Rails
292
293```json [tasks]
294[
295 {
296 "label": "test $ZED_RELATIVE_FILE -n /$ZED_CUSTOM_RUBY_TEST_NAME/",
297 "command": "bin/rails",
298 "args": [
299 "test",
300 "$ZED_RELATIVE_FILE",
301 "-n",
302 "\"$ZED_CUSTOM_RUBY_TEST_NAME\""
303 ],
304 "cwd": "$ZED_WORKTREE_ROOT",
305 "tags": ["ruby-test"]
306 }
307]
308```
309
310### Minitest
311
312Plain minitest does not support running tests by line number, only by name, so we need to use `$ZED_CUSTOM_RUBY_TEST_NAME` instead:
313
314```json [tasks]
315[
316 {
317 "label": "-Itest $ZED_RELATIVE_FILE -n /$ZED_CUSTOM_RUBY_TEST_NAME/",
318 "command": "bundle",
319 "args": [
320 "exec",
321 "ruby",
322 "-Itest",
323 "$ZED_RELATIVE_FILE",
324 "-n",
325 "\"$ZED_CUSTOM_RUBY_TEST_NAME\""
326 ],
327 "cwd": "$ZED_WORKTREE_ROOT",
328 "tags": ["ruby-test"]
329 }
330]
331```
332
333### RSpec
334
335```json [tasks]
336[
337 {
338 "label": "test $ZED_RELATIVE_FILE:$ZED_ROW",
339 "command": "bundle",
340 "args": ["exec", "rspec", "\"$ZED_RELATIVE_FILE:$ZED_ROW\""],
341 "cwd": "$ZED_WORKTREE_ROOT",
342 "tags": ["ruby-test"]
343 }
344]
345```
346
347Similar task syntax can be used for other test frameworks such as `quickdraw` or `tldr`.
348
349## Debugging
350
351The 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.
352
353### Examples
354
355#### Debug a Ruby script
356
357```json [debug]
358[
359 {
360 "label": "Debug current file",
361 "adapter": "rdbg",
362 "request": "launch",
363 "script": "$ZED_FILE",
364 "cwd": "$ZED_WORKTREE_ROOT"
365 }
366]
367```
368
369#### Debug Rails server
370
371```json [debug]
372[
373 {
374 "label": "Debug Rails server",
375 "adapter": "rdbg",
376 "request": "launch",
377 "command": "./bin/rails",
378 "args": ["server"],
379 "cwd": "$ZED_WORKTREE_ROOT",
380 "env": {
381 "RUBY_DEBUG_OPEN": "true"
382 }
383 }
384]
385```
386
387## Formatters
388
389### `erb-formatter`
390
391To 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.
392
393```json [settings]
394{
395 "HTML+ERB": {
396 "formatter": {
397 "external": {
398 "command": "erb-formatter",
399 "arguments": ["--stdin-filename", "{buffer_path}"]
400 }
401 }
402 }
403}
404```