Reputation: 2207
followed https://github.com/rubyide/vscode-ruby#linters and installed all gems and edited the settings.json like this.
{
"ruby.rubocop.executePath": "/Users/ac/.rbenv/shims/",
"ruby.rubocop.onSave": true,
"ruby.lint": {
"ruby": {
"unicode": true //Runs ruby -wc -Ku
},
"reek": true,
"rubocop": {
"lint": true,
"rails": true
},
"fasterer": true,
"debride": {
"rails": true //Add some rails call conversions.
},
"ruby-lint": true
},
"ruby.locate": {
"include": "**/*.rb",
"exclude": "{**/@(test|spec|tmp|.*),**/@(test|spec|tmp|.*)/**,**/*_spec.rb}"
}
}
On vscode, code highlighting is working fine.
*just to note, you see the extensions installed, and warnings in the problem tab.
I was under the inpression that vscode-ruby
and rubocop
would auto-correct indentations and cop rules on file save, but apparently it doesn't.
If I want it to format my code like prettier
, how should I set this up?
Upvotes: 29
Views: 47411
Reputation: 1
I'll add to the answer @Artur INTECH
also need to add in settings.json option "rubyLsp.formatter": "rubocop"
for example my settings.json looks like that:
{
"editor.tabSize": 2,
"files.trimTrailingWhitespace": true,
"ruby.useLanguageServer": true,
"ruby.intellisense": "rubyLocate",
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.tabSize": 2,
"editor.insertSpaces": true,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"editor.rulers": [
120
],
"editor.semanticHighlighting.enabled": true,
"editor.formatOnSave": true,
"editor.formatOnType": true
},
"terminal.integrated.scrollback": 10000000,
"explorer.confirmDelete": false,
"explorer.confirmDragAndDrop": false,
"editor.renderWhitespace": "all",
"rubyLsp.formatter": "rubocop",
"files.autoSave": "onWindowChange",
"workbench.colorTheme": "Spinel",
"window.zoomLevel": 1,
"security.workspace.trust.untrustedFiles": "open",
}
Upvotes: 0
Reputation: 7276
The extension referred in the question under vscode-ruby
(which is in fact rebornix.Ruby
) is deprecated and no longer maintained.
A modern way is to use Ruby LSP extension from Shopify (Shopify.ruby-lsp
) and add "editor.formatOnSave": true
. See more detailed answer on setting up the autocorrection.
Upvotes: 2
Reputation: 1885
I struggled with the extension ruby-rubocop, last updated in Dec 2021. It seems it's still using the auto-correct command --autocorrect
while Rubocop now uses --auto-correct
. I switched to another extension, ruby-rubocop-revived, which fixed my auto-correct issue.
It also introduces the useServer
option that speeds up the execution.
Here are the settings I used:
# .vscode/settings.json
"ruby.format": "rubocop",
"ruby.rubocop.onSave": true,
"ruby.rubocop.useServer": true,
"[ruby]": {
"editor.defaultFormatter": "rebornix.ruby"
},
Upvotes: 1
Reputation: 101
I've tried all the options other people shared and it fixed linking for me (so I would get warnings when my code wasn't formatted correctly) but it didn't fix indentation or other formatting on save.
What seemed to work for me is adding a default formatter option. If you look at the bottom right corner in vscode you should see a notification icon which might throw in a few warnings that can help with your config. For me it was adding:
"[ruby]": {
"editor.defaultFormatter": "misogi.ruby-rubocop"
}
Upvotes: 10
Reputation: 2037
I had this problem for a while and none of the other solutions worked for me.
According to a github comment, what solved was:
I Replace
bin
in the PATH withwrappers
and solved the issue: vscode setting:ruby.rubocop.executePath": "/Users/USER_NAME/.rvm/gems/ruby-2.6.5/wrappers/
Note: Another useful thing you could check if none of the above worked for you is to check your home folder for a .rubocop.yml
file (~/.rubocop.yml
) and delete it so you only have your project's .rubocop.yml
working on.
Upvotes: 4
Reputation: 604
Now, it's enough just adding these lines:
{
"ruby.rubocop.onSave": true,
"editor.formatOnSave": true,
}
Upvotes: 9
Reputation: 41
To make sure autocorrection works with ruby-rubocop add below settings make sure "ruby.rubocop.executePath": "path/where/rubocop/is/located", is set to default "ruby.rubocop.executePath": "",
Add the following to your json file in vscode
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "",
"ruby.format": "rubocop",
Upvotes: 1
Reputation: 371
This comment is now thankfully outdated
-- Unfortunately this cannot be done with the current rubocop extension. The primary use case is to lint your ruby and show visual cues in the IDE.
It is currently an open issue/feature request on github. View this issue to see progress until it is resolved.
https://github.com/misogi/vscode-ruby-rubocop/issues/49
Upvotes: 0
Reputation: 4420
Per this comment on the vscode-ruby-rubocop GitHub, you can use the following settings:
{
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "path/where/rubocop/is/located",
"ruby.format": "rubocop",
}
Just applied them to my user settings on my local box and it appears to work. VS Code was throwing an error for my ruby.rubocop.executePath
setting saying it wasn't executable, and removing the line appears to not cause that error to show, and still formats my code accordingly. Setting a lower timeout (I tried 2500) also seems to break auto format on saving, so I'd suggest leaving it at 5000.
Upvotes: 26