Ruby LSP Rails

Public
An opinionated language server for Ruby on Rails
Icons/chart bar
Used 25 times
Created by
V Viktor Schmidt

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/x9QsmG"
Template Source

Review the code before running this template on your machine.

def do_bundle
  Bundler.with_unbundled_env { run "bundle install" }
end

def print_green(heredoc)
  puts set_color heredoc, :green
end

def do_commit
  git :init
  git add: "."
  Bundler.with_unbundled_env { git commit: " -m 'Add language server for Ruby' " }
end

say "\nApplying language server for Ruby..."
# Check if RSpec default folder exists, and if so, install ruby-lsp-rspec
# See https://github.com/st0012/ruby-lsp-rspec/issues/45#issuecomment-2421964971
if Dir.exist?("spec")
  inject_into_file 'Gemfile', after: 'group :development do' do
    <<~RUBY.indent(2)
    # Ruby LSP RSpec add-on for better RSpec support
    gem "ruby-lsp-rspec", require: false
    RUBY
  end
end

# Don't do this!
# Inject Ruby LSP for development
# inject_into_file 'Gemfile', after: 'group :development do' do
#   <<~RUBY.indent(2)

#   # Language server for Ruby
#   gem "ruby-lsp-rails", require: false
#   RUBY
# end

do_bundle

say "\nAdding documentation for developers..."
create_file "docs/quality_assurance.md", "# Source code quality assurance and best practices\n" unless File.exist? "docs/quality_assurance.md"
append_file "docs/quality_assurance.md" do
  <<~EOF

  ## Ruby LSP

  [Ruby LSP Rails](https://rubygems.org/gems/ruby-lsp-rails) for code completion, inline documentation, and static analysis.

  ### Usage

  #### With VS Code

  If using VS Code, all you have to do is install the [Ruby LSP extension](https://marketplace.visualstudio.com/items?itemName=Shopify.ruby-lsp)
  to get the extra features in the editor. Do not install the `ruby-lsp` gem manually.

  For more information on using and configuring the extension, see the extension’s [README.md](https://github.com/Shopify/ruby-lsp/blob/main/vscode/README.md).

  #### With other editors

  See [editors](https://shopify.github.io/ruby-lsp/editors) for community instructions on setting up the Ruby LSP,
  which currently includes Emacs, Neovim, Sublime Text, and Zed.

  The gem can be installed by doing

  ```bash
  gem install ruby-lsp
  ```

  and the language server can be launched running `ruby-lsp`
  (without bundle exec in order to properly hook into your project’s dependencies).

  #### Composed Ruby LSP bundle

  The Ruby LSP executable generates a composed bundle with the goal of not requiring users to add the `ruby-lsp`
  gem to their Gemfiles, and at the same time being able to hook into project dependencies.
  [Learn more](https://shopify.github.io/ruby-lsp/composed-bundle).
  EOF
end

do_commit

print_green "\nAdded language server for Ruby successfully!"
Comments
Viktor Schmidt
Use rubyfmt too:

```
def do_bundle
  Bundler.with_unbundled_env { run "bundle install" }
end

def print_green(heredoc)
  puts set_color heredoc, :green
end

def do_commit
  git :init
  git add: "."
  Bundler.with_unbundled_env { git commit: " -m 'Add language server for Ruby and Rubyfmt' " }
end

say "\nApplying language server for Ruby and Rubyfmt..."
# Check if RSpec default folder exists, and if so, install ruby-lsp-rspec
if Dir.exist?("spec")
  inject_into_file 'Gemfile', after: 'group :development do' do
    <<-RUBY

    # Ruby LSP RSpec add-on for better RSpec support
    gem "ruby-lsp-rspec", require: false
    RUBY
  end
end

# Inject Rubyfmt add-on
inject_into_file 'Gemfile', after: 'group :development do' do
  <<-RUBY

  # Rubyfmt for Ruby LSP formatting
  gem "ruby-lsp-rubyfmt", require: false
  RUBY
end

# Inject Ruby LSP for development
inject_into_file 'Gemfile', after: 'group :development do' do
  <<-RUBY

  # Language server for Ruby
  gem "ruby-lsp-rails", require: false
  RUBY
end

do_bundle

say "\nAdding documentation for developers..."
create_file "docs/quality_assurance.md", "# Source code quality assurance and best practices\n" unless File.exist? "docs/quality_assurance.md"
append_file "docs/quality_assurance.md" do
  <<~EOF

  ## Ruby LSP

  [Ruby LSP Rails](https://rubygems.org/gems/ruby-lsp-rails) for code completion, inline documentation, and static analysis.

  Also check out Microsoft Visual Studio Code Extensions:

  - [Ruby LSP](https://marketplace.visualstudio.com/items?itemName=Shopify.ruby-lsp)
  - [Shopify Ruby Extensions Pack](https://marketplace.visualstudio.com/items?itemName=Shopify.ruby-extensions-pack)

  For more information on configuring Ruby LSP, visit:

  - [Ruby LSP Documentation](https://shopify.github.io/ruby-lsp)
  - [Ruby LSP Rails Add-on](https://shopify.github.io/ruby-lsp/rails-add-on)

  ### Rubyfmt Setup

  To use Rubyfmt with Ruby LSP, follow these steps:

  1. Modify your VS Code `settings.json` file:

  ```json
  "rubyLsp.formatter": "rubyfmt"
  ```

  2. Open the VS Code command palette, then select "Developer: Reload Window".

  Rubyfmt is an external dependency required by this add-on.

  ### Installing Rubyfmt

  If you already have Homebrew installed, you can install Rubyfmt by running:

  ```
  brew install rubyfmt
  ```

  This add-on calls the `rubyfmt` executable directly via `addon.rb`.
  EOF
end

do_commit

print_green "\nAdded language server for Ruby, Ruby LSP RSpec (if applicable), and Rubyfmt successfully!"
````