1

I'm using VS Code and writing Ruby code on MacOS 12.6. I have installed several extensions for linting. I can see the errors, but I cannot get any of them to provide automatic correction, even for trivial mistakes like single vs double quotes. It always says "no quick fixes available".

enter image description here

If I use the command palette to run "Format document" it will correct these kinds of mistakes. So somehow VS Code knows how to fix these problems. It just won't do it in a convenient way.

You can see in the screenshot which extensions I have installed / active:

  • Ruby by Peng Lv
  • VSCode Ruby by Stafford Bunk
  • Ruby Solargraph by Castwide
  • ruby-rubocop by misogi

I don't really care which extensions are used. I would be happy with any working configuration that provides this basic functionality.

Here is my entire settings.json

{
  "workbench.tree.indent": 16,
  "editor.formatOnSaveMode": "modifications",
  "editor.formatOnSaveTimeout": 5000,
  // "ruby.rubocop.onSave": true,
  "ruby.useBundler": true, //run non-lint commands with bundle exec
  "ruby.useLanguageServer": true, // use the internal language server (see below)
  "ruby.lint": {

    "rubocop": {
      "useBundler": true // enable rubocop via bundler
    },
    "reek": {
      "useBundler": true // enable reek via bundler
    }
  },
  "ruby.format": "rubocop", // use rubocop for formatting
  "eslint.format.enable": true,
  "eslint.options": {
    "extensions": [ ".html", ".js", ".vue", ".jsx" ]
  },
  "eslint.validate": [
    "html",
    "javascript",
    "vue"
  ],  
  "[vue]":  {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  // "[ruby]": {
  //   "editor.defaultFormatter": "misogi.ruby-rubocop",
  //   "editor.formatOnSave": true
  // },
  "[json]": {},
  "ruby.codeCompletion": "rcodetools",
  "ruby.intellisense": "rubyLocate",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "editor.tabSize": 2,
  "[ruby]": {
    "editor.defaultFormatter": "misogi.ruby-rubocop"
  
  },
  "editor.formatOnSave": true,
  "ruby.rubocop.useBundler": true,
}

Note

This question sounds similar to: vscode( vscode-ruby + rubocop ) how to auto correct on save?

But that question is old and the answers reference config settings that aren’t recognized anymore.

emersonthis
  • 32,822
  • 59
  • 210
  • 375
  • This a duplicated thread. Answer: https://stackoverflow.com/questions/74976343/how-to-auto-correct-ruby-linting-errors-in-vs-code – Julian Jan 01 '23 at 22:20
  • Does this answer your question? [vscode( vscode-ruby + rubocop ) how to auto correct on save?](https://stackoverflow.com/questions/48030698/vscode-vscode-ruby-rubocop-how-to-auto-correct-on-save) – Julian Jan 01 '23 at 22:21
  • No that question is focused on correcting on save as opposed to the quick fix Im trying to solve. I’ve been through that question many times and none of the answers solve my problem. – emersonthis Jan 02 '23 at 01:55
  • There is a [revived version of the VSCode Rubocop extension](https://dev.to/lorankloeze/revived-extension-to-autoformat-with-rubocop-in-vscode-1fd8) which might fix some of your issues, specifically with [this](https://github.com/LoranKloeze/vscode-ruby-rubocop-revived/pull/7) merged. – Holger Just Jan 02 '23 at 10:43
  • @HolgerJust That extension works! I've been searching for more than a year and this is the first VS Code extension for Ruby linting that seems to work completely. Thanks! If you submit an answer, I'll accept it! – emersonthis Jan 02 '23 at 18:05
  • All the mentioned extensions are old and either not maintained anymore or are half-working. See my answer for a working solution below (https://stackoverflow.com/questions/74976343/how-to-auto-correct-ruby-linting-errors-in-vs-code-in-2023/76295215#answer-76295215). – Artur INTECH May 20 '23 at 12:26

2 Answers2

3

There is a revived Rubocop for Visual Studio Code extension by Loran Kloeze available on the Visual Studio Marketplace.

This extension is a fork of the original Rubocop extension and adds a few features, such as Rubocop server support for improved performance and specifically, a feature to Implement quick fixes for Rubocop.

Even with the original extension by m1sogi, you should have been able to use the global auto correct function. The new quick fix feature is probably more useful though...

Holger Just
  • 52,918
  • 14
  • 115
  • 123
1

May 2023 update:

  1. Install Ruby LSP extension from Shopify (Shopify.ruby-lsp).
  2. Add gem "rubocop" to your Gemfile.
  3. Restart VS Code (or run Ruby LSP: restart action) and make sure Rubocop is picked up by that extension as a formatter by clicking on the braces icon in the right bottom corner. It should like like this:

enter image description here

  1. Enjoy autocorrections available either via "Problems" panel or under "Quick Fix" popup. By the way, SHIFT + ALT + . (period) key combination applies auto-fix without displaying a popup.

enter image description here enter image description here

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34