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