Skip to content

Commit dda4321

Browse files
java-team-github-botgoogle-java-format Team
authored and
google-java-format Team
committed
Reformat parameter comments in the canonical Java format (/* foo= */).
This only formats comments that would already trigger the `ParameterComment` check; it shouldn't make things parameter comments which could then cause compilation errors. PiperOrigin-RevId: 486631656
1 parent c1ea933 commit dda4321

File tree

7 files changed

+119
-37
lines changed

7 files changed

+119
-37
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package com.google.googlejavaformat;
1616

17+
import com.google.googlejavaformat.Input.Tok;
18+
import java.util.Optional;
19+
import java.util.regex.Pattern;
20+
1721
/**
1822
* Rewrite comments. This interface is implemented by {@link
1923
* com.google.googlejavaformat.java.JavaCommentsHelper JavaCommentsHelper}.
@@ -28,4 +32,19 @@ public interface CommentsHelper {
2832
* @return the rewritten comment
2933
*/
3034
String rewrite(Input.Tok tok, int maxWidth, int column0);
35+
36+
static Optional<String> reformatParameterComment(Tok tok) {
37+
if (!tok.isSlashStarComment()) {
38+
return Optional.empty();
39+
}
40+
var match = PARAMETER_COMMENT.matcher(tok.getOriginalText());
41+
if (!match.matches()) {
42+
return Optional.empty();
43+
}
44+
return Optional.of(String.format("/* %s= */", match.group(1)));
45+
}
46+
47+
Pattern PARAMETER_COMMENT =
48+
Pattern.compile(
49+
"/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)(\\Q...\\E)?\\s*=\\s*\\*/");
3150
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.googlejavaformat;
1616

1717
import static com.google.common.collect.Iterables.getLast;
18+
import static com.google.googlejavaformat.CommentsHelper.reformatParameterComment;
1819
import static java.lang.Math.max;
1920

2021
import com.google.common.base.MoreObjects;
@@ -727,7 +728,7 @@ float computeWidth() {
727728
// Account for line comments with missing spaces, see computeFlat.
728729
return tok.length() + 1;
729730
} else {
730-
return tok.length();
731+
return reformatParameterComment(tok).map(String::length).orElse(tok.length());
731732
}
732733
}
733734
return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length();
@@ -741,7 +742,7 @@ String computeFlat() {
741742
if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
742743
return "// " + tok.getOriginalText().substring("//".length());
743744
}
744-
return tok.getOriginalText();
745+
return reformatParameterComment(tok).orElse(tok.getOriginalText());
745746
}
746747

747748
@Override

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
5353
}
5454
if (tok.isSlashSlashComment()) {
5555
return indentLineComments(lines, column0);
56-
} else if (javadocShaped(lines)) {
57-
return indentJavadoc(lines, column0);
58-
} else {
59-
return preserveIndentation(lines, column0);
6056
}
57+
return CommentsHelper.reformatParameterComment(tok)
58+
.orElseGet(
59+
() ->
60+
javadocShaped(lines)
61+
? indentJavadoc(lines, column0)
62+
: preserveIndentation(lines, column0));
6163
}
6264

6365
// For non-javadoc-shaped block comments, shift the entire block to the correct

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,41 @@ class B21283374 {
1414

1515
{
1616
f(
17-
/*flagA=*/ Boolean.TRUE,
18-
/*flagB=*/ Boolean.FALSE,
19-
/*flagC=*/ Boolean.FALSE,
20-
/*flagD=*/ Boolean.FALSE,
21-
/*flagE=*/ Boolean.FALSE,
22-
/*flagF=*/ Boolean.FALSE,
23-
/*flagG=*/ Boolean.FALSE,
24-
/*flagH=*/ Boolean.FALSE,
25-
/*flagI=*/ Boolean.FALSE,
26-
/*flagJ=*/ Boolean.FALSE,
27-
/*flagK=*/ Boolean.FALSE);
17+
/* flagA= */ Boolean.TRUE,
18+
/* flagB= */ Boolean.FALSE,
19+
/* flagC= */ Boolean.FALSE,
20+
/* flagD= */ Boolean.FALSE,
21+
/* flagE= */ Boolean.FALSE,
22+
/* flagF= */ Boolean.FALSE,
23+
/* flagG= */ Boolean.FALSE,
24+
/* flagH= */ Boolean.FALSE,
25+
/* flagI= */ Boolean.FALSE,
26+
/* flagJ= */ Boolean.FALSE,
27+
/* flagK= */ Boolean.FALSE);
2828

2929
f(
30-
/*flagA=*/ Boolean.TRUE,
31-
/*flagB=*/ Boolean.FALSE,
32-
/*flagC=*/ Boolean.FALSE,
33-
/*flagD=*/ Boolean.FALSE,
34-
/*flagE=*/ Boolean.FALSE,
35-
/*flagF=*/ Boolean.FALSE,
36-
/*flagG=*/ Boolean.FALSE,
37-
/*flagH=*/ Boolean.FALSE,
38-
/*flagI=*/ Boolean.FALSE,
39-
/*flagJ=*/ Boolean.FALSE,
40-
/*flagK=*/ Boolean.FALSE);
30+
/* flagA= */ Boolean.TRUE,
31+
/* flagB= */ Boolean.FALSE,
32+
/* flagC= */ Boolean.FALSE,
33+
/* flagD= */ Boolean.FALSE,
34+
/* flagE= */ Boolean.FALSE,
35+
/* flagF= */ Boolean.FALSE,
36+
/* flagG= */ Boolean.FALSE,
37+
/* flagH= */ Boolean.FALSE,
38+
/* flagI= */ Boolean.FALSE,
39+
/* flagJ= */ Boolean.FALSE,
40+
/* flagK= */ Boolean.FALSE);
4141

42-
assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
43-
assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
44-
assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
42+
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));
43+
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));
44+
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));
4545

46-
f(/*paramName=*/ false);
46+
f(/* paramName= */ false);
4747

4848
assertThat__________________________________________________________(
49-
/*paramName=*/ false, thingy);
49+
/* paramName= */ false, thingy);
5050
assertThat__________________________________________________________(
51-
/*paramName=*/ false, thingy);
51+
/* paramName= */ false, thingy);
5252

5353
f(
5454
arg1, /* which arg is this attached to? */

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ class B26694550 {
33
/* === not a param comment */
44
fffffffffffffffffffffffffffffff(
55
ImmutableList.copyOf(keys), /*&=*/
6-
/*keepGoing=*/ false,
6+
/* keepGoing= */ false,
77
ggggggggggggggggggggggggggggggggggggggggggg);
88
fffffffffffffffffffffffffffffff(
99
ImmutableList.copyOf(keys),
10-
/*keepGoing=*/ false,
10+
/* keepGoing= */ false,
1111
ggggggggggggggggggggggggggggggggggggggggggg);
1212
fffffffffffffffffffffffffffffff(
1313
ImmutableList.copyOf(keys),
14-
/*foo_bar=*/ false,
14+
/* foo_bar= */ false,
1515
/*foo-bar=*/ false,
1616
ggggggggggggggggggggggggggggggggggggggggggg);
1717
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.google.googlejavaformat.java.test;
2+
3+
/** Tests for formatting of ParameterComments. */
4+
class Q {
5+
static void f(int a) {
6+
f(/*a=*/ 1);
7+
f(
8+
/* a= */ 1
9+
);
10+
}
11+
static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) {
12+
g(
13+
/*a=*/ 1,
14+
/*b=*/ 1,
15+
/*c=*/ 1,
16+
/*d=*/ 1,
17+
/*e=*/ 1,
18+
/*f=*/ 1,
19+
/*g=*/ 1,
20+
/*h=*/ 1,
21+
/*i=*/ 1,
22+
/*j=*/ 1,
23+
/*k=*/ 1);
24+
g(/*a=*/ 1, /*b=*/ 1, /*c=*/ 1, /*d=*/ 1, /*e=*/ 1, /*f=*/ 1, /*g=*/ 1, /*h=*/ 1, /*i=*/ 1);
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.google.googlejavaformat.java.test;
2+
3+
/** Tests for formatting of ParameterComments. */
4+
class Q {
5+
static void f(int a) {
6+
f(/* a= */ 1);
7+
f(/* a= */ 1);
8+
}
9+
10+
static void g(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) {
11+
g(
12+
/* a= */ 1,
13+
/* b= */ 1,
14+
/* c= */ 1,
15+
/* d= */ 1,
16+
/* e= */ 1,
17+
/* f= */ 1,
18+
/* g= */ 1,
19+
/* h= */ 1,
20+
/* i= */ 1,
21+
/* j= */ 1,
22+
/* k= */ 1);
23+
g(
24+
/* a= */ 1,
25+
/* b= */ 1,
26+
/* c= */ 1,
27+
/* d= */ 1,
28+
/* e= */ 1,
29+
/* f= */ 1,
30+
/* g= */ 1,
31+
/* h= */ 1,
32+
/* i= */ 1);
33+
}
34+
}

0 commit comments

Comments
 (0)