Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vorburger opened this issue Jan 28, 2024 · 4 comments
Open

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

vorburger opened this issue Jan 28, 2024 · 4 comments

Comments

@vorburger
Copy link
Member

#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

@sormuras
Copy link
Contributor

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

@cushon
Copy link
Collaborator

cushon commented Jan 29, 2024

I agree this is not ideal. I think the specific change in 1.19.2 was 38de9c4, 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.

@vorburger
Copy link
Member Author

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

@cushon
Copy link
Collaborator

cushon commented Feb 4, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants