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

Fix text block indentation when the text block has no prefix

Open dweiss opened this issue 1 year ago • 2 comments

The simplest example is this:

public class A {
  public void foo() {
  String a = """
lorem
ipsum
""";

  String b = """
 lorem
 ipsum
 """;
 }
}

Both text blocks should be aligned after the formatter pass but a isn't:

public class A {
  public void foo() {
    String a =
        """
lorem
ipsum
""";

    String b =
        """
        lorem
        ipsum
        """;
  }
}

I think the StringWrapper used to apply some more logic to actually "deindent" text block strings but that logic isn't needed and is in fact wrong - I added a comment in the patch.

dweiss avatar Sep 24 '24 11:09 dweiss

This is currently intentional, although there's some ongoing internal discussion about what the Google Java Style Guide should say about text blocks, and the 'outdenting' behaviour may change.

I think this is the same issue as https://github.com/google/google-java-format/issues/1087#issuecomment-2016582504

cushon avatar Sep 26 '24 03:09 cushon

Yes, it seems to be the same underlying issue (code condition). I find it hard to understand why text blocks without an indent prefix would have to left without any formatting - this is inconsistent and strange to me. I also think it can lead to destructive formatting changes when used with intellij's plugin (which doesn't apply text block formatting). A subsequent run of the preformatter leaves the code in a different state compared to one run through the formatter. I'd have to come up with an example of this but I'm sure it's possible because I seem to indent those text blocks manually all the time...

dweiss avatar Sep 26 '24 14:09 dweiss

I am going to close this out for now. The 'automatic deindent' behaviour is being revisited, and I will update #1087 when that happens.

cushon avatar Mar 21 '25 23:03 cushon