@@ -1317,6 +1317,7 @@ pub struct IfStatement {
1317
1317
pub consequence : Statement ,
1318
1318
pub alternative : Option < Statement > ,
1319
1319
pub node_context : NodeContext ,
1320
+ pub has_else_and_followed_by_new_line_comment : bool , // https://github.com/xixiaofinland/afmt/issues/67
1320
1321
}
1321
1322
1322
1323
impl IfStatement {
@@ -1325,10 +1326,23 @@ impl IfStatement {
1325
1326
1326
1327
let alternative = node. try_c_by_n ( "alternative" ) . map ( |a| Statement :: new ( a) ) ;
1327
1328
1329
+ let has_else_and_followed_by_new_line_comment = alternative. is_some ( )
1330
+ && node
1331
+ . children ( & mut node. walk ( ) )
1332
+ . find ( |n| n. kind ( ) == "else" )
1333
+ . and_then ( |else_node| {
1334
+ else_node. next_sibling ( ) . map ( |next_sibling| {
1335
+ next_sibling. is_extra ( )
1336
+ && next_sibling. start_position ( ) . row != else_node. end_position ( ) . row
1337
+ } )
1338
+ } )
1339
+ . unwrap_or ( false ) ;
1340
+
1328
1341
Self {
1329
1342
condition : ParenthesizedExpression :: new ( node. c_by_n ( "condition" ) ) ,
1330
1343
consequence : Statement :: new ( node. c_by_n ( "consequence" ) ) ,
1331
1344
alternative,
1345
+ has_else_and_followed_by_new_line_comment,
1332
1346
node_context : NodeContext :: with_punctuation ( & node) ,
1333
1347
}
1334
1348
}
@@ -1350,37 +1364,28 @@ impl<'a> DocBuild<'a> for IfStatement {
1350
1364
1351
1365
// Handle the 'else' part
1352
1366
if let Some ( ref a) = self . alternative {
1353
- match a {
1354
- Statement :: If ( _) => {
1355
- if self . consequence . is_block ( ) {
1356
- result. push ( b. txt ( " else " ) ) ;
1357
- } else {
1358
- result. push ( b. nl ( ) ) ;
1359
- result. push ( b. txt ( "else " ) ) ;
1360
- }
1361
- result. push ( a. build ( b) ) ; // Recursively build the nested 'else if' statement
1367
+ if self . consequence . is_block ( ) {
1368
+ result. push ( b. txt ( " else" ) ) ;
1369
+ if self . has_else_and_followed_by_new_line_comment {
1370
+ result. push ( b. nl ( ) ) ;
1371
+ } else {
1372
+ result. push ( b. txt ( " " ) ) ;
1362
1373
}
1363
- Statement :: Block ( _) => {
1364
- if self . consequence . is_block ( ) {
1365
- result. push ( b. txt ( " else " ) ) ;
1366
- } else {
1367
- result. push ( b. nl ( ) ) ;
1368
- result. push ( b. txt ( "else " ) ) ;
1369
- }
1370
- result. push ( a. build ( b) ) ;
1374
+ } else {
1375
+ result. push ( b. nl ( ) ) ;
1376
+ result. push ( b. txt ( "else" ) ) ;
1377
+ if self . has_else_and_followed_by_new_line_comment {
1378
+ result. push ( b. nl ( ) ) ;
1371
1379
}
1372
- // Handle "else" with a single statement
1373
- _ => {
1374
- if self . consequence . is_block ( ) {
1375
- result. push ( b. txt ( " else " ) ) ;
1376
- } else {
1377
- result. push ( b. nl ( ) ) ;
1378
- result. push ( b. txt ( "else" ) ) ;
1379
- result. push ( b. indent ( b. nl ( ) ) ) ;
1380
- }
1381
- result. push ( a. build ( b) ) ; // Build the else statement
1380
+
1381
+ if !matches ! ( a, Statement :: If ( _) | Statement :: Block ( _) ) {
1382
+ result. push ( b. indent ( b. nl ( ) ) ) ;
1383
+ } else {
1384
+ result. push ( b. txt ( " " ) ) ;
1382
1385
}
1383
1386
}
1387
+
1388
+ result. push ( a. build ( b) ) ;
1384
1389
}
1385
1390
} ) ;
1386
1391
}
0 commit comments