1# Ruby
2
3Ruby support is available through the [Ruby extension](https://github.com/zed-industries/zed/tree/main/extensions/ruby).
4
5## Choosing a language server
6
7The Ruby extension offers both `solargraph` and `ruby-lsp` language server support.
8
9`solargraph` is enabled by default.
10
11To switch to `ruby-lsp`, add the following to your `settings.json`:
12
13```json
14{
15 "languages": {
16 "Ruby": {
17 "language_servers": ["ruby-lsp", "!solargraph", "..."]
18 }
19 }
20}
21```
22
23## Setting up `solargraph`
24
25Zed 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`.
26
27You can install the gem manually with the following command:
28
29```shell
30gem install solargraph
31```
32
33Alternatively, if your project uses Bundler, you can add the Solargraph gem to your `Gemfile`:
34
35```ruby
36gem 'solargraph', group: :development
37```
38
39Solargraph has formatting and diagnostics disabled by default. We can tell Zed to enable them by adding the following to your `settings.json`:
40
41```json
42{
43 "lsp": {
44 "solargraph": {
45 "initialization_options": {
46 "diagnostics": true,
47 "formatting": true
48 }
49 }
50 }
51}
52```
53
54### Configuration
55
56Solargraph 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).
57
58## Setting up `ruby-lsp`
59
60Zed 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`.
61
62You can install the gem manually with the following command:
63
64```shell
65gem install ruby-lsp
66```
67
68Ruby 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`:
69
70```json
71{
72 "lsp": {
73 "ruby-lsp": {
74 "initialization_options": {
75 "enabledFeatures": {
76 "diagnostics": false
77 }
78 }
79 }
80 }
81}
82```
83
84## Using the Tailwind CSS Language Server with Ruby
85
86It'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.
87
88In 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`:
89
90```json
91{
92 "languages": {
93 "Ruby": {
94 "language_servers": ["tailwindcss-language-server", "..."]
95 }
96 },
97 "lsp": {
98 "tailwindcss-language-server": {
99 "settings": {
100 "includeLanguages": {
101 "erb": "html",
102 "ruby": "html"
103 },
104 "experimental": {
105 "classRegex": ["\\bclass:\\s*['\"]([^'\"]*)['\"]"]
106 }
107 }
108 }
109 }
110}
111```
112
113With 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:
114
115```ruby
116# Ruby file:
117def method
118 div(class: "pl-2 <completion here>") do
119 p(class: "mt-2 <completion here>") { "Hello World" }
120 end
121end
122
123# ERB file:
124<%= link_to "Hello", "/hello", class: "pl-2 <completion here>" %>
125<a href="/hello" class="pl-2 <completion here>">Hello</a>
126```