From a735a37ccc2e033acaa94fe0b5b7b65f2aa6e8b4 Mon Sep 17 00:00:00 2001 From: Shashank Mittal Date: Sat, 17 Feb 2024 10:14:11 +0530 Subject: [PATCH] fix: formatting support for multiple if statements (#1041) * formatting support for multiple if statements inside else block Signed-off-by: Shashank Mittal * fmt fix Signed-off-by: Shashank Mittal * elif stmt fix Signed-off-by: Shashank Mittal --------- Signed-off-by: Shashank Mittal --- kclvm/ast_pretty/src/node.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/kclvm/ast_pretty/src/node.rs b/kclvm/ast_pretty/src/node.rs index 7f83891a0..d32845968 100644 --- a/kclvm/ast_pretty/src/node.rs +++ b/kclvm/ast_pretty/src/node.rs @@ -121,15 +121,24 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> { self.write_indentation(Indentation::Indent); self.stmts(&if_stmt.body); self.write_indentation(Indentation::Dedent); + if !if_stmt.orelse.is_empty() { - if let ast::Stmt::If(elif_stmt) = &if_stmt.orelse[0].node { - // Nested if statements need to be considered, - // so `el` needs to be preceded by the current indentation. - self.fill("el"); - self.walk_if_stmt(elif_stmt); + // Check if orelse contains exactly one if statement + if if_stmt.orelse.len() == 1 { + if let ast::Stmt::If(elif_stmt) = &if_stmt.orelse[0].node { + // Nested if statements need to be considered, + // so `el` needs to be preceded by the current indentation. + self.fill("el"); + self.walk_if_stmt(elif_stmt); + } else { + self.fill("else:"); + self.write_newline_without_fill(); + self.write_indentation(Indentation::Indent); + self.stmts(&if_stmt.orelse); + self.write_indentation(Indentation::Dedent); + } } else { - // Nested if statements need to be considered, - // so `el` needs to be preceded by the current indentation. + // Handle multiple else statements self.fill("else:"); self.write_newline_without_fill(); self.write_indentation(Indentation::Indent);