Skip to content

Commit

Permalink
fix: formatting support for multiple if statements (kcl-lang#1041)
Browse files Browse the repository at this point in the history
* formatting support for multiple if statements inside else block

Signed-off-by: Shashank Mittal <[email protected]>

* fmt fix

Signed-off-by: Shashank Mittal <[email protected]>

* elif stmt fix

Signed-off-by: Shashank Mittal <[email protected]>

---------

Signed-off-by: Shashank Mittal <[email protected]>
  • Loading branch information
shashank-iitbhu authored Feb 17, 2024
1 parent 80f6849 commit a735a37
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a735a37

Please sign in to comment.