File tree Expand file tree Collapse file tree 3 files changed +111
-2
lines changed
packages/prettier-plugin-java Expand file tree Collapse file tree 3 files changed +111
-2
lines changed Original file line number Diff line number Diff line change @@ -265,13 +265,20 @@ export class BlocksAndStatementPrettierVisitor extends BaseCstPrettierPrinter {
265
265
266
266
switchBlockStatementGroup ( ctx : SwitchBlockStatementGroupCtx ) {
267
267
const switchLabel = this . visit ( ctx . switchLabel ) ;
268
-
269
268
const blockStatements = this . visit ( ctx . blockStatements ) ;
270
269
270
+ const statements = ctx . blockStatements ?. [ 0 ] . children . blockStatement ;
271
+ const hasSingleStatementBlock =
272
+ statements ?. length === 1 &&
273
+ statements [ 0 ] . children . statement ?. [ 0 ] . children
274
+ . statementWithoutTrailingSubstatement ?. [ 0 ] . children . block !== undefined ;
275
+
271
276
return concat ( [
272
277
switchLabel ,
273
278
ctx . Colon [ 0 ] ,
274
- blockStatements && indent ( [ hardline , blockStatements ] )
279
+ hasSingleStatementBlock
280
+ ? concat ( [ " " , blockStatements ] )
281
+ : blockStatements && indent ( [ hardline , blockStatements ] )
275
282
] ) ;
276
283
}
277
284
Original file line number Diff line number Diff line change @@ -48,6 +48,41 @@ public String shouldWrapEvenForSmallSwitchCases() {
48
48
switch (answer ) { case "YES" : return "YES" ; default : return "NO" ; }
49
49
}
50
50
51
+ void switchCaseWithBlock1 () {
52
+ switch (a ) {
53
+ case 0 : {}
54
+ default : {}
55
+ }
56
+ }
57
+
58
+ void switchCaseWithBlock2 () {
59
+ switch (a ) {
60
+ case 0 : { b (); }
61
+ default : { c (); }
62
+ }
63
+ }
64
+
65
+ void switchCaseWithBlock3 () {
66
+ switch (a ) {
67
+ case 0 : { b (); } { c (); }
68
+ default : { d (); } { e (); }
69
+ }
70
+ }
71
+
72
+ void switchCaseWithBlock4 () {
73
+ switch (a ) {
74
+ case 0 : b (); { c (); }
75
+ default : d (); { e (); }
76
+ }
77
+ }
78
+
79
+ void switchCaseWithBlock5 () {
80
+ switch (a ) {
81
+ case 0 : { b (); } c ();
82
+ default : { d (); } e ();
83
+ }
84
+ }
85
+
51
86
// Switch rules
52
87
static void howManyAgain (int k ) {
53
88
switch (k ) {
Original file line number Diff line number Diff line change @@ -53,6 +53,73 @@ public String shouldWrapEvenForSmallSwitchCases() {
53
53
}
54
54
}
55
55
56
+ void switchCaseWithBlock1 () {
57
+ switch (a ) {
58
+ case 0 : {}
59
+ default : {}
60
+ }
61
+ }
62
+
63
+ void switchCaseWithBlock2 () {
64
+ switch (a ) {
65
+ case 0 : {
66
+ b ();
67
+ }
68
+ default : {
69
+ c ();
70
+ }
71
+ }
72
+ }
73
+
74
+ void switchCaseWithBlock3 () {
75
+ switch (a ) {
76
+ case 0 :
77
+ {
78
+ b ();
79
+ }
80
+ {
81
+ c ();
82
+ }
83
+ default :
84
+ {
85
+ d ();
86
+ }
87
+ {
88
+ e ();
89
+ }
90
+ }
91
+ }
92
+
93
+ void switchCaseWithBlock4 () {
94
+ switch (a ) {
95
+ case 0 :
96
+ b ();
97
+ {
98
+ c ();
99
+ }
100
+ default :
101
+ d ();
102
+ {
103
+ e ();
104
+ }
105
+ }
106
+ }
107
+
108
+ void switchCaseWithBlock5 () {
109
+ switch (a ) {
110
+ case 0 :
111
+ {
112
+ b ();
113
+ }
114
+ c ();
115
+ default :
116
+ {
117
+ d ();
118
+ }
119
+ e ();
120
+ }
121
+ }
122
+
56
123
// Switch rules
57
124
static void howManyAgain (int k ) {
58
125
switch (k ) {
You can’t perform that action at this time.
0 commit comments