Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use google-format-code instead of eclipse ? #663

Open
hexa00 opened this issue Oct 4, 2018 · 9 comments · May be fixed by #3062
Open

Use google-format-code instead of eclipse ? #663

hexa00 opened this issue Oct 4, 2018 · 9 comments · May be fixed by #3062

Comments

@hexa00
Copy link

hexa00 commented Oct 4, 2018

Is there a way to use google-format-code instead of the eclipse formatter with vscode-java ?

At the moment i'm forced to use it with the SaveAndRun extension, but this has issues like me doing changes while the file is saved and having to reconcile the buffer as it tries to reload.

I could work on a PR for this if you give a few stating points on how to implement this, it would help ?

Thanks,

@gorkem gorkem added the question label Oct 5, 2018
@gorkem
Copy link
Contributor

gorkem commented Oct 5, 2018

Take a look at eclipse-jdtls/eclipse.jdt.ls#640 I think it provides what you need.

@hexa00
Copy link
Author

hexa00 commented Oct 5, 2018

@gorkem Yeah unfortunately even using like:

{
"java.format.enabled": true,
"java.saveActions.organizeImports": true,
"java.format.onType.enabled": true,
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml"
}

The style is still different than what I get when I run google-format-code :(

@snjeza
Copy link
Contributor

snjeza commented Oct 5, 2018

@hexa00 Try to add:

"editor.tabSize": 2,
"editor.detectIndentation": false,
"editor.insertSpaces": true,

@hexa00
Copy link
Author

hexa00 commented Oct 5, 2018

@snjeza Thanks but no luck it's like eclipse will format:

Like this:

  List<String> someIds = foo.stream()
        .map(id -> Something.Func(ABC, id))
        .collect(Collectors.toList());

But google-format-code will do:

 List<String> someIds =
        foo
            .stream()
            .map(id -> Something.Func(ABC, id))
            .collect(Collectors.toList());

@gorkem
Copy link
Contributor

gorkem commented Oct 6, 2018

I think Eclipse formatter does not have an option to insert newline before '.' references.
I am not sure if there is any value over SaveAndRun extension to making this internal to vscode-java. What is the issue that you are having with SaveAndRun?

@hexa00
Copy link
Author

hexa00 commented Oct 6, 2018

@gorkem The main problem is that there's a delay to between the save and reload of the formatted code in vscode. So if I save and continue to type then I have modifications that are not in the reloaded file causing vscode to prompt me to compare the unsaved buffer and the reload file. It can get quite annoying :(

@anthonyvdotbe
Copy link

anthonyvdotbe commented Mar 22, 2020

edit: @fbricon will you please relabel? Thanks in advance

@gorkem will you please label this as an enhancement rather than a question?

@doggy8088
Copy link
Contributor

I think embed google-java-format into language server will be the best option. Just like the IntelliJ IDEA's google-java-format plugin did.

@abceleung
Copy link

abceleung commented Feb 21, 2024

Bumping because Google released binary versions of google-java-format this week.

Not sure if they are useful to this extension (the binaries are huge). But for future readers, you can just download the binary and put it on PATH (rename it to google-java-format or google-java-format.exe if on Windows), then run it with the vscode-runonsave extension.

Example config:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants