Skip to content

Commit 6492e36

Browse files
cushonronshapiro
authored andcommitted
Preserve columnized arrays, even if they have a single column
This change preservers existing whitespace for arrays where each element is on a separate line, e.g.: new int[]{ 1, 2, 3, }; Previously this was only done if each row had two or more elements: new int[]{ 1, 2, 3, 4, 5, 6, }; ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=155128355
1 parent 5b59a76 commit 6492e36

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public boolean visitArrayInitializer(List<? extends ExpressionTree> expressions)
471471
if (!first) {
472472
builder.forcedBreak();
473473
}
474-
builder.open(row.iterator().next().getKind() == NEW_ARRAY ? ZERO : plusFour);
474+
builder.open(row.iterator().next().getKind() == NEW_ARRAY || cols == 1 ? ZERO : plusFour);
475475
boolean firstInRow = true;
476476
for (ExpressionTree item : row) {
477477
if (!firstInRow) {
@@ -2889,7 +2889,10 @@ private int argumentsAreTabular(List<? extends ExpressionTree> arguments) {
28892889
while (it.hasNext() && actualColumn(it.peek()) > start0) {
28902890
row.add(it.next());
28912891
}
2892-
if (!it.hasNext() || row.size() == 1) {
2892+
if (!it.hasNext()) {
2893+
return -1;
2894+
}
2895+
if (rowLength(row) <= 1) {
28932896
return -1;
28942897
}
28952898
rows.add(row);
@@ -2934,6 +2937,23 @@ private int argumentsAreTabular(List<? extends ExpressionTree> arguments) {
29342937
return size0;
29352938
}
29362939

2940+
static int rowLength(List<? extends ExpressionTree> row) {
2941+
int size = 0;
2942+
for (ExpressionTree tree : row) {
2943+
if (tree.getKind() != NEW_ARRAY) {
2944+
size++;
2945+
continue;
2946+
}
2947+
NewArrayTree array = (NewArrayTree) tree;
2948+
if (array.getInitializers() == null) {
2949+
size++;
2950+
continue;
2951+
}
2952+
size += rowLength(array.getInitializers());
2953+
}
2954+
return size;
2955+
}
2956+
29372957
private Integer actualColumn(ExpressionTree expression) {
29382958
Map<Integer, Integer> positionToColumnMap = builder.getInput().getPositionToColumnMap();
29392959
return positionToColumnMap.get(builder.actualStartColumn(getStartPosition(expression)));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class B37895033 {
2+
String[][] xs =
3+
new String[][] {
4+
{"B3", "C3", "D3"},
5+
{"B4", "C4", "D4"},
6+
{"B5", "C5", "D5"}
7+
};
8+
String[] ys =
9+
new String[] {
10+
"B3",
11+
"B4",
12+
"B5",
13+
};
14+
String[][] zs =
15+
new String[][] {
16+
{"B3"},
17+
{"B4"},
18+
{"B5"},
19+
};
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class B37895033 {
2+
String[][] xs =
3+
new String[][] {
4+
{"B3", "C3", "D3"},
5+
{"B4", "C4", "D4"},
6+
{"B5", "C5", "D5"}
7+
};
8+
String[] ys =
9+
new String[] {
10+
"B3", "B4", "B5",
11+
};
12+
String[][] zs =
13+
new String[][] {
14+
{"B3"}, {"B4"}, {"B5"},
15+
};
16+
}

0 commit comments

Comments
 (0)