ruby.md

  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": ["rubocop", "ruby-lsp", "!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
 69### Configuration
 70
 71Solargraph 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).
 72
 73## Setting up `ruby-lsp`
 74
 75Zed 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`.
 76
 77You can install the gem manually with the following command:
 78
 79```shell
 80gem install ruby-lsp
 81```
 82
 83Ruby 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`:
 84
 85```json
 86{
 87  "lsp": {
 88    "ruby-lsp": {
 89      "initialization_options": {
 90        "enabledFeatures": {
 91          "diagnostics": false
 92        }
 93      }
 94    }
 95  }
 96}
 97```
 98
 99## Setting up `rubocop` LSP
100
101Zed 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`.
102
103You can install the gem manually with the following command:
104
105```shell
106gem install rubocop
107```
108
109Rubocop has unsafe autocorrection disabled by default. We can tell Zed to enable it by adding the following to your `settings.json`:
110
111```json
112{
113  "lsp": {
114    "rubocop": {
115      "initialization_options": {
116        "safeAutocorrect": false
117      }
118    }
119  }
120}
121```
122
123## Using the Tailwind CSS Language Server with Ruby
124
125It'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.
126
127In 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`:
128
129```json
130{
131  "languages": {
132    "Ruby": {
133      "language_servers": ["tailwindcss-language-server", "..."]
134    }
135  },
136  "lsp": {
137    "tailwindcss-language-server": {
138      "settings": {
139        "includeLanguages": {
140          "erb": "html",
141          "ruby": "html"
142        },
143        "experimental": {
144          "classRegex": ["\\bclass:\\s*['\"]([^'\"]*)['\"]"]
145        }
146      }
147    }
148  }
149}
150```
151
152With 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:
153
154```ruby
155# Ruby file:
156def method
157  div(class: "pl-2 <completion here>") do
158    p(class: "mt-2 <completion here>") { "Hello World" }
159  end
160end
161
162# ERB file:
163<%= link_to "Hello", "/hello", class: "pl-2 <completion here>" %>
164<a href="/hello" class="pl-2 <completion here>">Hello</a>
165```