Skip to content

Commit 34ddeaa

Browse files
authored
Merge pull request #68 from openjavaformat/semicolon-only-class-body
Format a class body that holds nothing but semicolons
2 parents b1f0450 + 956f1d8 commit 34ddeaa

5 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3811,6 +3811,14 @@ private void addBodyDeclarations(
38113811
tokenBreakTrailingComment("{", plusTwo);
38123812
builder.blankLineWanted(BlankLineWanted.NO);
38133813
builder.open(ZERO);
3814+
if (builder.peekToken().equals(Optional.of(";"))) {
3815+
// A body with nothing but stray semicolons: javac drops them, so the member list is empty, but
3816+
// the tokens are still there and have to be written out, one per line at the member indent.
3817+
builder.open(memberIndent);
3818+
dropEmptyDeclarations();
3819+
builder.close();
3820+
builder.forcedBreak();
3821+
}
38143822
token("}", plusTwo);
38153823
builder.close();
38163824
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SemicolonInClass {
2+
;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class SemicolonInClass {
2+
;
3+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
interface B { ; }
2+
enum C { X; ; }
3+
record R(int a) { ; }
4+
class E { ;; }
5+
class F { Object o = new Object() { ; }; }
6+
class G { // comment
7+
;
8+
}
9+
@interface H { ; }
10+
class I {
11+
;
12+
int x;
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
interface B {
2+
;
3+
}
4+
5+
enum C {
6+
X;
7+
;
8+
}
9+
10+
record R(int a) {
11+
;
12+
}
13+
14+
class E {
15+
;
16+
;
17+
}
18+
19+
class F {
20+
Object o = new Object() {
21+
;
22+
};
23+
}
24+
25+
class G { // comment
26+
;
27+
}
28+
29+
@interface H {
30+
;
31+
}
32+
33+
class I {
34+
;
35+
int x;
36+
}

0 commit comments

Comments
 (0)