1# Ruby
2
3Ruby support is available through the [Ruby extension](https://github.com/zed-industries/zed/tree/main/extensions/ruby).
4
5The Ruby extension also provides support for ERB files.
6
7## Choosing a language server
8
9The Ruby extension offers both `solargraph` and `ruby-lsp` language server support.
10
11`solargraph` is enabled by default.
12
13To switch to `ruby-lsp`, add the following to your `settings.json`:
14
15```json
16{
17 "languages": {
18 "Ruby": {
19 "language_servers": ["ruby-lsp", "!solargraph", "!rubocop", "..."]
20 }
21 }
22}
23```
24
25The Ruby extension also provides support for `rubocop` language server for offense detection and autocorrection. To enable it, add the following to your
26`settings.json`:
27
28```json
29{
30 "languages": {
31 "Ruby": {
32 "language_servers": ["ruby-lsp", "rubocop", "!solargraph", "..."]
33 }
34 }
35}
36```
37
38## Setting up `solargraph`
39
40Zed currently doesn't install Solargraph automatically. To use Solargraph, you need to install the gem. Zed just looks for an executable called `solargraph` on your `PATH`.
41
42You can install the gem manually with the following command:
43
44```shell
45gem install solargraph
46```
47
48Alternatively, if your project uses Bundler, you can add the Solargraph gem to your `Gemfile`:
49
50```ruby
51gem 'solargraph', group: :development
52```
53
54Solargraph has formatting and diagnostics disabled by default. We can tell Zed to enable them by adding the following to your `settings.json`:
55
56```json
57{
58 "lsp": {
59 "solargraph": {
60 "initialization_options": {
61 "diagnostics": true,
62 "formatting": true
63 }
64 }
65 }
66}
67```
68
69To use Solargraph in the context of the bundle, you can use [folder-specific settings](../configuring-zed#settings-files) and specify the absolute path to the [`binstub`](https://bundler.io/v2.5/man/bundle-binstubs.1.html) of Solargraph:
70
71```json
72{
73 "lsp": {
74 "solargraph": {
75 "binary": {
76 "path": "<path_to_your_project>/bin/solargraph"
77 }
78 }
79 }
80}
81```
82
83### Configuration
84
85Solargraph 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).
86
87## Setting up `ruby-lsp`
88
89Zed currently doesn't install Ruby LSP automatically. To use Ruby LSP, you need to install the gem. Zed just looks for an executable called `ruby-lsp` on your `PATH`.
90
91You can install the gem manually with the following command:
92
93```shell
94gem install ruby-lsp
95```
96
97Ruby LSP uses pull-based diagnostics which Zed doesn't support yet. We can tell Zed to disable it by adding the following to your `settings.json`:
98
99```json
100{
101 "lsp": {
102 "ruby-lsp": {
103 "initialization_options": {
104 "enabledFeatures": {
105 "diagnostics": false
106 }
107 }
108 }
109 }
110}
111```
112
113## Setting up `rubocop` LSP
114
115Zed currently doesn't install `rubocop` automatically. To use `rubocop`, you need to install the gem. Zed just looks for an executable called `rubocop` on your `PATH`.
116
117You can install the gem manually with the following command:
118
119```shell
120gem install rubocop
121```
122
123Rubocop has unsafe autocorrection disabled by default. We can tell Zed to enable it by adding the following to your `settings.json`:
124
125```json
126{
127 "lsp": {
128 "rubocop": {
129 "initialization_options": {
130 "safeAutocorrect": false
131 }
132 }
133 }
134}
135```
136
137To use Rubocop in the context of the bundle, you can use [folder-specific settings](../configuring-zed#settings-files) and specify the absolute path to the [`binstub`](https://bundler.io/v2.5/man/bundle-binstubs.1.html) of Rubocop:
138
139```json
140{
141 "lsp": {
142 "rubocop": {
143 "binary": {
144 "path": "<path_to_your_project>/bin/rubocop"
145 }
146 }
147 }
148}
149```
150
151## Using the Tailwind CSS Language Server with Ruby
152
153It'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.
154
155In 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`:
156
157```json
158{
159 "languages": {
160 "Ruby": {
161 "language_servers": ["tailwindcss-language-server", "..."]
162 }
163 },
164 "lsp": {
165 "tailwindcss-language-server": {
166 "settings": {
167 "includeLanguages": {
168 "erb": "html",
169 "ruby": "html"
170 },
171 "experimental": {
172 "classRegex": ["\\bclass:\\s*['\"]([^'\"]*)['\"]"]
173 }
174 }
175 }
176 }
177}
178```
179
180With 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:
181
182```ruby
183# Ruby file:
184def method
185 div(class: "pl-2 <completion here>") do
186 p(class: "mt-2 <completion here>") { "Hello World" }
187 end
188end
189
190# ERB file:
191<%= link_to "Hello", "/hello", class: "pl-2 <completion here>" %>
192<a href="/hello" class="pl-2 <completion here>">Hello</a>
193```