From f4bf91074c19f75dd16da216f7d71255228db47a Mon Sep 17 00:00:00 2001 From: Andrew Reid Date: Thu, 24 Sep 2026 23:02:44 +0300 Subject: [PATCH] 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. --- .../javaformat/java/JavaInputAstVisitor.java | 8 +++++++- .../testdata/TabularMixedSignInitializer.input | 17 +++++++++++++++++ .../testdata/TabularMixedSignInitializer.output | 17 +++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.input create mode 100644 open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.output diff --git a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java index 0fd3569d8..4b30ad64b 100644 --- a/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java +++ b/open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java @@ -3468,7 +3468,13 @@ private static boolean expressionsAreParallel(List> rows, i if (column >= row.size()) { continue; } - nodeTypes.add(row.get(column).getKind()); + // Treat UnaryTree expressions as their underlying type for the comparison (so, for example + // -ve and +ve numeric literals are considered the same). + if (row.get(column) instanceof UnaryTree) { + nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind()); + } else { + nodeTypes.add(row.get(column).getKind()); + } } for (Multiset.Entry nodeType : nodeTypes.entrySet()) { if (nodeType.getCount() >= atLeastM) { diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.input b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.input new file mode 100644 index 000000000..2715158b1 --- /dev/null +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.input @@ -0,0 +1,17 @@ +public class T { + private static final double[] f = { + 95.0, 75.0, -95.0, 75.0, + -95.0, 75.0, +95.0, 75.0 + }; + + private static final int[] g = { + x++, y, ++z, + x, y, ~z, + --x, ++y, z-- + }; + + private static final bool[] h = { + a, b, c, d, + !e, a, b, c + }; +} diff --git a/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.output b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.output new file mode 100644 index 000000000..f2c0c8252 --- /dev/null +++ b/open-java-format/src/test/resources/com/palantir/javaformat/java/testdata/TabularMixedSignInitializer.output @@ -0,0 +1,17 @@ +public class T { + private static final double[] f = { + 95.0, 75.0, -95.0, 75.0, + -95.0, 75.0, +95.0, 75.0 + }; + + private static final int[] g = { + x++, y, ++z, + x, y, ~z, + --x, ++y, z-- + }; + + private static final bool[] h = { + a, b, c, d, + !e, a, b, c + }; +}