Using Ruby LSP to format code the Standard way
I recently started using the new Ruby LSP from Shopify in VSCode. It’s pretty great. At the same time I used the (also excellent) Standard Ruby extension to handle formatting. This was not so simple, as it began linting all kinds of rules, I just wanted to ignore. My boss helped me with a solution. It requires just editing two files:
# .rubocop.yml
require:
- standard
inherit_gem:
standard: config/base.yml
AllCops:
TargetRubyVersion: 3.2.0
# either in your settings.json or .vscode/settings.json if you want to scope it to your project
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp", // Use the Ruby LSP as the default formatter
"editor.formatOnSave": true, // Format files automatically when saving
"editor.tabSize": 2, // Use 2 spaces for indentation
"editor.insertSpaces": true, // Use spaces and not tabs for indentantion
"editor.semanticHighlighting.enabled": true, // Enable semantic highlighting
"editor.formatOnType": true // Enable formatting while typing
},
"rubyLsp.formatter": "rubocop"
Now you can remove the Standard Ruby extension and still get that sweet autoformatting - along with all the benefits of Ruby LSP.