Skip to content

Commit

Permalink
Reformat parameter comments in the canonical Java format (/* foo= */).
Browse files Browse the repository at this point in the history
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
  • Loading branch information
java-team-github-bot authored and google-java-format Team committed Nov 7, 2022
1 parent c1ea933 commit dda4321
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 37 deletions.
19 changes: 19 additions & 0 deletions core/src/main/java/com/google/googlejavaformat/CommentsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package com.google.googlejavaformat;

import com.google.googlejavaformat.Input.Tok;
import java.util.Optional;
import java.util.regex.Pattern;

/**
* Rewrite comments. This interface is implemented by {@link
* com.google.googlejavaformat.java.JavaCommentsHelper JavaCommentsHelper}.
Expand All @@ -28,4 +32,19 @@ public interface CommentsHelper {
* @return the rewritten comment
*/
String rewrite(Input.Tok tok, int maxWidth, int column0);

static Optional<String> reformatParameterComment(Tok tok) {
if (!tok.isSlashStarComment()) {
return Optional.empty();
}
var match = PARAMETER_COMMENT.matcher(tok.getOriginalText());
if (!match.matches()) {
return Optional.empty();
}
return Optional.of(String.format("/* %s= */", match.group(1)));
}

Pattern PARAMETER_COMMENT =
Pattern.compile(
"/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*)(\\Q...\\E)?\\s*=\\s*\\*/");
}
5 changes: 3 additions & 2 deletions core/src/main/java/com/google/googlejavaformat/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.googlejavaformat;

import static com.google.common.collect.Iterables.getLast;
import static com.google.googlejavaformat.CommentsHelper.reformatParameterComment;
import static java.lang.Math.max;

import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -727,7 +728,7 @@ float computeWidth() {
// Account for line comments with missing spaces, see computeFlat.
return tok.length() + 1;
} else {
return tok.length();
return reformatParameterComment(tok).map(String::length).orElse(tok.length());
}
}
return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length();
Expand All @@ -741,7 +742,7 @@ String computeFlat() {
if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
return "// " + tok.getOriginalText().substring("//".length());
}
return tok.getOriginalText();
return reformatParameterComment(tok).orElse(tok.getOriginalText());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
}
if (tok.isSlashSlashComment()) {
return indentLineComments(lines, column0);
} else if (javadocShaped(lines)) {
return indentJavadoc(lines, column0);
} else {
return preserveIndentation(lines, column0);
}
return CommentsHelper.reformatParameterComment(tok)
.orElseGet(
() ->
javadocShaped(lines)
? indentJavadoc(lines, column0)
: preserveIndentation(lines, column0));
}

// For non-javadoc-shaped block comments, shift the entire block to the correct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ class B21283374 {

{
f(
/*flagA=*/ Boolean.TRUE,
/*flagB=*/ Boolean.FALSE,
/*flagC=*/ Boolean.FALSE,
/*flagD=*/ Boolean.FALSE,
/*flagE=*/ Boolean.FALSE,
/*flagF=*/ Boolean.FALSE,
/*flagG=*/ Boolean.FALSE,
/*flagH=*/ Boolean.FALSE,
/*flagI=*/ Boolean.FALSE,
/*flagJ=*/ Boolean.FALSE,
/*flagK=*/ Boolean.FALSE);
/* flagA= */ Boolean.TRUE,
/* flagB= */ Boolean.FALSE,
/* flagC= */ Boolean.FALSE,
/* flagD= */ Boolean.FALSE,
/* flagE= */ Boolean.FALSE,
/* flagF= */ Boolean.FALSE,
/* flagG= */ Boolean.FALSE,
/* flagH= */ Boolean.FALSE,
/* flagI= */ Boolean.FALSE,
/* flagJ= */ Boolean.FALSE,
/* flagK= */ Boolean.FALSE);

f(
/*flagA=*/ Boolean.TRUE,
/*flagB=*/ Boolean.FALSE,
/*flagC=*/ Boolean.FALSE,
/*flagD=*/ Boolean.FALSE,
/*flagE=*/ Boolean.FALSE,
/*flagF=*/ Boolean.FALSE,
/*flagG=*/ Boolean.FALSE,
/*flagH=*/ Boolean.FALSE,
/*flagI=*/ Boolean.FALSE,
/*flagJ=*/ Boolean.FALSE,
/*flagK=*/ Boolean.FALSE);
/* flagA= */ Boolean.TRUE,
/* flagB= */ Boolean.FALSE,
/* flagC= */ Boolean.FALSE,
/* flagD= */ Boolean.FALSE,
/* flagE= */ Boolean.FALSE,
/* flagF= */ Boolean.FALSE,
/* flagG= */ Boolean.FALSE,
/* flagH= */ Boolean.FALSE,
/* flagI= */ Boolean.FALSE,
/* flagJ= */ Boolean.FALSE,
/* flagK= */ Boolean.FALSE);

assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
assertThat(foo.barAndBaz(/*paramName=*/ false, thingy)).isEqualTo(new Something(""));
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));
assertThat(foo.barAndBaz(/* paramName= */ false, thingy)).isEqualTo(new Something(""));

f(/*paramName=*/ false);
f(/* paramName= */ false);

assertThat__________________________________________________________(
/*paramName=*/ false, thingy);
/* paramName= */ false, thingy);
assertThat__________________________________________________________(
/*paramName=*/ false, thingy);
/* paramName= */ false, thingy);

f(
arg1, /* which arg is this attached to? */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ class B26694550 {
/* === not a param comment */
fffffffffffffffffffffffffffffff(
ImmutableList.copyOf(keys), /*&=*/
/*keepGoing=*/ false,
/* keepGoing= */ false,
ggggggggggggggggggggggggggggggggggggggggggg);
fffffffffffffffffffffffffffffff(
ImmutableList.copyOf(keys),
/*keepGoing=*/ false,
/* keepGoing= */ false,
ggggggggggggggggggggggggggggggggggggggggggg);
fffffffffffffffffffffffffffffff(
ImmutableList.copyOf(keys),
/*foo_bar=*/ false,
/* foo_bar= */ false,
/*foo-bar=*/ false,
ggggggggggggggggggggggggggggggggggggggggggg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.google.googlejavaformat.java.test;

/** Tests for formatting of ParameterComments. */
class Q {
static void f(int a) {
f(/*a=*/ 1);
f(
/* a= */ 1
);
}
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) {
g(
/*a=*/ 1,
/*b=*/ 1,
/*c=*/ 1,
/*d=*/ 1,
/*e=*/ 1,
/*f=*/ 1,
/*g=*/ 1,
/*h=*/ 1,
/*i=*/ 1,
/*j=*/ 1,
/*k=*/ 1);
g(/*a=*/ 1, /*b=*/ 1, /*c=*/ 1, /*d=*/ 1, /*e=*/ 1, /*f=*/ 1, /*g=*/ 1, /*h=*/ 1, /*i=*/ 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.google.googlejavaformat.java.test;

/** Tests for formatting of ParameterComments. */
class Q {
static void f(int a) {
f(/* a= */ 1);
f(/* a= */ 1);
}

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) {
g(
/* a= */ 1,
/* b= */ 1,
/* c= */ 1,
/* d= */ 1,
/* e= */ 1,
/* f= */ 1,
/* g= */ 1,
/* h= */ 1,
/* i= */ 1,
/* j= */ 1,
/* k= */ 1);
g(
/* a= */ 1,
/* b= */ 1,
/* c= */ 1,
/* d= */ 1,
/* e= */ 1,
/* f= */ 1,
/* g= */ 1,
/* h= */ 1,
/* i= */ 1);
}
}

0 comments on commit dda4321

Please sign in to comment.