Skip to content

Format only the ranges on the command line, and leave the imports - #234

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/Range-text-output
Nov 18, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/Range-text-output

Conversation

@abashev

@abashev abashev commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #233; merge that first.

open-java-format --lines, --offset and --character-ranges, printing text, now give what applying --output-replacements for the same ranges gives: the code of the ranges formatted and its long strings reflowed, the imports and the rest of the file as they are (ADR-001). Before, they formatted the ranges, then fixed the imports of the whole file and reflowed every long string in it. Without ranges nothing changes.

What changes for users

  • With ranges: unused imports stay and imports are not sorted. An import line inside the ranges is still laid out, as --output-replacements lays it out. Long strings outside the code of the ranges stay as they are.
  • Without ranges: nothing. 1,499 real files come out byte for byte as with the 21.0.2 native binary.

Two tests pinned the old behaviour and change with it: MainTest.importRemovalLines, now linesLeaveTheImports, and PartialFormattingTest.importJavadocNewlines, which no longer gets the import orderer's blank line after the import.

Tests

  • Each case of FormatterCasesTest also expects the same text from --character-ranges as from applying its --output-replacements for the lines formatting changes. CommandLineRangesTest: --lines leaves the imports and a long string on another line. On the code of Reflow only the long strings in the code that formatting the ranges rewrites #233, 25 cases and CommandLineRangesTest fail.
  • JDK 21: ./gradlew check passes; 1,423 tests in :open-java-format. JDK 25 and JDK 28 (28+23-ea): :open-java-format:test passes.

@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 2897566 to 2e3876f Compare November 16, 2026 20:03
@abashev
abashev force-pushed the abashev/Range-text-output branch from 9582a08 to 863e920 Compare November 16, 2026 20:04
@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 2e3876f to 6e28b71 Compare November 16, 2026 20:05
@abashev
abashev force-pushed the abashev/Range-text-output branch from 863e920 to 275270f Compare November 16, 2026 20:05
@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 6e28b71 to 3c36af2 Compare November 16, 2026 20:22
@abashev
abashev force-pushed the abashev/Range-text-output branch from 275270f to acb3e43 Compare November 16, 2026 20:25
With --lines, --offset or --character-ranges and no
--output-replacements, Main formatted the ranges, then fixed the
imports of the whole file and reflowed every long string in it. It
now prints what applying its --output-replacements for the same
ranges gives: the code of the ranges formatted, its long strings
reflowed, the imports and the rest of the file as they are
(ADR-001). Without ranges nothing changes.

Two tests pinned the old way: MainTest.importRemovalLines, now
linesLeaveTheImports, and PartialFormattingTest.importJavadocNewlines,
whose import line is still laid out but no longer gets the import
orderer's blank line after it.

Each case of FormatterCasesTest now also holds the text to the
replacements for the lines it changes, and CommandLineRangesTest
checks --lines on a file with imports and two long strings.
@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 3c36af2 to c799977 Compare November 17, 2026 18:45
@abashev
abashev force-pushed the abashev/Range-text-output branch from acb3e43 to bf770f1 Compare November 17, 2026 18:45
Base automatically changed from abashev/Range-reflow-inside to 21.X.X November 18, 2026 07:09
@abashev
abashev merged commit 440cf7a into 21.X.X Nov 18, 2026
12 of 13 checks passed
@abashev
abashev deleted the abashev/Range-text-output branch November 18, 2026 07:10
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.

1 participant