vscode( vscode-ruby + rubocop ) 如何在保存时自动更正?

vscode( vscode-ruby + rubocop ) how to auto correct on save?

环境

按照 https://github.com/rubyide/vscode-ruby#linters 并安装了所有 gem 并像这样编辑了 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 上,代码突出显示工作正常。
*请注意,您会在问题选项卡中看到已安装的扩展和警告。

问题

我的印象是 vscode-rubyrubocop 会在 file save 上自动更正缩进和 cop 规则,但显然它不会.
如果我想让它像 prettier 一样格式化我的代码,我应该如何设置它?

谢天谢地,这条评论现在已经过时了

-- 不幸的是,当前的 rubocop 扩展无法做到这一点。主要用例是检查 ruby 并在 IDE 中显示视觉提示。

目前在 github 上有一个开放的 issue/feature 请求。查看此问题以查看进度,直到它得到解决。

https://github.com/misogi/vscode-ruby-rubocop/issues/49

根据 vscode-ruby-rubocop GitHub 上的 this comment,您可以使用以下设置:

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

刚刚将它们应用到我本地机器上的用户设置中,它似乎有效。 VS Code 为我的 ruby.rubocop.executePath 设置抛出一个错误,说它不可执行,并且删除该行似乎不会导致显示该错误,并且仍然相应地格式化我的代码。设置较低的超时(我试过 2500)似乎也会破坏保存时的自动格式,所以我建议将其保留在 5000。

为确保自动更正与 ruby-rubocop 一起使用,请添加以下设置 确保 "ruby.rubocop.executePath": "path/where/rubocop/is/located" 设置为默认值 "ruby.rubocop.executePath": "",

将以下内容添加到 vscode

中的 json 文件中
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "",
"ruby.format": "rubocop",

现在,只需添加以下行就足够了:

{
  "ruby.rubocop.onSave": true,
  "editor.formatOnSave": true,
}

我有一段时间遇到这个问题,none 其他解决方案对我有用。
根据一个github comment,解决的是:

I Replace bin in the PATH with wrappers and solved the issue: vscode setting: ruby.rubocop.executePath": "/Users/USER_NAME/.rvm/gems/ruby-2.6.5/wrappers/

注意: 您可以检查上述 none 是否对您有用的另一项有用的事情是检查您的主文件夹中是否有 .rubocop.yml 文件( ~/.rubocop.yml) 并将其删除,这样您就只有项目的 .rubocop.yml 在处理。

我已经尝试了其他人共享的所有选项并且它为我修复了链接(因此当我的代码格式不正确时我会收到警告)但它没有修复缩进或保存时的其他格式。

似乎对我有用的是添加默认格式化程序选项。如果您查看 vscode 中的右下角,您应该会看到一个通知图标,它可能会发出一些有助于您进行配置的警告。对我来说,它正在添加:

"[ruby]": {
  "editor.defaultFormatter": "misogi.ruby-rubocop"
}