Repository navigation
Leave alone a long literal under a cast, .new or method reference - #256
Merged
abashev merged 1 commit intoNov 23, 2026
Merged
Conversation
Reflowing a long string literal failed the whole file when the
literal sat under something that binds tighter than `+`: a cast, the
enclosing expression of a qualified `.new`, or a method reference.
Split into "a" + "b", the literal left the cast only its first piece,
as in ((Object) "a") + "b", and the `.new` or the `::` only its last.
The AST check at the end of wrapOnce caught this and threw, so the
formatter printed "Something has gone terribly wrong" and formatted
nothing in the file.
LongStringsAndTextBlockScanner already left alone the expression of a
member select ("...".length()) for this reason. It now skips the
three other contexts too, and such a literal stays on its line as the
formatter lays it out. A literal in parentheses, as in
(Object) ("..."), is still reflowed inside them.
abashev
deleted the
abashev/a-long-string-literal-under-a-cast-a-qualified
branch
November 23, 2026 09:43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #252. Based on
21.X.X, after #254; no other open pull request touches these files.A long string literal under a cast, as the enclosing expression of a qualified
.new, or as the qualifier of a method reference made the formatter fail the whole file.StringWrappersplit it into"a" + "b", which leaves the cast only the first piece and the.newor the::only the last. The AST check at the end ofwrapOncecaught that and threw "Something has gone terribly wrong".LongStringsAndTextBlockScanner.visitLiteralalready skipped a literal that is the expression of a member select,"…".length(), for the same reason. That check is nowbindsTighterThanPlus, which also skips the three contexts above. As the issue proposes, the literal is left alone rather than wrapped in parentheses that nobody wrote.What changes for users
The file from the issue now formats:
The first three literals come out as
--skip-reflowing-long-stringslays them out, on a line of their own past the column limit. A literal in parentheses,(Object) ("…"), is still reflowed inside them.Nothing else changes: before, such a literal either had nothing to reflow or made
wrapOncethrow."…"[0]and unary operators such as-"…"bind tighter than+too, but they do not compile on aString, so they are left out.Tests
stopsAReflowThatWouldMoveACastToTheFirstPieceandstopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPiece, which Skip the StringWrapper work that cannot change the file #254 ported from Optimize StringWrapper fast-path, parse reuse, and AST verification google/google-java-format#1533 to pin the exception, now expect the input back.stopsAReflowThatWouldMoveAMethodReferenceToTheLastPiecedoes the same for::.stringwrapper/longLiteralsUnderACastANewOrAMethodReference.caseis the file from the issue plus a cast of a literal in parentheses: the three literals stay whole, and the other two are reflowed.Without the fix, all four fail with "Something has gone terribly wrong".
With the change, JDK 21:
./gradlew checkpasses.