Now Reading
A good VS Code + Ruby on Rails setup

A good VS Code + Ruby on Rails setup

2024-02-20 07:16:35

Establishing VS Code for Ruby on Rails improvement could be tough, so I wrote this text to assist. Plus, I’ve turned the extensions on this article right into a VS Code Extension Pack. Use it to put in all of the extensions from this text in 1-click, to get began rapidly with VS Code + Ruby on Rails.

Desk of Contents

Utilizing VS Code as a Ruby on Rails editor should not be so arduous! It is tough deciding which extensions to make use of, and a bunch of them are outdated or in a state of disrepair.

I am hoping this quick article will make it simpler to setup VS Code for Ruby on Rails — I’ve collected the core extensions I take advantage of for all my Ruby on Rails enhancing, and even bundled them right into a handy extension pack so you’ll be able to set up them all of sudden.

I’ve grouped the extensions on this article into 4 classes — language servers, different LSPs, formatters/linters, and ERB formatters and language instruments.

Language Server (Ruby LSP… and Solargraph)

The language server is the bread-and-butter of an enhancing expertise. It powers issues like autocomplete, error-checking, jump-to-definition and extra. Within the Ruby on Rails world, we have got two Language Servers — the Shopify Ruby LSP (newer), and Solargraph (older); I run each!

Language servers give you variable suggestions, goto definiton and more. This is an area that Ruby extensions have typically struggled in (compared to other languages)
Language servers offer you variable options, goto definiton and extra. That is an space that Ruby extensions have sometimes struggled in (in comparison with different languages)

The Shopify Ruby LSP is an superior mission and enhancing each day (actually, there are updates to the extension each few days), however after almost 2 years of improvement, it nonetheless would not fully exchange Solargraph. I discover that Solargraph remains to be higher at autocompleting variables and methodology names, so I simply run each (they play properly collectively).

I am hoping that someday, the Shopify Ruby LSP will overtake Solargraph in options and we’ll have the ability to simply run a single Language Server, nevertheless it will not occur for some time. Till then although, you’ll be able to safely run each Language Servers collectively with none hassle.

Language Server Extensions:

Different LSP Integrations (Stimulus LSP and Rails LSP)

The introduction of the LSP protocol paved the best way for full-fledged LSPs just like the Ruby LSP from above, nevertheless it’s additionally made it straightforward for smaller, bespoke LSPs to combine with VS Code. There are at the moment two key LSPs value testing — Stimulus LSP and Rails LSP. Each are nice!

The Stimulus LSP from Marco Roth offers you editor options for Stimulus controllers, and the Rails LSP from Shopify enables you to hover over fashions in your app to view their schema.

For those who’re utilizing the Rails LSP, be certain that to comply with the installation instructions by including it to your Gemfile —

group :improvement do
  gem "ruby-lsp-rails"

Be aware: the Rails LSP sits on prime of the Ruby LSP extension. You needn’t set up an additional extension, simply the gem.

The Stimulus LSP will get up to date fairly ceaselessly and is shaping as much as be a very helpful device for constructing Stimulus controllers; The Rails LSP is fairly barebones proper now, however I am hoping that after Shopify has the Ruby LSP in an excellent place, they’re going to shift their focus to enhancing the Rails LSP.

Different LSP Extensions:

Ruby Linter & Formatter (Rubocop, Commonplace and Rufo)

Linters and formatters are likely to go hand-in-hand, regardless of being barely completely different — linters right code fashion (like the way you construction conditionals), whereas formatters right code formatting (like whether or not to insert an area inside hashes).

Each are sometimes must-haves for any non-trivial mission — they maintain your code constant between information and initiatives, and between teammates too.

The linter/formatter you decide simply comes all the way down to desire. Do you wish to:

  • use heavyweight Rubocop and tweak it till you are glad,
  • select Standard as a prescriptive linter + formatter,
  • forgo linting altogether and use Rufo for formatting?

I take advantage of Rufo in all my side-projects, and Commonplace at work — I like how prescriptive they’re (in comparison with Rubocop), and I discover that for side-projects I am wonderful giving up linting for my most well-liked formatter, Rufo.

Formatting some odd looking Ruby code with the Rufo extension
Formatting some odd trying Ruby code with the Rufo extension

Be aware: be sure you’ve bought the rubocop, commonplace or rufo gems accessible in your PATH (relying on which one you select). You will most likely have to run one in every of:

See Also

  • bundle add rubocop
  • bundle add commonplace
  • bundle add rufo

Ruby Linter & Formatter extensions:

Lastly, you may want a separate set of extensions to deal with Rails view templates. ERB is the preferred, and it is what I take advantage of, so I’ve included ERB-related extensions (you may want to seek out your individual extensions for HAML or Slim).

I take advantage of two ERB extensions for VS Code — the primary is Simple Ruby ERB. It is terribly maintained (final up to date: 7 years in the past), and I solely actually use it for the ctrl + shift + , shortcut to insert and cycle by means of ERB tags (<%%> --> <%=%> --> <%#%>). It does even have some primary helpers for if/else blocks, link_to and so forth. which I sometimes use (full listing here).

The opposite ERB extension I take advantage of is ERB Formatter/Beautify; it is a formatter for .erb template information (duh!). It really works fairly effectively, though I favor how Rufo codecs Ruby inside ERB information so I normally run each throughout a file.

Using a snippet from Simple Ruby ERB, then formatting HTML and ERB with the ERB Formatter/Beautify extension
Utilizing a snippet from Easy Ruby ERB, then formatting HTML and ERB with the ERB Formatter/Beautify extension

There are a bunch extra ERB extensions that you may check out (for issues like code snippet options), however I am fairly proud of these two and discover them to be sufficient.

Ruby Linter & Formatter extensions:


That is it! I attempt to maintain my set of VS Code extensions fairly light-weight, however I am certain there are a bunch extra nice ones on the market.

I hope my Ruby on Rails VS Code extension pack is helpful to you! I believe it is an effective way to get VS Code put in with a stable baseline set of extensions to cowl all of your bases.

Source Link

What's Your Reaction?
In Love
Not Sure
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top