google / google-java-format

Reformats Java source code to comply with Google Java Style.
Other
5.49k stars 848 forks source link

Excess indentation with method-receiving text blocks #1081

Closed Stephan202 closed 3 months ago

Stephan202 commented 4 months ago

The new fmt-maven-plugin release comes with an upgrade from GJF 1.19.2 to GJF 1.21.0. With this upgrade it appears that text blocks ~inside lambda expressions~ that are the receiver of a method are indented too far to the right. See PicnicSupermarket/error-prone-support@a2196ee (#1069) for an example. This is what that code would look like if the text block contents are shifted to the right to match the new position of the text block start marker:

unexpectedMatchesByLineNumber.entries().stream()
    .map(
        e ->
                """
                Rule `%s` matches on line %s, while it should match in a method named \
                `test%s`."""
                .formatted(e.getValue(), e.getKey(), e.getValue()))

I.e.: an amount of indentation that better matches AOSP rather than GOOGLE style. I assume that this is a side-effect of ce3cb59a8d649359a8e6e7fcc5f2f21bb79b3df1.

(Apologies for not providing a 100% stand-alone reproduction case; can do that ~this weekend if desired.)

cushon commented 3 months ago

Thanks for the report, I agree this is an issue. I don't think it's specific to lambda bodies, in general if text blocks show up as the receiver of a method call they are getting a surprising continuation indent:

class T {
  {
    System.err.printf("hello %s\n", world);
        """
        hello %s
        """
        .formatted("world");
  }
}
Stephan202 commented 3 months ago

Right, makes sense! Should have played around with the example a bit more. I'll update the ticket description :+1: