Google Java Coding Standards
visual studio code - Google Styleguide Java Format for VSCode - Stack Overflow
Google Java style rules fail with code formatted using Google Java Format tool
Codestyle and formatters
I spent some time working on this same problem yesterday. The best and most reliable solution I was able to come up with was to install google-java-format into my path and then use the RunOnSave extension to invoke the binary for files with the .java extension.
Install emeraldwalk/vscode-runonsave:
$ code --install-extension emeraldwalk.RunOnSave
Install google-java-format for your system:
# Example using Homebrew on OS X
$ brew install google-java-format
Configure RunOnSave in Visual Studio Code settings:
{
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.java$",
"cmd": "google-java-format --replace ${file}"
},
],
},
}
I describe more of the methodology in this blog post.
google-java-format requires Java 16.0.1 SDK. You have to install it first.
There is an extension ilkka.google-java-format on the Visual Studio Code marketplace, but it is incompatible with current version.
I have to change it this way:
Open file :
C:\Documents and Settings\<USERNAME>\.vscode\extensions\ilkka.google-java-format-1.0.2\out\extension.jsUpdate function provideDocumentRangeFormattingEdits this way:
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", "--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED", "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED", "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED", "-jar", executablePath, "--lines", `${range.start.line}:${range.end.line}`, "-", ]);In Settings -> Preferences set executablePath to path to jar archive (google-java-format-1.10.0-all-deps.jar)
Enjoy