Answer a question

Is there a good extension or proven methodology to automatically format Java code in VSCode to adhere to Google's Java Styleguide? Nothing from the list of links below seems to work for my set-up. I get a soft-fail and nothing changes about formatting.

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

Answers

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.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐