VSCode的Google Styleguide Java格式

是否有很好的扩展或行之有效的方法来自动格式化VSCode中的Java代码以遵守Google的Java样式指南?下面的链接列表对我的设置似乎无效。我收到软失败,格式化没有任何变化。

Version: 1.39.2 (user setup)
Commit: 6ab598523be7a800d7f3eb4d92d7ab9a66069390
Date: 2019-10-15T15:35:18.241Z
Electron: 4.2.10
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Windows_NT x64 10.0.16299

google-java-format
GitHub - Formatting VSCode : 419
GitHub - Formatting VSCode : 450
GitHub - Formatting VSCode : 663
Formatter Settings
Eclipse Java Google Style XML
VS Java Editing - Main Page
VSCode Extension - Checkstyle

jiayouboa 回答:VSCode的Google Styleguide Java格式

昨天我花了一些时间解决这个问题。我能想到的最好,最可靠的解决方案是将google-java-format安装到我的路径中,然后使用RunOnSave扩展名来调用扩展名为.java的文件的二进制文件。

安装emeraldwalk / vscode-runonsave:

$ code --install-extension emeraldwalk.RunOnSave

为您的系统安装google-java-format:

# Example using Homebrew on OS X
$ brew install google-java-format

在Visual Studio代码设置中配置RunOnSave:

{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.java$","cmd": "google-java-format --replace ${file}"
      },],},}

我在this blog post中描述了更多的方法。

,

尝试配置以下设置:

"java.configuration.updateBuildConfiguration": "automatic","checkstyle.enable": true,"checkstyle.configurationPath": "config/checkstyle/checkstyle.xml","checkstyle.checkstylePath": "C:/dev/tools/checkstyle-8.8-all.jar"
,

google-java-format 需要 Java 16.0.1 SDK。您必须先安装它。

Visual Studio Code 市场上有一个扩展 ilkka.google-java-format,但它与当前版本不兼容。

我必须这样改变它:

  1. 打开文件:C:\Documents and Settings\<USERNAME>\.vscode\extensions\ilkka.google-java-format-1.0.2\out\extension.js

  2. 以这种方式更新函数provideDocumentRangeFormattingEdits

    return new Promise((resolve,reject) => {
        let stdout = "";
        let stderr = "";
        let child = cp.spawn("java.exe",[
                "--add-exports","jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED","--add-exports","jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED","jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED","jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED","jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED","-jar",executablePath,"--lines",`${range.start.line}:${range.end.line}`,"-",]);
    
  3. Settings -> Preferences 中将 executablePath 设置为 jar 存档的路径 (google-java-format-1.10.0-all-deps.jar)

享受

本文链接:https://www.f2er.com/3165172.html

大家都在问