misogi / vscode-ruby-rubocop

Rubocop extension for Visual Studio Code
https://marketplace.visualstudio.com/items/misogi.ruby-rubocop
MIT License
137 stars 68 forks source link

autocorrect on save #49

Open esalter opened 7 years ago

esalter commented 7 years ago

It seems like this plugin only lints on save, not autocorrect. I'm used to being able to just save a file and have it auto-run formatters to correct mistakes. Can that be added?

This is somewhat related to https://github.com/misogi/vscode-ruby-rubocop/issues/48 since I wouldn't want to lose undo history in this case though.

Thanks!

damien commented 7 years ago

@esalter I'm not sure I understand your request, could you clarify?

When you say "lint on autocorrect", do you mean that Rubocop should re-lint after autocorrecting a file?

esalter commented 7 years ago

I mean that the act of saving the file should auto-correct it as well. It does not appear to do that right now.

damien commented 7 years ago

@esalter You are correct. We could plan this as a feature, but it'd probably be opt-in via a config option of some sort.

esalter commented 7 years ago

That would be fine. I took a stab at it locally and it seems easy (just append the option if configured), but I'd want the undo functionality mentioned in the other issue which I'm not sure how to do. Thanks for taking a look. :)

damien commented 7 years ago

@esalter Would you be interested in contributing once #48 was resolved? The more the merrier, as they say!

esalter commented 7 years ago

I'd be happy to, just ping me. Thanks!

hackervera commented 6 years ago

That would be fine. I took a stab at it locally and it seems easy

@esalter Is there a branch with your changes?

gdxn96 commented 6 years ago

Any updates on this?

ippa commented 6 years ago

autoformat/correct on save would indeed be nice, just like I get with prettier when working with JS.

ryenski commented 6 years ago

VS Code has a new setting to Run Code Actions on save. It looks like it should be able to run the rubocopAutocorrect command, like this:

"editor.codeActionsOnSave": {
    "ruby.rubocopAutocorrect": true
},

... but it does nothing when saved. I also tried scoping the language, but same result:

"[ruby]": {
    "editor.codeActionsOnSave": {
        "ruby.rubocopAutocorrect": true
    }
},

Anyone else able to get this to work?

rtoma commented 6 years ago

following is working for me:

{
    "editor.formatOnSave": true,
    "editor.formatOnSaveTimeout": 5000,
    "ruby.rubocop.executePath": "path/where/rubocop/is/located",
    "ruby.format": "rubocop",
}
rashmi-tondare commented 5 years ago

following is working for me:

{
    "editor.formatOnSave": true,
    "editor.formatOnSaveTimeout": 5000,
    "ruby.rubocop.executePath": "path/where/rubocop/is/located",
    "ruby.format": "rubocop",
}

I'm unable to get the autocorrection to work. Tried the settings mentioned above, but vscode throws up an error for editor.formatOnSaveTimeout saying Unknown configuration setting. Anyone else facing this issue or am I missing something?

rtoma commented 5 years ago

I'm unable to get the autocorrection to work. Tried the settings mentioned above, but vscode throws up an error for editor.formatOnSaveTimeout saying Unknown configuration setting. Anyone else facing this issue or am I missing something?

To be sure: that piece of config is my in <projectdir>/.vscode/settings.json - works for me with vscode 1.28.2

rdyson commented 5 years ago

I landed here looking for a way to run rubocop on save via Solargraph, and realized the current configuration instructions describe a setup that works correctly. Posting here for others who arrived the same way I did.

Here's what is working for me, VS Code version 1.33.0, ruby-rubocop version 0.7.1.

"editor.formatOnSave": true,
"ruby.format": "rubocop",
"ruby.rubocop.onSave": true,
DanielTetration commented 5 years ago

@rdyson I tried this and ruby.format is an 'unknown configuration setting'

rdyson commented 5 years ago

@DanielTetration Perhaps that setting isn't required — ruby.format is a setting for the Ruby extension.

Try removing the config settings I mention above (all 3 lines) and make sure you have the latest version of the ruby-rubocop extension, which is 0.8.0. Rubocop is invoked on save successfully with this setup, with no additional settings in VS Code config. I verified this by starting with a blank settings.json.

Hope that helps!

boddhisattva commented 4 years ago

For one's future reference, I was able to do it by adding settings present here & have written a little documentation about how to add the settings in VS code here

kirrg001 commented 4 years ago

I have used in my settings.json

    "[ruby]": {
      "editor.formatOnSave": true
    },

Works fine, but the auto-correction kicks in very very late. I have to wait a couple of seconds. Any idea why?

guicattani commented 4 years ago

If anyone has problems with this, I used bundler and it ran fine. Maybe it's some problem with virtual environments like RVM or Rbenv. Cheers!

"ruby.rubocop.useBundler": true

carlows commented 4 years ago

Same issue, even with useBundler it takes seconds to save the file 🤔

benzoh commented 3 years ago

I solved it by writing as follows.

  "[ruby]": {
    "editor.defaultFormatter": "misogi.ruby-rubocop"
  }
ethanneff commented 3 years ago

Can confirm, this worked for me: