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", "..."]
 20    }
 21  }
 22}
 23```
 24
 25## Setting up `solargraph`
 26
 27Zed 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`.
 28
 29You can install the gem manually with the following command:
 30
 31```shell
 32gem install solargraph
 33```
 34
 35Alternatively, if your project uses Bundler, you can add the Solargraph gem to your `Gemfile`:
 36
 37```ruby
 38gem 'solargraph', group: :development
 39```
 40
 41Solargraph has formatting and diagnostics disabled by default. We can tell Zed to enable them by adding the following to your `settings.json`:
 42
 43```json
 44{
 45  "lsp": {
 46    "solargraph": {
 47      "initialization_options": {
 48        "diagnostics": true,
 49        "formatting": true
 50      }
 51    }
 52  }
 53}
 54```
 55
 56### Configuration
 57
 58Solargraph 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).
 59
 60## Setting up `ruby-lsp`
 61
 62Zed 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`.
 63
 64You can install the gem manually with the following command:
 65
 66```shell
 67gem install ruby-lsp
 68```
 69
 70Ruby 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`:
 71
 72```json
 73{
 74  "lsp": {
 75    "ruby-lsp": {
 76      "initialization_options": {
 77        "enabledFeatures": {
 78          "diagnostics": false
 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```