ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

Special-case `trimIndent`?

Open kevinb9n opened this issue 2 years ago • 7 comments

This might be an easy close, not sure.

But following an ending """ with .trimIndent() is arguably an inherent part of the language idiom for many many use cases, and I wondered if those could feasibly be kept together on the same line, as a special carve-out.

Current

    loader(
        """
        Foobar
        ${decl.joinToString("") { "$it\n" }}
        """
            .trimIndent())

Seems nicer

    loader(
        """
        Foobar
        ${decl.joinToString("") { "$it\n" }}
        """.trimIndent())

Eh?

kevinb9n avatar Mar 02 '23 18:03 kevinb9n

There's two ways to getting this I can see:

  1. Better multiline string handling: Not really possible, see discussion in #343.
  2. Special casing for trimIndent: We try not to special case any particular functions.

Leaving this open to see if others have thoughts, but I think this is unlikely to work.

davidtorosyan avatar Mar 02 '23 21:03 davidtorosyan

  1. Special casing for trimIndent: We try not to special case any particular functions.

And I do think that's the right way. The case to make here would be that this function is just about as close to a language feature as a thing could be. Like, it's more that it is a special case than that the tool is making it so. Again... "Eh?"

(I mentioned in another issue that I still sometimes guiltily counteract what the formatter does in my code, so I feel some certain responsibility to file those things for consideration, but don't feel too invested in what happens to them after that.)

kevinb9n avatar Mar 02 '23 22:03 kevinb9n

This is a duplicate of #251 I believe.

att14 avatar Mar 13 '23 18:03 att14

I don't believe it is; that one seems to be about reindenting the actual string contents using the signal of trimIndent as a way to know that is safe. This is about how to format the trimIndent call itself.

kevinb9n avatar Mar 13 '23 18:03 kevinb9n

Ya, your right, sorry I misread.

att14 avatar Mar 13 '23 21:03 att14

This here is what Google Java Format does

➜ cat Test.java
class Test {
  public static final String msg1 = """
      foo asfasf asdf asf asdfasf asdf.
      bar asdf sf asdf asdfasdf as.
      baz asdf dsaf asdf sfd .
  """.trimIndent();

  void foo() {
    loader(
        """
        Foobar
        %s
        """.formatted(decl.joinToString(""))
    );
  }
}

➜  java -jar google-java-format-1.22.0-all-deps.jar Test.java
class Test {
  public static final String msg1 =
      """
          foo asfasf asdf asf asdfasf asdf.
          bar asdf sf asdf asdfasdf as.
          baz asdf dsaf asdf sfd .
      """
          .trimIndent();

  void foo() {
    loader(
        """
        Foobar
        %s
        """
            .formatted(decl.joinToString("")));
  }
}

Since we inherit from GJF, maybe we should request for the capability to handle exceptions for text blocks in there.

Could you open an issue in that repo (https://github.com/google/google-java-format) or link an existing one to this issue?

hick209 avatar Jun 05 '24 14:06 hick209