Repository navigation
Conversation
IntelliJ IDEA and Eclipse leave alone the code between these marker comments, such as a builder chain laid out by hand, and the IntelliJ plugin formatted it anyway. Honouring them in the plugin only would have the IDE keep what the command line, Gradle and Spotless then reformat, so the formatter honours them itself, in every tool alike. The markers are fixed and always on; the IDE's own setting for them does not apply. A region runs from the line of an off marker to the line of the next on marker, or to the end of the file. A marker is a line or block comment that starts with the marker text: the same text in a string, a Javadoc, a Markdown doc comment or later in a comment is none. Partial formatting widens a range to the statements or members around it, so getFormatReplacements leaves out every one that holds code of a region, and markers inside a statement keep all of it. StringWrapper does not reflow, join or indent the literals in those statements, and ModifierOrderer does not reorder the modifiers in a region. Imports are fixed as before. Files without an off marker format as before: of 9,532 files, the old and the new formatter differ only on the 344 with markers, and each of their 707 regions is kept verbatim. Closes #215.
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 #215. Based on
21.X.X.IntelliJ IDEA and Eclipse leave alone the code between
// @formatter:offand// @formatter:on, such as a builder chain laid out by hand, and the IntelliJ plugin formatted it anyway (palantir-java-format#1303 upstream). Honouring the markers in the plugin only would have the IDE keep what the command line, Gradle and Spotless then reformat, andspotlessCheckfail on the file, against the plugin's promise that the IDE and the build produce identical files. So the formatter honours them itself, in every tool alike: the command line, Gradle, Spotless, the native binary, IntelliJ IDEA and Eclipse. The markers are fixed and always on, so the IDE's own setting for them and its marker texts do not apply.// @formatter:offor/* @formatter:off */. The same text in a string, a text block, a Javadoc, a Markdown doc comment, later in a comment or as part of a longer word (@formatter:offline) is none, so code and docs that mention it are formatted as usual.getFormatReplacementsleaves out every one that holds code of a region (JavaOutput.expandToBreakableRegions). Markers inside a statement, such as in an argument list, keep all of that statement as written, and the code around it is formatted.StringWrapperdoes not reflow, join or indent the literals in those statements (Formatter.keptRanges), andModifierOrdererdoes not reorder the modifiers in a region.--lines,--character-rangesand a selection in the IDE: a range in a region changes nothing, one across it formats the code around it.A file without the off marker anywhere takes the same path as before; the check is a
String.contains. The README says so in a paragraph under "What the output looks like". Files with markers format differently, so this goes into a new minor version.Output
The old and the new
allJarformatted 9,532 files in place twice on JDK 21:java.baseandjdk.compilerfrom JDK 21'ssrc.zip, the sources of the 49 library jars in the Gradle cache that hold an off marker (JUnit, Spotless, JGit, Eclipse JDT, Apache Commons, Quarkus, SmallRye and more), and the 80 files of IntelliJ IDEA Community's sources that do.Tests
test-cases-java21/formatterOff: a statement, members, markers inside a statement, a long string outside the markers in that statement, an off marker without an on marker, block-comment markers, strings and modifiers, and marker text that is no marker. The case runner also checks a second run and the partial formatting of the changed lines through the service and the command line.PartialFormattingTest: a range between the markers and one across them, with\n,\rand\r\n.PalantirJavaFormatFormattingServiceTest: Reformat Code keeps a region with the IDE's own marker setting off. It fails with the marker detection of the core switched off../gradlew checkpasses on JDK 21, 1,751 tests, and the plugin's tests pass with--rerunagainst the final core.Not in this PR
.editorconfigasij_formatter_tags_enabled,ij_formatter_off_tagandij_formatter_on_tag, which the formatter could read so that the IDE and the build agree on them.testtask does not take the formatter jars in its sandbox as inputs: after a change in the core alone it stays up to date, and the plugin's tests need--rerunto run against the new formatter.