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; + } +}