ktfmt
ktfmt copied to clipboard
Special-case `trimIndent`?
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?
There's two ways to getting this I can see:
- Better multiline string handling: Not really possible, see discussion in #343.
- 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.
- 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.)
This is a duplicate of #251 I believe.
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.
Ya, your right, sorry I misread.
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?