Vscode-java: Google Java Format

Created on 24 Jan 2018  路  8Comments  路  Source: redhat-developer/vscode-java

Is there a way to setup formatting with Google Java Format?
Or an entirely new extension is needed which will run the jar formatter on opened file?
If so, what would be the best way to implement this as an extension, e.g running external jar?

question

Most helpful comment

I've found a solution that works ok.

https://code.visualstudio.com/docs/editor/tasks

For that to work make sure java-google-format can be executed from terminal. In my case:

  • Installing via brew install google-java-format does the trick.
  • Create a tasks.json file in .vscode directory inside your workspace with necessary commands.
  • Run from vscode command palette by running task and finding format task

Here's my tasks.json file:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run google java format",
      "type": "shell",
      "command": "google-java-format --replace ${file}"
    }
  ]
}

All 8 comments

This would be cool.

I'm looking for a way of configuring the formatter and the code organizer with similar settings to eclipse/intellij even better if I can import the files directly. Seems like this extension comes with predefined settings which cannot be changed.

The only way you can set it up currently is to save the google java format as eclipse formatter settings, in your .settings/org.eclipse.jdt.core.prefs.

I added an entry to the wiki

I've found a solution that works ok.

https://code.visualstudio.com/docs/editor/tasks

For that to work make sure java-google-format can be executed from terminal. In my case:

  • Installing via brew install google-java-format does the trick.
  • Create a tasks.json file in .vscode directory inside your workspace with necessary commands.
  • Run from vscode command palette by running task and finding format task

Here's my tasks.json file:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Run google java format",
      "type": "shell",
      "command": "google-java-format --replace ${file}"
    }
  ]
}

thanks @fbricon @azdanov for the swift response

Would be nice to have this built into the extension so it can work with "format on save".

@azdanov Could you go into a bit more detail about the process you found? I am running on a Windows system what exactly is the brew program and is is that the required method to install the formatter?

There is a new _Google Java Format_ extension released a couple of months ago available in Visual Studio Marketplace whose source code is hosted on GitLab.

To make it work you need to:

  1. download the latest version of google-java-format all-deps jar

  2. move the jar in a folder (e.g. /opt/)

  3. enter the full path to the jar in gjf.jarPath of your user or workspace settings as follows:

    "gjf.jarPath": "/opt/google-java-format-1.9-all-deps.jar"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ulongx picture ulongx  路  4Comments

yaohaizh picture yaohaizh  路  4Comments

ronniehicks picture ronniehicks  路  3Comments

xriu picture xriu  路  4Comments

sfariaNG picture sfariaNG  路  3Comments