昨天我花了一些时间解决这个问题。我能想到的最好,最可靠的解决方案是将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,但它与当前版本不兼容。
我必须这样改变它:
-
打开文件:C:\Documents and Settings\<USERNAME>\.vscode\extensions\ilkka.google-java-format-1.0.2\out\extension.js
-
以这种方式更新函数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}`,"-",]);
在 Settings -> Preferences 中将 executablePath 设置为 jar 存档的路径 (google-java-format-1.10.0-all-deps.jar)
享受
本文链接:https://www.f2er.com/3165172.html