google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

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

Open jwcheong0420 opened this issue 4 years ago • 3 comments

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 :)

jwcheong0420 avatar Dec 11 '21 14:12 jwcheong0420

@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 avatar Dec 12 '21 10:12 jbduncan

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

jbduncan avatar Dec 12 '21 10:12 jbduncan

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

jovan996 avatar Jun 20 '22 22:06 jovan996

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 avatar Jan 26 '23 00:01 kevinb9n

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

timo-abele avatar Feb 07 '24 15:02 timo-abele