Skip to content

Commit 616357c

Browse files
cushongoogle-java-format Team
authored andcommitted
Fence leading comments inside delimited groups in JavaInputAstVisitor.
`LayoutBuilder` (like `OpsBuilder`) splices a token's leading comments before any immediately preceding `open` events so comments before a statement or expression stay in the enclosing group. When `JavaInputAstVisitor` emits an opening token or delimiter (such as `(`, `<`, `assert`, or `enum` constants) and then opens an indented group without an intervening break before the first child token, a leading comment after that delimiter is hoisted out of the group along with its trailing forced break, leaving both the comment and the following line unindented. Call `LayoutBuilder.fenceComments()` after opening the indented group for `assert` conditions with detail expressions, semicolons after `enum` constants, `for`-loop initializers, multi-resource `try` headers, multi-catch clauses, and explicit type argument lists on constructor calls and method references so leading comments and the tokens following them stay indented inside the group. PiperOrigin-RevId: 1008009721
1 parent 441b262 commit 616357c

2 files changed

Lines changed: 198 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ public Void visitAssert(AssertTree node, Void unused) {
623623
token("assert");
624624
builder.space();
625625
builder.open(node.getDetail() == null ? ZERO : plusFour);
626+
builder.fenceComments();
626627
scan(node.getCondition(), null);
627628
if (node.getDetail() != null) {
628629
builder.breakOp(" ");
@@ -921,6 +922,7 @@ private boolean visitEnumDeclaration(ClassTree node) {
921922
builder.close();
922923
if (builder.peekToken().equals(Optional.of(";"))) {
923924
builder.open(plusTwo);
925+
builder.fenceComments();
924926
token(";");
925927
builder.forcedBreak();
926928
dropEmptyDeclarations();
@@ -1081,6 +1083,7 @@ public Void visitForLoop(ForLoopTree node, Void unused) {
10811083
builder.space();
10821084
token("(");
10831085
builder.open(plusFour);
1086+
builder.fenceComments();
10841087
builder.open(
10851088
node.getInitializer().size() > 1
10861089
&& node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT
@@ -2111,6 +2114,7 @@ public Void visitTry(TryTree node, Void unused) {
21112114
if (!node.getResources().isEmpty()) {
21122115
token("(");
21132116
builder.open(node.getResources().size() > 1 ? plusFour : ZERO);
2117+
builder.fenceComments();
21142118
boolean afterFirstToken = false;
21152119
for (Tree resource : node.getResources()) {
21162120
if (afterFirstToken) {
@@ -2683,6 +2687,7 @@ private void visitCatchClause(CatchTree node, AllowTrailingBlankLine allowTraili
26832687
builder.space();
26842688
token("(");
26852689
builder.open(plusFour);
2690+
builder.fenceComments();
26862691
VariableTree ex = node.getParameter();
26872692
if (ex.getType().getKind() == UNION_TYPE) {
26882693
builder.open(ZERO);
@@ -3394,6 +3399,7 @@ private void addTypeArguments(List<? extends Tree> typeArguments, Indent plusInd
33943399
}
33953400
token("<");
33963401
builder.open(plusIndent);
3402+
builder.fenceComments();
33973403
boolean afterFirstToken = false;
33983404
for (Tree typeArgument : typeArguments) {
33993405
if (afterFirstToken) {

‎core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java‎

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,4 +1108,196 @@ void f(Map<String, String> map) {
11081108
""";
11091109
assertThat(new Formatter().formatSource(input)).isEqualTo(input);
11101110
}
1111+
1112+
@Test
1113+
public void fenceCommentsAssert() throws Exception {
1114+
String input =
1115+
"""
1116+
class T {
1117+
void f(int x) {
1118+
assert
1119+
// comment
1120+
x > 0 : "positive";
1121+
}
1122+
}
1123+
""";
1124+
String expected =
1125+
"""
1126+
class T {
1127+
void f(int x) {
1128+
assert
1129+
// comment
1130+
x > 0
1131+
: "positive";
1132+
}
1133+
}
1134+
""";
1135+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1136+
}
1137+
1138+
@Test
1139+
public void fenceCommentsEnumSemicolon() throws Exception {
1140+
String input =
1141+
"""
1142+
enum E1 {
1143+
A,
1144+
// comment
1145+
;
1146+
}
1147+
1148+
enum E2 {
1149+
A
1150+
// comment
1151+
;
1152+
}
1153+
""";
1154+
String expected =
1155+
"""
1156+
enum E1 {
1157+
A,
1158+
// comment
1159+
;
1160+
}
1161+
1162+
enum E2 {
1163+
A
1164+
// comment
1165+
;
1166+
}
1167+
""";
1168+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1169+
}
1170+
1171+
@Test
1172+
public void fenceCommentsForInitializer() throws Exception {
1173+
String input =
1174+
"""
1175+
class T {
1176+
void f(int n) {
1177+
for (
1178+
// comment
1179+
int i = 0; i < n; i++) {}
1180+
for (
1181+
/* comment */
1182+
int i = 0; i < n; i++) {}
1183+
}
1184+
}
1185+
""";
1186+
String expected =
1187+
"""
1188+
class T {
1189+
void f(int n) {
1190+
for (
1191+
// comment
1192+
int i = 0;
1193+
i < n;
1194+
i++) {}
1195+
for (
1196+
/* comment */
1197+
int i = 0;
1198+
i < n;
1199+
i++) {}
1200+
}
1201+
}
1202+
""";
1203+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1204+
}
1205+
1206+
@Test
1207+
public void fenceCommentsTryWithResources() throws Exception {
1208+
String input =
1209+
"""
1210+
class T {
1211+
void f(AutoCloseable a, AutoCloseable b) throws Exception {
1212+
try (
1213+
// comment
1214+
AutoCloseable r1 = a; AutoCloseable r2 = b) {}
1215+
}
1216+
}
1217+
""";
1218+
String expected =
1219+
"""
1220+
class T {
1221+
void f(AutoCloseable a, AutoCloseable b) throws Exception {
1222+
try (
1223+
// comment
1224+
AutoCloseable r1 = a;
1225+
AutoCloseable r2 = b) {}
1226+
}
1227+
}
1228+
""";
1229+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1230+
}
1231+
1232+
@Test
1233+
public void fenceCommentsMultiCatch() throws Exception {
1234+
String input =
1235+
"""
1236+
class T {
1237+
void f() {
1238+
try {
1239+
} catch (
1240+
// comment
1241+
IllegalArgumentException | IllegalStateException e) {
1242+
}
1243+
}
1244+
}
1245+
""";
1246+
String expected =
1247+
"""
1248+
class T {
1249+
void f() {
1250+
try {
1251+
} catch (
1252+
// comment
1253+
IllegalArgumentException | IllegalStateException e) {
1254+
}
1255+
}
1256+
}
1257+
""";
1258+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1259+
}
1260+
1261+
@Test
1262+
public void fenceCommentsTypeArguments() throws Exception {
1263+
String input =
1264+
"""
1265+
class T {
1266+
<U> T() {}
1267+
1268+
static <U> void g() {}
1269+
1270+
void f() {
1271+
new <
1272+
// comment
1273+
String>T();
1274+
Runnable r =
1275+
T
1276+
::<
1277+
// comment
1278+
String>g;
1279+
}
1280+
}
1281+
""";
1282+
String expected =
1283+
"""
1284+
class T {
1285+
<U> T() {}
1286+
1287+
static <U> void g() {}
1288+
1289+
void f() {
1290+
new <
1291+
// comment
1292+
String>T();
1293+
Runnable r =
1294+
T
1295+
::<
1296+
// comment
1297+
String>g;
1298+
}
1299+
}
1300+
""";
1301+
assertThat(new Formatter().formatSource(input)).isEqualTo(expected);
1302+
}
11111303
}

0 commit comments

Comments
 (0)