From 5a27e260ae919f7c23d0df56cbc6296e4b56b658 Mon Sep 17 00:00:00 2001 From: Alex Abashev Date: Fri, 30 Oct 2026 12:30:26 +0300 Subject: [PATCH] Keep the rows of a table that follows an operator such as -> or += The lexer splits an operator such as == or -> into toks of one character, and it moved the column on by the whole operator for each of them, so every tok after == on its line sat two columns too far right, and after >>>= twelve. The detection of tables is the only reader of those columns: it compares the column each row starts at with the first row's. A table whose first row came after such an operator on its line was never recognized, so its pairs of arguments or its array elements were laid out like any others, on one line or one per line, while the same table after any other code kept its rows. The column now moves on by the text of each tok. The comment in continueTrailingComment no longer gives this as the reason it reads the column of a trailing comment in the text, which it still does, together with whether code comes first. The case tablesAfterMultiCharacterOperators fails without the fix. Of the 15,747 files of the JDK 21 sources, 4 format differently, each with a call laid out by hand in pairs after += or ->, which now keeps its pairs as it would after =. The files that change on a second run are the same 67 as before. Tests on JDK 21: FormatterCasesTest 886 -> 887; 1408 tests in the module, which pass on JDK 25 and 27 as well. --- .../palantir/javaformat/java/JavaInput.java | 5 ++- .../tablesAfterMultiCharacterOperators.case | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 open-java-format/src/test/resources/test-cases/tablesAfterMultiCharacterOperators.case diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java index 2b6792df0..de951b0ca 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java @@ -460,7 +460,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept for (String str : strings) { toks.add(new Tok(isNumbered ? kN++ : -1, str, str, charI, columnI, isToken, null)); charI += str.length(); - columnI = updateColumn(columnI, originalTokText); + columnI = updateColumn(columnI, str); } } if (extraNewline != null) { @@ -523,8 +523,7 @@ private static boolean continueTrailingComment(List toks, String comment, S return false; } - // What comes before the trailing comment on its line is read in the text, since Tok.getColumn() counts an - // operator such as == once for each of its characters. Code there makes the comment a trailing one, and its + // What comes before the trailing comment on its line: code there makes the comment a trailing one, and its // length is the comment's column. var position = trailing.getPosition(); var lineStart = Math.max(text.lastIndexOf('\n', position), text.lastIndexOf('\r', position)) + 1; diff --git a/open-java-format/src/test/resources/test-cases/tablesAfterMultiCharacterOperators.case b/open-java-format/src/test/resources/test-cases/tablesAfterMultiCharacterOperators.case new file mode 100644 index 000000000..636ff62d4 --- /dev/null +++ b/open-java-format/src/test/resources/test-cases/tablesAfterMultiCharacterOperators.case @@ -0,0 +1,31 @@ +# Pairs of arguments and array elements laid out as a table keep their rows when an operator of more than one +# character, such as -> or ==, comes before the table on its line, as they do after any other code. +---------- +class T { + Supplier> pairs() { + return () -> Map.of("one", 1, + "two", 2); + } + + int[] grid(int mode) { + return mode == 0 ? new int[] {1, 2, 3, + 4, 5, 6} : null; + } +} +---------- +class T { + Supplier> pairs() { + return () -> Map.of( + "one", 1, + "two", 2); + } + + int[] grid(int mode) { + return mode == 0 + ? new int[] { + 1, 2, 3, + 4, 5, 6 + } + : null; + } +}