Title: Join adjacent string literals that share a line by abashev · Pull Request #176 · openjavaformat/open-java-format · GitHub
Open Graph Title: Join adjacent string literals that share a line by abashev · Pull Request #176 · openjavaformat/open-java-format
X Title: Join adjacent string literals that share a line by abashev · Pull Request #176 · openjavaformat/open-java-format
Description: Fixes #148. What When a concatenation of string literals ends up on one line, the + between them stays. The string wrapping pass only looks at lines past the column limit, so the output depends on where the author broke the string. // 21.X.X throw new SafeIllegalArgumentException( "Invalid recipient field - contains multiple " + "foreign key properties.", UnsafeArg.of("field", maybeField)); foo("one " + "two " + value + "three " + "four"); // this PR throw new SafeIllegalArgumentException( "Invalid recipient field - contains multiple foreign key properties.", UnsafeArg.of("field", maybeField)); foo("one two " + value + "three four"); What the pass does StringWrapper now joins each run of adjacent string literals that share a line into one literal, after the reflow and the text block passes, whether the author wrote them so or the layout brought them together. The code is then laid out again around the joined literal, so a call whose argument now fits goes onto one line. A line with a quote on both sides of a + sets the pass off, in needWrapping and in linesNeedWrapping (the --output-replacements path), and the pass returns at once when it finds nothing to do. Literals stay apart: on different lines, which is wrapping done on purpose, by the author or by the reflow; with a comment between them; at a text block (literals after one still join among themselves); on a line that ends in a line comment, which may speak of them as they are written: a NON-NLS marker counts the literals of its line (#23), and a table of literals has a comment for each row; where the left one ends in an octal escape of one or two digits and the right one starts with an octal digit: "\1" + "2" is two characters, "\12" one; where either holds a Unicode escape. A run that a reflowed chain or a text block overlaps is left to that pass. Differences from palantir#1781 The code is Zayan Khan's, from palantir#1781 (closed there unmerged), which addresses palantir#68; the commit keeps the author. Two rules are new here: A line comment at the end of the line. Upstream joins those literals. The JDK's java/util/LocaleISOData.java holds tables of rows such as "aa" + "aar" // Afar, 437 of them, which became "aaaar" // Afar. The rule also keeps our NON-NLS markers with their literals: without it the ojf-issue-23 cases fail. It changes no other JDK file. Unicode escapes. Upstream's octal check reads the raw text, so "\u005c1" + "2", a backslash written as a Unicode escape, became "\u005c12", one character instead of two. The AST check of the pass rejects that, and the whole file fails to format. The goldens and unit tests became case files, and the changelog entry is left out. Tests stringwrapper/adjacentLiteralsAreJoined.case: an argument the formatter brings onto one line, runs around other operands of the +, parentheses, a two-line chain the formatter pulls onto one line, escapes copied as written (\t, \", \\ before a digit, a three-digit octal escape, \1 before 8), literals after a text block, an annotation and a case label. stringwrapper/adjacentLiteralsThatStayApart.case: a comment between, a line comment at the end of the line (a table and a call), short octal escapes, Unicode escapes. testdata/NON-NLS-* and testdata/palantir-10-*, in all three line endings: the expected outputs join their literals, and nothing else in them changes. Upstream's unit tests of joiningWouldChangeEscapes and mayHaveJoinableLiterals were not kept, because the cases already fail when either breaks. I checked that by taking out each rule in turn: the line comment rule, the text block rule, the Unicode rule, the octal rule and the trigger each fail at least one case. Taking out the early return changes no output. A plain formatSource leaves all 889 after blocks as they are. Tests on JDK 21: FormatterCasesTest 887 → 889; 1410 tests in the module, 1471 in all modules. JDK 21 corpus Of the 15,747 files, 787 format differently, in 2,303 places, and each is a join: with the + between two literals taken out and blanks ignored, both versions read the same. 737 files join on lines that stay as they are, for example SSLLogger.fine("Discard invalid record: " + "too small record to hold a handshake fragment"). In 167 the code is then laid out again, mostly a call whose argument now fits on its line, as in java/lang/StackWalker.java: - throw new UnsupportedOperationException( - "This stack walker " + "does not have RETAIN_CLASS_REFERENCE access"); + throw new UnsupportedOperationException("This stack walker does not have RETAIN_CLASS_REFERENCE access"); 221 places join literals with escapes, such as "\"" + ",offset=" into "\",offset=". The pass compares the folded values of the code before and after, and no file failed. The files that change on a second run go down from 67 to 48, and none of them is new. In 19 files, a second run used to move pieces of a chain around, and those pieces are now joined in the first run. No slowdown is visible: 42–48 s with the change and 64 s without, though on a machine under load (load average 15–100), so this is not a clean benchmark. Partial formatting Joins reach as far as the reflow does today, which is the whole file: --lines and --character-ranges: the string pass runs over the whole file, so formatting a few lines can join literals elsewhere in it. --output-replacements (IntelliJ): the pass runs when a line in the ranges needs it, and then answers with one replacement for the whole file. A line in the ranges with joinable literals now counts as such a line. Eclipse plugin: it goes through getFormatReplacements, which has no string pass (#33), so it joins nothing, just as it wraps nothing today. Version This changes the output, so under the version scheme it moves the second number.
Open Graph Description: Fixes #148. What When a concatenation of string literals ends up on one line, the + between them stays. The string wrapping pass only looks at lines past the column limit, so the output depends on ...
X Description: Fixes #148. What When a concatenation of string literals ends up on one line, the + between them stays. The string wrapping pass only looks at lines past the column limit, so the output depends on ...
Opengraph URL: https://github.com/openjavaformat/open-java-format/pull/176
X: @github
Domain: redirect.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:8fefb69e-36d6-37e1-e063-86df94904aa0 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E160:3EA038:678ED8:891F5E:6AFBCA17 |
| html-safe-nonce | 77159a66f9d555109f058450fcd3f7d8e35bb1510e99e145f4eb85d00b654033 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMTYwOjNFQTAzODo2NzhFRDg6ODkxRjVFOjZBRkJDQTE3IiwidmlzaXRvcl9pZCI6IjQ5MjQzNTQ1ODg0ODk1MzM5NzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 4d4e9c808d005b5771c0e4e3cf9cb61fa0de29b1d16d4818f48dee67db152d01 |
| hovercard-subject-tag | pull_request:5083260146 |
| 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/176/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 | Fixes #148. What When a concatenation of string literals ends up on one line, the + between them stays. The string wrapping pass only looks at lines past the column limit, so the output depends on ... |
| 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 full-width |
| 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