Skip to content

Commit 459ad8a

Browse files
committed
Don't fill type parameter declaration lists
Like formal parameter declaration lists, class type parameter declarations should switch to one declaration per line after the first line. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=118391112
1 parent 6e6f195 commit 459ad8a

File tree

6 files changed

+556
-62
lines changed

6 files changed

+556
-62
lines changed

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

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,12 +1264,13 @@ public boolean visit(MethodDeclaration node) {
12641264
{
12651265
boolean first = true;
12661266
if (!node.typeParameters().isEmpty()) {
1267-
visitTypeParameters(node.typeParameters(), ZERO, BreakOrNot.NO);
1267+
token("<");
1268+
typeParametersRest(node.typeParameters(), plusFour);
12681269
first = false;
12691270
}
12701271

12711272
boolean openedNameAndTypeScope = false;
1272-
// constructor-like declarations that don't match the name of the enclosing class are
1273+
// constructor-like declarations that don't match the name of the enclosing class are
12731274
// parsed as method declarations with a null return type
12741275
if (!node.isConstructor() && node.getReturnType2() != null) {
12751276
if (!first) {
@@ -1864,22 +1865,22 @@ public boolean visit(TypeDeclaration node) {
18641865
visitModifiers(node.modifiers(), Direction.VERTICAL, Optional.<BreakTag>absent());
18651866
boolean hasSuperclassType = node.getSuperclassType() != null;
18661867
boolean hasSuperInterfaceTypes = !node.superInterfaceTypes().isEmpty();
1867-
builder.open(ZERO);
18681868
builder.addAll(breaks);
18691869
token(node.isInterface() ? "interface" : "class");
18701870
builder.space();
18711871
visit(node.getName());
18721872
if (!node.typeParameters().isEmpty()) {
1873-
visitTypeParameters(
1874-
node.typeParameters(), hasSuperclassType || hasSuperInterfaceTypes ? plusFour : ZERO,
1875-
BreakOrNot.YES);
1873+
token("<");
18761874
}
1877-
if (hasSuperclassType || hasSuperInterfaceTypes) {
1878-
builder.open(plusFour);
1875+
builder.open(plusFour);
1876+
{
1877+
if (!node.typeParameters().isEmpty()) {
1878+
typeParametersRest(
1879+
node.typeParameters(), hasSuperclassType || hasSuperInterfaceTypes ? plusFour : ZERO);
1880+
}
18791881
if (hasSuperclassType) {
18801882
builder.breakToFill(" ");
18811883
token("extends");
1882-
// TODO(b/20761216): using a non-breaking space here could cause >100 char lines
18831884
builder.space();
18841885
node.getSuperclassType().accept(this);
18851886
}
@@ -1899,7 +1900,6 @@ public boolean visit(TypeDeclaration node) {
18991900
}
19001901
builder.close();
19011902
}
1902-
builder.close();
19031903
}
19041904
builder.close();
19051905
if (node.bodyDeclarations() == null) {
@@ -2501,29 +2501,27 @@ private void visitToDeclare(
25012501
ReceiverParameter.NO);
25022502
}
25032503

2504-
/** Helper method for {@link MethodDeclaration}s and {@link TypeDeclaration}s. */
2505-
private void visitTypeParameters(
2506-
List<TypeParameter> nodes, Indent plusIndent, BreakOrNot breakAfterOpen) {
2507-
if (!nodes.isEmpty()) {
2508-
token("<");
2509-
builder.open(plusIndent);
2510-
builder.open(plusFour);
2511-
if (breakAfterOpen.isYes()) {
2512-
builder.breakOp();
2513-
}
2514-
boolean first = true;
2515-
for (TypeParameter node : nodes) {
2516-
if (!first) {
2517-
token(",");
2518-
builder.breakToFill(" ");
2519-
}
2520-
visit(node);
2521-
first = false;
2504+
/**
2505+
* Helper method for formatting the type parameter list of a {@link MethodDeclaration} or
2506+
* {@link TypeDeclaration}. Does not omit the leading '<', which should be associated with
2507+
* the type name.
2508+
*/
2509+
private void typeParametersRest(List<TypeParameter> typeParameters, Indent plusIndent) {
2510+
builder.open(plusIndent);
2511+
builder.breakOp();
2512+
builder.open(ZERO);
2513+
boolean first = true;
2514+
for (TypeParameter typeParameter : typeParameters) {
2515+
if (!first) {
2516+
token(",");
2517+
builder.breakOp(" ");
25222518
}
2523-
builder.close();
2524-
builder.close();
2525-
token(">");
2519+
typeParameter.accept(this);
2520+
first = false;
25262521
}
2522+
token(">");
2523+
builder.close();
2524+
builder.close();
25272525
}
25282526

25292527
/** Helper method for {@link UnionType}s. */

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class B21305044 {
22

33
class T<
4-
@Nullable X extends @Nullable Object, @Nullable Y extends @Nullable Object,
5-
@Nullable Z extends @Nullable Object> extends Function<@Nullable X, @Nullable Y>
6-
implements Predicate<@Nullable Z> {
4+
@Nullable X extends @Nullable Object,
5+
@Nullable Y extends @Nullable Object,
6+
@Nullable Z extends @Nullable Object>
7+
extends Function<@Nullable X, @Nullable Y> implements Predicate<@Nullable Z> {
78
public T(@Nullable T this, List<@Nullable X> xs) {}
89

910
public T(@Nullable T B21305044.this, List<@Nullable X> xs) {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Xxxx {
2+
3+
public static final class XxxxXxxxxxxxXxxxxxxxxXxxxxxXxxxxxxx<
4+
X, X, X extends XxxxxxxxxXxxxxxxxxXxxxxxxx<X, X>> {
5+
6+
int x;
7+
}
8+
9+
public static final class XxxxXxxxxxxxXxxxxxxxxXxxxxxXxxxxxxx<
10+
X, X, X extends XxxxxxxxxXxxxxxxxxXxxxxxxx<X, X>> extends Xxxxx {
11+
12+
int x;
13+
}
14+
15+
class XxxxxxxxXxxxxxxxxXxxxXxxx<
16+
X extends XxxxxxxxxXxxxxxx, X extends XxxxxxxxxXxxxxx, X extends Xxxxxxxxxxxxx, X>
17+
extends XxxxXxxx {
18+
19+
int x;
20+
}
21+
22+
@Xxx
23+
class XxxxxxxxxxxXxxxxxXx<
24+
X extends XxxxxxXxxxxxxxxxxXxxxxxXxxxxx, X extends Xxxxxxx, XX extends Xxxxxxxxx<X>>
25+
extends XxxxxxxxXx<X, XX> {
26+
27+
int x;
28+
}
29+
30+
static class XxxxxXxxxxXxxx<
31+
XXXXX extends XxxxxxxxXxxxxxx<XXXXX>, XXX extends XxxxxxxxXxxxxxx<XXX>> {
32+
33+
int x;
34+
}
35+
36+
@Xxx
37+
class XxxxxxxxxxxXxxxxxXx<
38+
X extends XxxxxxXxxxxxxxxxxXxxxxxXxxxxx, X extends Xxxxxxx, X extends Xxxxxxx,
39+
XX extends Xxxxxxxxx<X>> extends XxxxxxxxXx<X, XX> {
40+
41+
int x;
42+
}
43+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Xxxx {
2+
3+
public static final class XxxxXxxxxxxxXxxxxxxxxXxxxxxXxxxxxxx<
4+
X, X, X extends XxxxxxxxxXxxxxxxxxXxxxxxxx<X, X>> {
5+
6+
int x;
7+
}
8+
9+
public static final class XxxxXxxxxxxxXxxxxxxxxXxxxxxXxxxxxxx<
10+
X, X, X extends XxxxxxxxxXxxxxxxxxXxxxxxxx<X, X>>
11+
extends Xxxxx {
12+
13+
int x;
14+
}
15+
16+
class XxxxxxxxXxxxxxxxxXxxxXxxx<
17+
X extends XxxxxxxxxXxxxxxx, X extends XxxxxxxxxXxxxxx, X extends Xxxxxxxxxxxxx, X>
18+
extends XxxxXxxx {
19+
20+
int x;
21+
}
22+
23+
@Xxx
24+
class XxxxxxxxxxxXxxxxxXx<
25+
X extends XxxxxxXxxxxxxxxxxXxxxxxXxxxxx, X extends Xxxxxxx, XX extends Xxxxxxxxx<X>>
26+
extends XxxxxxxxXx<X, XX> {
27+
28+
int x;
29+
}
30+
31+
static class XxxxxXxxxxXxxx<
32+
XXXXX extends XxxxxxxxXxxxxxx<XXXXX>, XXX extends XxxxxxxxXxxxxxx<XXX>> {
33+
34+
int x;
35+
}
36+
37+
@Xxx
38+
class XxxxxxxxxxxXxxxxxXx<
39+
X extends XxxxxxXxxxxxxxxxxXxxxxxXxxxxx,
40+
X extends Xxxxxxx,
41+
X extends Xxxxxxx,
42+
XX extends Xxxxxxxxx<X>>
43+
extends XxxxxxxxXx<X, XX> {
44+
45+
int x;
46+
}
47+
}

0 commit comments

Comments
 (0)