Skip to content

Commit

Permalink
Include comments when calculating AST node size
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cushon authored and cgruber committed Nov 9, 2015
1 parent addd86d commit 83cd759
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
21 changes: 21 additions & 0 deletions core/src/main/java/com/google/googlejavaformat/OpsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.googlejavaformat.Input.Tok;
import com.google.googlejavaformat.Input.Token;
import com.google.googlejavaformat.OpsBuilder.BlankLineWanted;
import com.google.googlejavaformat.Output.BreakTag;

Expand All @@ -32,6 +34,25 @@
*/
public final class OpsBuilder {

/** @return the actual size of the AST node at position, including comments. */
public int actualSize(int position, int length) {
Token startToken = input.getPositionTokenMap().floorEntry(position).getValue();
int start = startToken.getTok().getPosition();
for (Tok tok : startToken.getToksBefore()) {
if (tok.isComment()) {
start = Math.min(start, tok.getPosition());
}
}
Token endToken = input.getPositionTokenMap().lowerEntry(position + length).getValue();
int end = endToken.getTok().getPosition() + endToken.getTok().getText().length();
for (Tok tok : endToken.getToksAfter()) {
if (tok.isComment()) {
end = Math.max(end, tok.getPosition() + tok.getText().length());
}
}
return end - start;
}

/** A request to add or remove a blank line in the output. */
public abstract static class BlankLineWanted {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,14 @@ public boolean visit(ArrayInitializer node) {
* Returns {@code defaultThreshold} if bin-packing can be used for the given
* expression list, and {code NEVER_FILL} otherwise.
*/
private static int maxLinesFilledForItems(List<Expression> expressions, int defaultThreshold) {
private int maxLinesFilledForItems(List<Expression> expressions, int defaultThreshold) {
return hasOnlyShortItems(expressions) ? ALWAYS_FILL : defaultThreshold;
}

private static boolean hasOnlyShortItems(List<Expression> expressions) {
private boolean hasOnlyShortItems(List<Expression> expressions) {
for (Expression expression : expressions) {
// TODO(cushon): this ignores attached comments, so `/*myParameterName=*/ true` has length 4.
if (expression.getLength() >= MAX_ITEM_LENGTH_FOR_FILLING) {
if (builder.actualSize(expression.getStartPosition(), expression.getLength())
>= MAX_ITEM_LENGTH_FOR_FILLING) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class B20844369 {

int x =
//foo
42 + //bar
42
+ //bar
1;

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

int x =
/*foo*/
42 + //bar
42
+ //bar
1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Test {
{
// For "All goals" view, show only advertiser goals tab.
return newPageTypeSet(
false /* advertisers */,
false /* userManagement */,
false /* campaigns */,
false /* adGroups */,
false /* ads */,
false /* keywords */,
false /* negativeKeywords */,
false /* advertiserBidStrategies */,
false /* bidStrategies */,
false /* bidStrategyRecommendations */,
false /* bidKeywords */,
false /* engineBidStrategies */,
false /* conversionTrackers */,
false /* labels */,
false /* labelKeywords */,
false /* evergreen labels */,
false /* searchQueries */,
false /* engineSearchQueries */,
false /* remarketingTarget */,
false /* sitelinks */,
false /* feedSitelinks */,
false /* locationExtensions */,
false /* callExtensions */,
false /* calloutExtensions */,
false /* appExtensions */,
false /* events */);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Test {
{
// For "All goals" view, show only advertiser goals tab.
return newPageTypeSet(
false /* advertisers */,
false /* userManagement */,
false /* campaigns */,
false /* adGroups */,
false /* ads */,
false /* keywords */,
false /* negativeKeywords */,
false /* advertiserBidStrategies */,
false /* bidStrategies */,
false /* bidStrategyRecommendations */,
false /* bidKeywords */,
false /* engineBidStrategies */,
false /* conversionTrackers */,
false /* labels */,
false /* labelKeywords */,
false /* evergreen labels */,
false /* searchQueries */,
false /* engineSearchQueries */,
false /* remarketingTarget */,
false /* sitelinks */,
false /* feedSitelinks */,
false /* locationExtensions */,
false /* callExtensions */,
false /* calloutExtensions */,
false /* appExtensions */,
false /* events */);
}
}

0 comments on commit 83cd759

Please sign in to comment.