@@ -67,38 +67,12 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
6767 // Unwrap the innermost else
6868 J .If modifiedChain = removeInnermostElse (ifStatement );
6969 Statement innermostElseBody = innermost .getElsePart ().getBody ();
70- if (innermostElseBody instanceof J .Block ) {
71- J .Block elseBlock = (J .Block ) innermostElseBody ;
72- endWhitespace .set (elseBlock .getEnd ());
73- return ListUtils .concat (modifiedChain , ListUtils .mapFirst (elseBlock .getStatements (), elseStmt -> {
74- List <Comment > elseComments = elseBlock .getPrefix ().getComments ();
75- List <Comment > stmtComments = elseStmt .getPrefix ().getComments ();
76- if (!elseComments .isEmpty () || !stmtComments .isEmpty ()) {
77- return elseStmt .withComments (ListUtils .concatAll (elseComments , stmtComments ));
78- }
79- String whitespace = innermost .getElsePart ().getPrefix ().getWhitespace ();
80- return elseStmt .withPrefix (elseStmt .getPrefix ().withWhitespace (whitespace ));
81- }));
82- }
83- return Arrays .asList (modifiedChain , innermostElseBody .<Statement >withPrefix (innermost .getElsePart ().getPrefix ()));
70+ return flatten (innermost , innermostElseBody , endWhitespace , modifiedChain );
8471 }
8572 } else {
8673 // Plain else block: unwrap directly
8774 J .If newIf = ifStatement .withElsePart (null );
88- if (elsePart instanceof J .Block ) {
89- J .Block elseBlock = (J .Block ) elsePart ;
90- endWhitespace .set (elseBlock .getEnd ());
91- return ListUtils .concat (newIf , ListUtils .mapFirst (elseBlock .getStatements (), elseStmt -> {
92- List <Comment > elseComments = elseBlock .getPrefix ().getComments ();
93- List <Comment > stmtComments = elseStmt .getPrefix ().getComments ();
94- if (!elseComments .isEmpty () || !stmtComments .isEmpty ()) {
95- return elseStmt .withComments (ListUtils .concatAll (elseComments , stmtComments ));
96- }
97- String whitespace = ifStatement .getElsePart ().getPrefix ().getWhitespace ();
98- return elseStmt .withPrefix (elseStmt .getPrefix ().withWhitespace (whitespace ));
99- }));
100- }
101- return Arrays .asList (newIf , elsePart .<Statement >withPrefix (ifStatement .getElsePart ().getPrefix ()));
75+ return flatten (ifStatement , elsePart , endWhitespace , newIf );
10276 }
10377 }
10478 }
@@ -114,6 +88,23 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
11488 return maybeAutoFormat (b , alteredBlock , ctx );
11589 }
11690
91+ private List <Statement > flatten (J .If tailIf , Statement tailElse , AtomicReference <@ Nullable Space > endWhitespace , J .If ifWithoutElse ) {
92+ if (tailElse instanceof J .Block ) {
93+ J .Block elseBlock = (J .Block ) tailElse ;
94+ endWhitespace .set (elseBlock .getEnd ());
95+ return ListUtils .concat (ifWithoutElse , ListUtils .mapFirst (elseBlock .getStatements (), elseStmt -> {
96+ List <Comment > elseComments = elseBlock .getPrefix ().getComments ();
97+ List <Comment > stmtComments = elseStmt .getPrefix ().getComments ();
98+ if (!elseComments .isEmpty () || !stmtComments .isEmpty ()) {
99+ return elseStmt .withComments (ListUtils .concatAll (elseComments , stmtComments ));
100+ }
101+ String whitespace = tailIf .getElsePart ().getPrefix ().getWhitespace ();
102+ return elseStmt .withPrefix (elseStmt .getPrefix ().withWhitespace (whitespace ));
103+ }));
104+ }
105+ return Arrays .asList (ifWithoutElse , tailElse .withPrefix (tailIf .getElsePart ().getPrefix ()));
106+ }
107+
117108 private J .@ Nullable If findInnermostIfWithElse (J .If ifStatement ) {
118109 if (ifStatement .getElsePart () == null ) {
119110 return null ;
0 commit comments