环境
遵循https://github.com/rubyide/vscode-ruby#linters并安装所有的gems,并像这样编辑settings.json。
{
"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}"
}
}
在vscode上,代码高亮显示很好。
*请注意,您可以在problem选项卡中看到已安装的扩展和警告。
问题
我认为vscode-ruby
和rubocop
会自动更正文件上的缩进和cop规则,但显然没有。
如果我希望它像prettier
那样格式化我的代码,我应该如何设置它?
发布于 2018-12-14 15:40:32
在vscode-rubocop 这句话 GitHub上,您可以使用以下设置:
{
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "path/where/rubocop/is/located",
"ruby.format": "rubocop",
}
只需将它们应用到本地框上的用户设置中,它就可以工作了。VS代码为我的ruby.rubocop.executePath
设置抛出了一个错误,说它不是可执行的,删除行似乎不会导致错误的显示,并且仍然相应地格式化我的代码。设置一个较低的超时(我尝试了2500次)似乎也打破了自动格式的储蓄,所以我建议把它保持在5000。
发布于 2021-10-27 16:45:38
我已经尝试过其他人共享的所有选项,它为我修复了链接(因此,当我的代码没有正确格式化时,我会收到警告),但是它没有修复保存时的缩进或其他格式。
似乎对我有用的是添加一个默认的格式化程序选项。如果您查看vscode中的右下角,您应该会看到一个通知图标,它可能会添加一些可以帮助您配置的警告。对我来说,它是在补充:
"[ruby]": {
"editor.defaultFormatter": "misogi.ruby-rubocop"
}
发布于 2021-02-03 12:02:05
现在,仅仅添加以下几行就足够了:
{
"ruby.rubocop.onSave": true,
"editor.formatOnSave": true,
}
https://stackoverflow.com/questions/48030698
复制相似问题