Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smcabrera/a132579c80b2c5ffb75149383508d7bc to your computer and use it in GitHub Desktop.
Save smcabrera/a132579c80b2c5ffb75149383508d7bc to your computer and use it in GitHub Desktop.
Setting up linting and auto-formatting for ruby with VS code

vs code configuration

#work/reference

Ruby linting vs code

Setting up vim bindings on VS code

Ruby plugin

  • Install solargraph gem so that you can get code completion
  • Install rubocop and rubocop-airbnb gems so you can get linting (reading this last part suggests to me that this may be all we need)

Go through the ruby plugin and in each of the different sections there will be configurations for you to choose and

Linting

We use rubocop for linting so set it up with that.

These are modified from the instructions in the README to fit our particular use case at Canvas.

In User settings

// Basic settings: turn linter(s) on
“ruby.lint”: {
	“reek”: false,
	“rubocop”: true,
	“ruby”: false, 
	“fasterer”: false,
	“debride”: false,
	“ruby-lint”: false
},

// Time (ms) to wait after keypress before running enabled linters. Ensures
// linters are only run when typing has finished and not for every keypress
“ruby.lintDebounceTime”: 500,

Gotchas

To automatically format ruby files when you save. If you’ve already installed rubocop gem all you should need to do is add the following to your settings.json:

    "[ruby]": {
        "editor.formatOnSave": true
    },
    "ruby.format": "rubocop",
    "editor.formatOnSaveTimeout": 2500

Debugging

Apparently this doesn’t work perfectly out of the box if you’re using rvm. For rubocop installed with rvm you may need to explicitly tell VS code where to find the executable. So your config will look something like this:

{
    "ruby.lint": {
        "reek": false,
        "rubocop": {
          "path": "path/to/folder/containing/rubocop"
        },
        "ruby": false, //Runs ruby -wc
        "fasterer": false,
        "debride": false,
        "ruby-lint": false
      },
      // Formatting to apply cops on save
      "[ruby]": {
        "editor.formatOnSave": true
      },
      "ruby.format": "rubocop",
      "editor.formatOnSaveTimeout": 2500,
    
      "ruby.lintDebounceTime": 500,
      "ruby.rubocop.executePath" : "/Users/clairecanvas/.rvm/gems/ruby-2.5.0/bin/"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment