Skip to content

Commit f4bf910

Browse files
andrewkreidabashev
authored andcommitted
Keep a tabular array initializer with mixed signs in rows
An array initializer written in rows, such as a table of coordinates, keeps its rows when every column holds expressions of the same kind. A column that mixed 95.0 with -95.0 or +95.0 did not count as one kind, because a signed number is a unary expression in javac's tree, so the whole table collapsed into one line, or into a filled block. This ports google/google-java-format#406 by Andrew Reid, which fixes google/google-java-format#400: a unary expression counts as the kind of its operand for that comparison. The golden TabularMixedSignInitializer comes from upstream, with the expected output in this project's style. The 15,747 files of the JDK 21 sources format exactly as before.
1 parent 746fb60 commit f4bf910

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3468,7 +3468,13 @@ private static boolean expressionsAreParallel(List<List<ExpressionTree>> rows, i
34683468
if (column >= row.size()) {
34693469
continue;
34703470
}
3471-
nodeTypes.add(row.get(column).getKind());
3471+
// Treat UnaryTree expressions as their underlying type for the comparison (so, for example
3472+
// -ve and +ve numeric literals are considered the same).
3473+
if (row.get(column) instanceof UnaryTree) {
3474+
nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind());
3475+
} else {
3476+
nodeTypes.add(row.get(column).getKind());
3477+
}
34723478
}
34733479
for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) {
34743480
if (nodeType.getCount() >= atLeastM) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class T {
2+
private static final double[] f = {
3+
95.0, 75.0, -95.0, 75.0,
4+
-95.0, 75.0, +95.0, 75.0
5+
};
6+
7+
private static final int[] g = {
8+
x++, y, ++z,
9+
x, y, ~z,
10+
--x, ++y, z--
11+
};
12+
13+
private static final bool[] h = {
14+
a, b, c, d,
15+
!e, a, b, c
16+
};
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class T {
2+
private static final double[] f = {
3+
95.0, 75.0, -95.0, 75.0,
4+
-95.0, 75.0, +95.0, 75.0
5+
};
6+
7+
private static final int[] g = {
8+
x++, y, ++z,
9+
x, y, ~z,
10+
--x, ++y, z--
11+
};
12+
13+
private static final bool[] h = {
14+
a, b, c, d,
15+
!e, a, b, c
16+
};
17+
}

0 commit comments

Comments
 (0)