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

What's the difference between google-java-format plugin and google java style xml file? #695

Closed
jwcheong0420 opened this issue Dec 11, 2021 · 5 comments

Comments

@jwcheong0420
Copy link

Hello,

As far as I know, there are two ways to apply google java style for Intellij

  1. Install google-java-format plugin and enable it
  2. Download google style xml file and import it into File→Settings→Editor→Code Style

I expected the same result, but the two methods seem to produce different results as follows,

  • Location of @Getter when @Getter put on a field

    • Code reformatted with google-java-format plugin(1.13.0.0)
    @Getter private final String value;
    • Code reformatted with imported code style from xml file
    @Getter
    private final String value;
  • Indent of @ApiImplicitParams({})

    • Code reformatted with google-java-format plugin(1.13.0.0)
    @ApiImplicitParams({
      @ApiImplicitParam(name = "name", value = "user name", dataTypeClass = String.class),
    })
    • Code reformatted with imported code style from xml file
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "user name", dataTypeClass = String.class),
    })
  • Line wrapping when using stream

    • Code reformatted with google-java-format plugin(1.13.0.0)
    @RequiredArgsConstructor
    List<Integer> numbers = Arrays.asList(10, 2, 5, 32, 2, 5, 12, 34);
    List<Integer> results =
        numbers.stream()
            .map(number -> number * number)
            .filter(number -> number < 100)
            .collect(Collectors.toList());
    • Code reformatted with imported code style from xml file
    List<Integer> numbers = Arrays.asList(10, 2, 5, 32, 2, 5, 12, 34);
    List<Integer> results = numbers.stream()
        .map(number -> number * number)
        .filter(number -> number < 100)
        .collect(Collectors.toList());

Is it right that the results are different?
I think the reformat result by the plugin is consistent with the google java convention.
Why did the reformat result by xml file come out like this?

Any help would be much appreciated. Thanks :)

@jbduncan
Copy link
Contributor

jbduncan commented Dec 12, 2021

@jwcheong0420 Good questions. My understanding is the GJF command line tool, library, and IDE plugins came after the XML file, and that Google made the CLI tool etc. because Eclipse and IntelliJ weren't flexible enough to meet all the requirements of the Google Java style guide at the time. And I think this is still the case, so the plugin/library/CLI tool should be preferred.

(This explains to me, for example, why the formatting of @ApiImplicitParam in your example differs between the two, since Eclipse and IntelliJ's indentation rules aren't flexible enough.)

I can't remember where I learned all this, though. Can someone from the GJF team confirm my understanding?

As for why the output is different for the @Getter and steam examples, I don't really know why, but this page in the wiki might have the information you're after?

I hope this helps. :)

@jbduncan
Copy link
Contributor

jbduncan commented Dec 12, 2021

I can't explain why the XML files weren't deprecated. 🤷

@jovan996
Copy link

Can we use both of these formatters together(google java format and google java style xml file) or one excludes the other

@kevinb9n
Copy link
Contributor

I'm actually not aware of who maintains the java-google-style.xml file, but indeed as @jbduncan said, this entire formatter exists because the ability of existing tools to format code well is too limited no matter how they're configured.

The one thing you do need it for is to handle import sorting, because the GJF IntelliJ plugin only takes over the "format code" function not "optimize imports".

@kevinb9n kevinb9n closed this as not planned Won't fix, can't repro, duplicate, stale Jan 26, 2023
@timo-abele
Copy link

The one thing you do need it for is to handle import sorting, because the GJF IntelliJ plugin only takes over the "format code" function not "optimize imports".

As of 2023-03-03 (4f5ffd0) this seems to no longer be the case:

When enabled, it will replace the normal Reformat Code and Optimize Imports actions.

https://github.com/google/google-java-format?tab=readme-ov-file#intellij-android-studio-and-other-jetbrains-ides

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

No branches or pull requests

5 participants