Repository navigation
Conversation
formatSourceAndFixImports, which the Maven plugin and the Spotless step run, ordered the imports and removed the unused ones before formatting. Main formats first and fixes the imports after, because the ranges of --lines and --offset are offsets into the input. The order decides where the comment of a removed import ends up: ImportOrderer moves a comment along with its import, and RemoveUnusedImports leaves it behind. Fixing them first also dropped an extraneous semicolon between imports, where the command line fails the file (#219). formatSourceAndFixImports now fixes the imports after formatting. FormatterServiceImplTest holds the in-process service to what Main prints for whole files.
--output-replacements, which IntelliJ IDEA and formatDiff with the native binary run, reflowed the whole file once a line in the ranges needed it, and long strings on lines nobody touched changed with it (#219). StringWrapper.wrap takes the ranges and reflows only the literals and text blocks that overlap them. Its passes format and replace text, so the ranges move along with each of them, growing to take in what a replacement rewrites. The ranges it gets are those of the code that formatting the given ranges rewrote, and they also decide whether to reflow at all; that check used to hold the ranges of the input against the lines of the formatted text. Without ranges nothing changes: the whole file is reflowed as before. The method that formats the ranges and reflows them moves to Formatter, for the in-process service to use next.
The Gradle plugin loads FormatterServiceImpl into its own JVM, and its getFormatReplacements never reflowed strings, while the bootstrapping and native services run Main --output-replacements, which does. formatDiff without the native binary left long strings on one line, and the GitHub Action failed on the result (#219). FormatterServiceImpl.getFormatReplacements now returns what --output-replacements prints. FormatterServiceImplTest compares the two for each line of its sources, and FormatterServicesTest and FormatDiffTest run the in-process formatter next to the bootstrapping and native ones, which they never did.
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 #219.
formatDiffon the JVM and with the native binary, IntelliJ IDEA, the Maven plugin and the Spotless step now format a file the way the command line does.Formatter.formatSourceAndFixImports, behind the Maven plugin and the Spotless step, fixes the imports after formatting, asMaindoes. The order decided where the comment of a removed import ended up, and whether a blank line stayed under it.--output-replacementsandFormatterServiceImpl.getFormatReplacementsgo through one method,Formatter.getFormatAndReflowReplacements, so the in-process service reflows long strings as the bootstrapping and native services do.formatDiffwithout the native binary left them on one line, and the GitHub Action failed on the result.StringWrapper.wraptakes the ranges and reflows only the literals and text blocks that overlap them, moving the ranges along with each of its passes. The ranges are those of the code that formatting them rewrote, which is the statement around a changed line where the formatter re-lays it out. Before, one long string in the ranges reflowed every long string in the file.What changes for users
StringWrapperruns as before. The patchedMaingives byte for byte what the 21.0.2 native binary gives on 1,500 files, so the command line, the GitHub Action and the pre-commit hook are unchanged.CharsetEncoderof JDK 21. A file with an extraneous semicolon between imports, which they used to drop, now fails as on the command line:extraneous semicolon.--output-replacementsreflowed 11, 2 of them outside it.Tests
FormatterServiceImplTestholds the in-process service to whatMainprints: whole files, and each line of each file as a range. On the code before this change 7 of the 11 new cases in it and inFormatterTestfail.StringWrapperTestcovers wrapping in ranges and how the ranges move with the replacements.FormatterServicesTestandFormatDiffTestrun the in-process formatter too, next to the bootstrapping and native ones, on a file with a long string on a changed line and another on a line that is not.On JDK 21:
./gradlew checkpasses, 1,435 tests in:open-java-format, 141 in:gradle-open-java-formatand 65 in the IntelliJ plugin; with-PnativeImage=true(macOS aarch64, GraalVM 25) the 11 ofFormatterServicesTestand the 7 ofFormatDiffTestpass. Each of the three commits passes:open-java-format:checkon its own.Not here
open-java-format --lineswithout--output-replacementsstill reflows the whole file: it fixes the imports between formatting and reflowing, and the ranges cannot be followed through that.Formatter.getFormatReplacementsand does not reflow strings.palantirJavaFormat()step never reflows strings; that is Spotless' code.