Repository navigation
Conversation
A long line without a quote on it holds no string literal to reflow, so needWrapping and linesNeedWrapping no longer parse the file for a long comment or import. When every replacement is the text it replaces, as for a text block that is indented already, and formatting their ranges changed nothing, wrapOnce returns: the second pass would format the same ranges of the same text, and the AST check would compare the text with itself. The first pass is left out as well when formatting returned the file unchanged, which formatSourceAndFixImports and the command line know. The formatter's output alone is not enough to leave out the first pass, as google-java-format#1533 does: formatting it again lays out a chain of + another way in 5 of 6170 files of the JDK and of dependency sources, and the first pass picks that up. Adds the two tests of google-java-format#1533 for the AST check.
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 #250. Ports the part of google/google-java-format#1533 (still open) that keeps our output identical. Based on
21.X.X; no other open pull request touches these files.What palantir#1533's ideas are worth here differs from upstream. Our
wrapOnceformats the file twice: once before the reflow and once after it. Those two passes cost far more than the AST check that palantir#1533 makes lighter. OnJavaInputAstVisitor.java(150 KB), one format takes about 60 ms, one parse about 4 ms, and the check, two parses withtoString, about 10 ms.What changes
needWrappingandlinesNeedWrappingonly count a long line when it has a quote on it. A long comment, URL or import holds no literal to reflow, and before, it cost a parse of the whole file.wrapOncereturns when every replacement is the text it replaces (typically a text block that is indented already) and the first pass changed nothing. The second pass would format the same ranges of the same text, and the AST check would compare the text with itself.StringWrapper.wrapFormattedleaves the first pass out.Formatter.formatSourceAndFixImportsand the command line call it only when formatting, and fixing imports, returned the file unchanged. Formatting it again then gives the same text, by determinism alone.The output does not change.
Measurements
Median times in one JVM after warm-up,
21.X.X→ this branch:formatSourceAndFixImportsagain on a file with a text block (DebugRenderer.java)wrapon a formatter output with a text block and nothing to changewrapon the 26 files ofjavaformat/javaformatSourceAndFixImports, first run,JavaInputAstVisitor.javawith 10 long stringsThe first run of a file with long strings to wrap gains nothing. Its first pass has to stay, see below.
Not ported
java/lang/ProcessBuilder.java,"builder redirectInput()" + " must be PIPE…" + builder.redirectInput()is laid out one way by the first run and another way by the second, and the first pass picks up the second layout. The test suite did not catch it; the corpus did.wraptakes on the long-string file. Our replacements are not only+chains of literals. They also carry the code and the NON-NLS markers after a chain to the end of its line, re-indented text blocks, and joined literals. The full check is also what checks the second format pass. Saving those milliseconds is not worth weakening it.Source.reflowReplacementsthroughFormatter.getFormatReplacementsintoformat.needWrappinggoes through 131 KB without an early exit in 0.27 ms.Tests
stopsAReflowThatWouldMoveACastToTheFirstPieceandstopsAReflowThatWouldMoveAnInnerClassCreationToTheLastPieceare Excavator: Upgrade dependencies palantir/palantir-java-format#1533's two tests. They pass on21.X.Xtoo: they pin the AST check, which today makes the formatter fail the whole file on such a literal. A long string literal under a cast, a qualified.newor a method reference fails the whole file #252 proposes to leave such literals alone instead, and would change both.longLinesWithoutAQuoteNeedNoWrappingfails on21.X.X.wrapsAFileThatFormattingLeavesAsItIsAsItWrapsAnyOther: on a file that formatting leaves as it is,wrapFormattedgives whatwrapgives.java.base/java,jdk.compiler,java.net.httpandjdk.httpserver, plus files with text blocks or long string lines from the sources jars in the Gradle cache (189 with text blocks). Each file was formatted twice, the second run on the first's output, with the21.X.Xjar and with this branch's. Both runs are byte-for-byte identical, with no errors.With the change, JDK 21:
./gradlew checkpasses.