Skip to content

Commit 6bcacfc

Browse files
authored
Merge pull request #170 from openjavaformat/fix/column-after-operator
Keep the rows of a table that follows an operator such as -> or +=
2 parents d17c6d7 + 5a27e26 commit 6bcacfc

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInput.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
460460
for (String str : strings) {
461461
toks.add(new Tok(isNumbered ? kN++ : -1, str, str, charI, columnI, isToken, null));
462462
charI += str.length();
463-
columnI = updateColumn(columnI, originalTokText);
463+
columnI = updateColumn(columnI, str);
464464
}
465465
}
466466
if (extraNewline != null) {
@@ -523,8 +523,7 @@ private static boolean continueTrailingComment(List<Tok> toks, String comment, S
523523
return false;
524524
}
525525

526-
// What comes before the trailing comment on its line is read in the text, since Tok.getColumn() counts an
527-
// operator such as == once for each of its characters. Code there makes the comment a trailing one, and its
526+
// What comes before the trailing comment on its line: code there makes the comment a trailing one, and its
528527
// length is the comment's column.
529528
var position = trailing.getPosition();
530529
var lineStart = Math.max(text.lastIndexOf('\n', position), text.lastIndexOf('\r', position)) + 1;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Pairs of arguments and array elements laid out as a table keep their rows when an operator of more than one
2+
# character, such as -> or ==, comes before the table on its line, as they do after any other code.
3+
----------
4+
class T {
5+
Supplier<Map<String, Integer>> pairs() {
6+
return () -> Map.of("one", 1,
7+
"two", 2);
8+
}
9+
10+
int[] grid(int mode) {
11+
return mode == 0 ? new int[] {1, 2, 3,
12+
4, 5, 6} : null;
13+
}
14+
}
15+
----------
16+
class T {
17+
Supplier<Map<String, Integer>> pairs() {
18+
return () -> Map.of(
19+
"one", 1,
20+
"two", 2);
21+
}
22+
23+
int[] grid(int mode) {
24+
return mode == 0
25+
? new int[] {
26+
1, 2, 3,
27+
4, 5, 6
28+
}
29+
: null;
30+
}
31+
}

0 commit comments

Comments
 (0)