Title: Format the same on every route: formatDiff on the JVM, ranges, and the order of fixing imports by abashev · Pull Request #220 · openjavaformat/open-java-format · GitHub
Open Graph Title: Format the same on every route: formatDiff on the JVM, ranges, and the order of fixing imports by abashev · Pull Request #220 · openjavaformat/open-java-format
X Title: Format the same on every route: formatDiff on the JVM, ranges, and the order of fixing imports by abashev · Pull Request #220 · openjavaformat/open-java-format
Description: Closes #219. formatDiff on 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. Imports. Formatter.formatSourceAndFixImports, behind the Maven plugin and the Spotless step, fixes the imports after formatting, as Main does. The order decided where the comment of a removed import ended up, and whether a blank line stayed under it. Ranges. --output-replacements and FormatterServiceImpl.getFormatReplacements go through one method, Formatter.getFormatAndReflowReplacements, so the in-process service reflows long strings as the bootstrapping and native services do. formatDiff without the native binary left them on one line, and the GitHub Action failed on the result. Strings outside the ranges. StringWrapper.wrap takes 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 Whole files: nothing. Without ranges StringWrapper runs as before. The patched Main gives 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. The Maven plugin and the Spotless step now write what the command line writes. In the 3,865 files of #219 that changes one, CharsetEncoder of JDK 21. A file with an extraneous semicolon between imports, which they used to drop, now fails as on the command line: extraneous semicolon. And import module java./* comment */ base; stays on one line, as the command line leaves it, where they broke it after the dot. Ranges reflow the strings in the code the formatter rewrites for them, and nothing else. They do that more often than the native binary did: its check of whether to reflow looked at the ranges of the input in the formatted text, and missed a long string the formatter had moved into a rewritten statement. Over every line with a string literal and more than 90 characters in 600 of those files, one line at a time: 28 results reflowed, all inside the rewritten code; 21.0.2's --output-replacements reflowed 11, 2 of them outside it. Tests FormatterServiceImplTest holds the in-process service to what Main prints: 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 in FormatterTest fail. StringWrapperTest covers wrapping in ranges and how the ranges move with the replacements. FormatterServicesTest and FormatDiffTest run 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. ModuleImportTest expects for a comment inside a module name what the command line prints: it held formatSourceAndFixImports to the line the old order left broken after the dot. It runs on JDK 25 and later only, which is why the first push failed the JDK 25 to 28 builds and passed on 21. On JDK 21: ./gradlew check passes, 1,435 tests in :open-java-format, 141 in :gradle-open-java-format and 65 in the IntelliJ plugin. ./gradlew test passes as the CI legs run it on JDK 25 (TestKit Gradle 9.3.1) and on JDK 27 (9.8.0). With -PnativeImage=true (macOS aarch64, GraalVM 25) the 11 of FormatterServicesTest and the 7 of FormatDiffTest pass. Not here open-java-format --lines without --output-replacements still reflows the whole file: it fixes the imports between formatting and reflowing, and the ranges cannot be followed through that. The Eclipse plugin formats with Formatter.getFormatReplacements and does not reflow strings. Spotless' own palantirJavaFormat() step never reflows strings; that is Spotless' code.
Open Graph Description: Closes #219. formatDiff on 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. Imports. Formatter.formatSourc...
X Description: Closes #219. formatDiff on 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. Imports. Formatter.formatSourc...
Opengraph URL: https://github.com/openjavaformat/open-java-format/pull/220
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:5f7f3b1e-5bb9-ecb6-7c8f-ae67d7d9afc2 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D0C4:197A56:6ACDE:91FEC:6AFBC76A |
| html-safe-nonce | 7d6c93268db951b5520e1ed145bbc51ee5c73318e3393512daae35c33ab1e8f1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMEM0OjE5N0E1Njo2QUNERTo5MUZFQzo2QUZCQzc2QSIsInZpc2l0b3JfaWQiOiI2OTI2MjY0OTkyOTE2MDMxMzM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a2fbd4e7e40fab147e1029824871b3908769b3bc60cea90c73007ce727c9bf7f |
| hovercard-subject-tag | pull_request:5284967440 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/openjavaformat/open-java-format/pull/220/files |
| twitter:image | https://avatars.githubusercontent.com/u/214171?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/214171?s=400&v=4 |
| og:image:alt | Closes #219. formatDiff on 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. Imports. Formatter.formatSourc... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | d440bda0d812801778b27bfe9f7c1ce9ff22420c73f6f95150c4fcbca311c50b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2c43021aed8695990c86c0eaaedc600bfeffec36 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width