google / google-java-format

Reformats Java source code to comply with Google Java Style.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jwcheong0420 opened this issue · 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 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. :)

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

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

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".