Skip to content

Commit 83cd759

Browse files
cushoncgruber
authored andcommitted
Include comments when calculating AST node size
The formatter prints method arguments one-per-line unless they all fit on the same line, or they're all very short. This fixes a bug where the formatter would fill the arguments even if they had long parameter comments attached to them. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=106897312
1 parent addd86d commit 83cd759

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.Iterables;
2222
import com.google.common.collect.Multimap;
23+
import com.google.googlejavaformat.Input.Tok;
24+
import com.google.googlejavaformat.Input.Token;
2325
import com.google.googlejavaformat.OpsBuilder.BlankLineWanted;
2426
import com.google.googlejavaformat.Output.BreakTag;
2527

@@ -32,6 +34,25 @@
3234
*/
3335
public final class OpsBuilder {
3436

37+
/** @return the actual size of the AST node at position, including comments. */
38+
public int actualSize(int position, int length) {
39+
Token startToken = input.getPositionTokenMap().floorEntry(position).getValue();
40+
int start = startToken.getTok().getPosition();
41+
for (Tok tok : startToken.getToksBefore()) {
42+
if (tok.isComment()) {
43+
start = Math.min(start, tok.getPosition());
44+
}
45+
}
46+
Token endToken = input.getPositionTokenMap().lowerEntry(position + length).getValue();
47+
int end = endToken.getTok().getPosition() + endToken.getTok().getText().length();
48+
for (Tok tok : endToken.getToksAfter()) {
49+
if (tok.isComment()) {
50+
end = Math.max(end, tok.getPosition() + tok.getText().length());
51+
}
52+
}
53+
return end - start;
54+
}
55+
3556
/** A request to add or remove a blank line in the output. */
3657
public abstract static class BlankLineWanted {
3758

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,14 @@ public boolean visit(ArrayInitializer node) {
571571
* Returns {@code defaultThreshold} if bin-packing can be used for the given
572572
* expression list, and {code NEVER_FILL} otherwise.
573573
*/
574-
private static int maxLinesFilledForItems(List<Expression> expressions, int defaultThreshold) {
574+
private int maxLinesFilledForItems(List<Expression> expressions, int defaultThreshold) {
575575
return hasOnlyShortItems(expressions) ? ALWAYS_FILL : defaultThreshold;
576576
}
577577

578-
private static boolean hasOnlyShortItems(List<Expression> expressions) {
578+
private boolean hasOnlyShortItems(List<Expression> expressions) {
579579
for (Expression expression : expressions) {
580-
// TODO(cushon): this ignores attached comments, so `/*myParameterName=*/ true` has length 4.
581-
if (expression.getLength() >= MAX_ITEM_LENGTH_FOR_FILLING) {
580+
if (builder.actualSize(expression.getStartPosition(), expression.getLength())
581+
>= MAX_ITEM_LENGTH_FOR_FILLING) {
582582
return false;
583583
}
584584
}

core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class B20844369 {
1717

1818
int x =
1919
//foo
20-
42 + //bar
20+
42
21+
+ //bar
2122
1;
2223

2324
int x = /*foo*/
@@ -26,6 +27,7 @@ public class B20844369 {
2627

2728
int x =
2829
/*foo*/
29-
42 + //bar
30+
42
31+
+ //bar
3032
1;
3133
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Test {
2+
{
3+
// For "All goals" view, show only advertiser goals tab.
4+
return newPageTypeSet(
5+
false /* advertisers */,
6+
false /* userManagement */,
7+
false /* campaigns */,
8+
false /* adGroups */,
9+
false /* ads */,
10+
false /* keywords */,
11+
false /* negativeKeywords */,
12+
false /* advertiserBidStrategies */,
13+
false /* bidStrategies */,
14+
false /* bidStrategyRecommendations */,
15+
false /* bidKeywords */,
16+
false /* engineBidStrategies */,
17+
false /* conversionTrackers */,
18+
false /* labels */,
19+
false /* labelKeywords */,
20+
false /* evergreen labels */,
21+
false /* searchQueries */,
22+
false /* engineSearchQueries */,
23+
false /* remarketingTarget */,
24+
false /* sitelinks */,
25+
false /* feedSitelinks */,
26+
false /* locationExtensions */,
27+
false /* callExtensions */,
28+
false /* calloutExtensions */,
29+
false /* appExtensions */,
30+
false /* events */);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Test {
2+
{
3+
// For "All goals" view, show only advertiser goals tab.
4+
return newPageTypeSet(
5+
false /* advertisers */,
6+
false /* userManagement */,
7+
false /* campaigns */,
8+
false /* adGroups */,
9+
false /* ads */,
10+
false /* keywords */,
11+
false /* negativeKeywords */,
12+
false /* advertiserBidStrategies */,
13+
false /* bidStrategies */,
14+
false /* bidStrategyRecommendations */,
15+
false /* bidKeywords */,
16+
false /* engineBidStrategies */,
17+
false /* conversionTrackers */,
18+
false /* labels */,
19+
false /* labelKeywords */,
20+
false /* evergreen labels */,
21+
false /* searchQueries */,
22+
false /* engineSearchQueries */,
23+
false /* remarketingTarget */,
24+
false /* sitelinks */,
25+
false /* feedSitelinks */,
26+
false /* locationExtensions */,
27+
false /* callExtensions */,
28+
false /* calloutExtensions */,
29+
false /* appExtensions */,
30+
false /* events */);
31+
}
32+
}

0 commit comments

Comments
 (0)