【问题标题】:vscode( vscode-ruby + rubocop ) how to auto correct on save?vscode(vscode-ruby + rubocop)如何在保存时自动更正?
【发布时间】:2018-06-10 08:56:08
【问题描述】:

环境

  • vscode 版本 1.19.1 (1.19.1)
  • rubocop (0.52.1)
  • 达尔文 mbp 16.7.0 达尔文内核版本 16.7.0:2017 年 10 月 4 日星期三 00:17:00 PDT;根:xnu-3789.71.6~1/RELEASE_X86_64 x86_64
  • ruby 2.3.5p376(2017-09-14 修订版 59905)[x86_64-darwin16]

关注 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 会自动更正文件保存上的缩进和复制规则,但显然它不会。
如果我希望它像prettier 这样格式化我的代码,我应该如何设置它?

【问题讨论】:

  • 即使我在 vs 代码中寻找相同的东西。
  • 您的配置也有帮助。我想给你买杯啤酒。几个月的争论。

标签: ruby visual-studio-code rubocop


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我有一段时间遇到这个问题,但没有其他解决方案适合我。
    根据github comment,解决的是:

    我将 PATH 中的 bin 替换为 wrappers 并解决了问题: vscode设置: ruby.rubocop.executePath": "/Users/USER_NAME/.rvm/gems/ruby-2.6.5/wrappers/

    注意:如果上述方法都不适合您,您可以检查的另一件有用的事情是检查您的主文件夹中的 .rubocop.yml 文件 (~/.rubocop.yml) 并将其删除,这样您就只有您的项目的 .rubocop.yml 正在处理中。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        为确保自动更正适用于 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",
        

        【讨论】:

          【解决方案5】:

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

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

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

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

          【讨论】:

          • 这可以通过 rubocop 扩展和 VS Code "editor.formatOnSave": true 设置来完成。
          【解决方案6】:

          根据 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。

          【讨论】:

          • 对于我的环境设置 "editor.formatOnSaveTimeout": 5000 成功了,我将它设置为 1500 但它不起作用。另外"ruby.rubocop.executePath" 似乎无法识别,也许它已经被删除了?
          • 这是修复一切的神奇设置,应该是安装 Visual Studio 时的默认设置:"editor.formatOnSave": true
          • "editor.formatOnSaveTimeout": 5000 也解决了我的问题。欢呼
          猜你喜欢
          • 2018-10-28
          • 2021-08-19
          • 1970-01-01
          • 1970-01-01
          • 2023-01-24
          • 2021-03-01
          • 2022-11-09
          • 2020-09-01
          • 1970-01-01
          相关资源
          最近更新 更多