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

String Templates Formatting for """ Multi-Line is "not nice"

Open vorburger opened this issue 1 year ago • 4 comments

#981 introduced JEP-430 / JEP-459 String Templates support.

This works great (~leaves it alone) for "single line", e.g. var html = STR."<table class=\"\{cssClass}\">";

However when exploring using this for """ Multi-Line it currently formats like this:

return STR."""
<table class="\{
        s(cssClass)}">
<tbody>

</tbody>
</table>
""";

To my eyes, that line break (as if it was a Builder type thing, presumably?) looks fairly "ugly"; I would expect it to simply look like this instead:

return STR."""
<table class="\{s(cssClass)}">
<tbody>

</tbody>
</table>
""";

Do folks agree?

@cushon

vorburger avatar Jan 28 '24 19:01 vorburger

Agreed. The extra line-break happens with 1.19.2, version 1.19.1 does not introduce it.

sormuras avatar Jan 29 '24 07:01 sormuras

I agree this is not ideal. I think the specific change in 1.19.2 was 38de9c4e05bc871496c232c1ccbfc4be06a580e2, prior to that code inside \{ ... } wasn't getting reformatted at all. I think we want to keep formatting complex expressions inside placeholders, but it would be nice to avoid the line breaks in this situation.

cushon avatar Jan 29 '24 16:01 cushon

This works great (~leaves it alone) for "single line", e.g. var html = STR."<table class="{cssClass}">";

Or does it? I just ran into the following, and am surprised I didn't run into this previously (it's likely because I reinstalled tools from scratch, so it's possible my google-java-format changed and upgraded, it looks like it's running the latest 1.19.2 now, as configured, it's possible I may have been running an older version before somehow, I'm not 100% sure anymore):

$ git clone https://github.com/enola-dev/enola.git
$ git checkout 983f7ee
$ ./test.sh
$ source .venv/bin/activate [.fish, or not; depending on your shell]
$ pre-commit run --all-files

Google Java Formatter....................................................Failed
- hook id: pretty-format-java
- exit code: 1

[cwd=/home/vorburger/git/github.com/vorburger/enola] Run command: ('java', '-version')
[return_code=0] | 
	stderr: openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment (build 17.0.9+9-Debian-2)
OpenJDK 64-Bit Server VM (build 17.0.9+9-Debian-2, mixed mode, sharing)

Downloading https://github.com/google/google-java-format/releases/download/v1.19.2/google-java-format-1.19.2-all-deps.jar
[cwd=/home/vorburger/git/github.com/vorburger/enola] Run command: ('java', '--add-exports', 'jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED', '--add-exports', 'jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED', '--add-exports', 'jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED', '--add-exports', 'jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED', '--add-exports', 'jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED', '-jar', '/home/vorburger/.cache/pre-commit/google-java-formatter1.19.2.jar', '--set-exit-if-changed', '--aosp', '--replace', 

(...)

[return_code=1] | 
	stderr: tools/hello/src/main/java/dev/enola/common/hello/Hello.java:30:30: error: illegal escape character
tools/hello/src/main/java/dev/enola/common/hello/Library.java:27:30: error: illegal escape character
web/ui/src/main/java/dev/enola/web/ui/ThingUI.java:72:50: error: illegal escape character
web/ui/src/main/java/dev/enola/web/ui/ThingUI.java:73:34: error: illegal escape character

vorburger avatar Feb 04 '24 23:02 vorburger

@vorburger the illegal escape character error is because you're running the formatting on JDK 17, which predates string template support. Using JDK 21 instead will resolve that error.

cushon avatar Feb 04 '24 23:02 cushon