Skip to content

Reflow only the long strings in the code that formatting the ranges rewrites - #233

Merged
abashev merged 3 commits into
21.X.Xfrom
abashev/Range-reflow-inside
Nov 18, 2026
Merged

abashev merged 3 commits into
21.X.Xfrom
abashev/Range-reflow-inside

Conversation

@abashev

@abashev abashev commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Builds on #232; merge that first. Part of #219.

Formatting a part of a file reflows only the long strings in the code that formatting it rewrites. Before, once one line in the ranges needed reflowing, every long string in the file was reflowed, on lines nobody touched. This is what IntelliJ IDEA, formatDiff and every other caller of --output-replacements or FormatterService.getFormatReplacements get, alike.

  • StringWrapper.wrap with ranges reflows only the literals and text blocks that overlap them, and moves the ranges along with each of its passes (moveRanges). Without ranges it runs as before.
  • Formatter.getFormatAndReflowReplacements hands it the ranges of the code that formatting rewrote, which also decide whether to reflow at all; that check used to hold the ranges of the input against the formatted text.
  • The SPI says it: FormatterService.getFormatReplacements formats the ranges and reflows the long strings in the code it rewrites; the imports, and the strings elsewhere, stay.

What changes for users

  • Whole files: nothing. The command line, the GitHub Action, the pre-commit hook and the build plugins write what 21.0.2 wrote: 1,499 real files byte for byte the same as the 21.0.2 native binary's output.
  • Ranges leave long strings outside the code they rewrite. They reflow more often than 21.0.2 did inside it, since the check now looks at the rewritten code: over every line with a string literal and more than 90 characters in 600 real files, one line at a time, 28 results reflowed, all inside the rewritten code; 21.0.2 reflowed 11, 2 of them outside it.

Tests

  • StringWrapperTest: wrapping in ranges, and how ranges move with the replacements.
  • FormatterServicesTest.reflows_the_long_string_of_the_range and FormatDiffTest.reflows_the_long_string_of_a_changed_line, from Reflow long strings in formatDiff on the JVM, as with the native binary #232, get a second long string on a line that is not changed: it stays, with the in-process, bootstrapping and native formatters. On the code of Reflow long strings in formatDiff on the JVM, as with the native binary #232 both fail with all three.
  • JDK 21: ./gradlew check passes; 1,421 tests in :open-java-format. JDK 25 and JDK 28 (28+23-ea): :open-java-format:test passes. -PnativeImage=true (macOS aarch64, GraalVM 25): :open-java-format-jdk-bootstrap:test 11 of 11, :gradle-open-java-format:test 156 of 156.

@abashev
abashev force-pushed the abashev/Range-reflow-in-process branch from dae406a to ed3bcf8 Compare November 16, 2026 20:03
@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-reflow-in-process branch from ed3bcf8 to 33fe8c1 Compare November 16, 2026 20:05
@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-reflow-in-process branch from 33fe8c1 to 94e5117 Compare November 16, 2026 20:18
@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 6e28b71 to 3c36af2 Compare November 16, 2026 20:22
Base automatically changed from abashev/Range-reflow-in-process to 21.X.X November 17, 2026 18:01
--output-replacements, which IntelliJ IDEA and formatDiff run, and now
the in-process service too, 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 tests of FormatterServicesTest and FormatDiffTest that reflow a
long string on a changed line, with each of the in-process,
bootstrapping and native formatters, get another long string, on a
line that is not changed: it stays as it is.
@abashev
abashev force-pushed the abashev/Range-reflow-inside branch from 3c36af2 to c799977 Compare November 17, 2026 18:45
@abashev
abashev merged commit 4b0b173 into 21.X.X Nov 18, 2026
31 of 33 checks passed
@abashev
abashev deleted the abashev/Range-reflow-inside branch November 18, 2026 07:09
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