From bf5ba6d69d27755cb5fc9eded156c5df4e2991f2 Mon Sep 17 00:00:00 2001 From: Andrew Reid Date: Sun, 20 Oct 2019 13:17:03 +1100 Subject: [PATCH 1/2] Preserve tabular arguments for mixed sign numeric lists. --- .../google/googlejavaformat/java/JavaInputAstVisitor.java | 7 ++++++- .../java/testdata/TabularMixedSignInitializer.input | 6 ++++++ .../java/testdata/TabularMixedSignInitializer.output | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index c0f645654..d2ef55315 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -3190,7 +3190,12 @@ private static boolean expressionsAreParallel( if (column >= row.size()) { continue; } - nodeTypes.add(row.get(column).getKind()); + // Treat negative and positive numeric literals as the same kind for the tabular comparison. + if (row.get(column).getKind() == Tree.Kind.UNARY_MINUS) { + 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/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input new file mode 100644 index 000000000..35c3ad6c6 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input @@ -0,0 +1,6 @@ +public class T { + private static final double[] f = { + 95.0, 75.0, -95.0, 75.0, + -95.0, 75.0, 95.0, 75.0 + }; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output new file mode 100644 index 000000000..35c3ad6c6 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output @@ -0,0 +1,6 @@ +public class T { + private static final double[] f = { + 95.0, 75.0, -95.0, 75.0, + -95.0, 75.0, 95.0, 75.0 + }; +} From de07ee4ce60f2c0d38f9e4c6c3f4b633770cf0a1 Mon Sep 17 00:00:00 2001 From: Andrew Reid Date: Fri, 25 Oct 2019 20:17:03 +1100 Subject: [PATCH 2/2] Generalize UNARY_MINUS fix to all types of UnaryTree. --- .../googlejavaformat/java/JavaInputAstVisitor.java | 5 +++-- .../java/testdata/TabularMixedSignInitializer.input | 13 ++++++++++++- .../testdata/TabularMixedSignInitializer.output | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index d2ef55315..af4e8c570 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -3190,8 +3190,9 @@ private static boolean expressionsAreParallel( if (column >= row.size()) { continue; } - // Treat negative and positive numeric literals as the same kind for the tabular comparison. - if (row.get(column).getKind() == Tree.Kind.UNARY_MINUS) { + // 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()); diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input index 35c3ad6c6..2715158b1 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.input @@ -1,6 +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 + -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/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output index 35c3ad6c6..2715158b1 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/TabularMixedSignInitializer.output @@ -1,6 +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 + -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 }; }