Skip to content

Keep the code between @formatter:off and @formatter:on as written - #271

Open
abashev wants to merge 1 commit into
21.X.Xfrom
abashev/intellij-plugin-formats-code-between-formatter-o
Open

abashev wants to merge 1 commit into
21.X.Xfrom
abashev/intellij-plugin-formats-code-between-formatter-o

Conversation

@abashev

@abashev abashev commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Closes #215. Based on 21.X.X.

IntelliJ IDEA and Eclipse leave alone the code between // @formatter:off and // @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, and spotlessCheck fail 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.

  • What stays as written. From the line with an off marker to the line with the next on marker, or to the end of the file; the on marker itself is formatted. A marker is a line or block comment that starts with the marker text, // @formatter:off or /* @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.
  • Whole statements. Partial formatting widens a range to the statements or members around it, so getFormatReplacements leaves 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.
  • Strings and modifiers. StringWrapper does not reflow, join or indent the literals in those statements (Formatter.keptRanges), and ModifierOrderer does not reorder the modifiers in a region.
  • Partial formatting, --lines, --character-ranges and a selection in the IDE: a range in a region changes nothing, one across it formats the code around it.
  • Imports are sorted and pruned as before, as IntelliJ's Optimize Imports does despite the markers.

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 allJar formatted 9,532 files in place twice on JDK 21: java.base and jdk.compiler from JDK 21's src.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.

  • No file without an off marker differs.
  • 344 of the 448 files with one differ, and a script finds each of their 707 regions verbatim in the output.
  • A second run changes the same 47 files with the old and the new formatter: its output is not always a fixed point, as before.

Tests

  • Eight case files under 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, \r and \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 check passes on JDK 21, 1,751 tests, and the plugin's tests pass with --rerun against the final core.

Not in this PR

  • Markers of one's own, or none. IntelliJ IDEA writes its marker settings to .editorconfig as ij_formatter_tags_enabled, ij_formatter_off_tag and ij_formatter_on_tag, which the formatter could read so that the IDE and the build agree on them.
  • The docs site. A page on openjavaformat.dev, in openjavaformat/docs.
  • The plugin's test task 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 --rerun to run against the new formatter.

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

Successfully merging this pull request may close these issues.

IntelliJ plugin formats code between formatter off/on marker comments

1 participant