Skip to content

Commit

Permalink
fix: simplify IfExpr formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Jan 14, 2025
1 parent f195a78 commit 6bf613f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 212 deletions.
28 changes: 3 additions & 25 deletions wdl-format/src/v1/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,31 +672,9 @@ pub fn format_parenthesized_expr(element: &FormatElement, stream: &mut TokenStre

/// Formats an [`IfExpr`](wdl_ast::v1::IfExpr).
pub fn format_if_expr(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
let mut children = element.children().expect("if expr children");

let last = stream.last_literal_kind();

// Nested `if` expressions are a special case where we don't want to add
// parentheses or increment the indent level.
// Otherwise, we need to add parentheses and increment the indent if the last
// token is not an open parenthesis.
let nested_else_if = matches!(last, Some(SyntaxKind::ElseKeyword));
let paren_needed = !matches!(last, Some(SyntaxKind::OpenParen)) && !nested_else_if;

if paren_needed {
stream.push_literal("(".to_string(), SyntaxKind::OpenParen);
}

let if_keyword = children.next().expect("if keyword");
assert!(if_keyword.element().kind() == SyntaxKind::IfKeyword);
(&if_keyword).write(stream);

for child in children {
stream.end_word();
for child in element.children().expect("if expr children") {
(&child).write(stream);
stream.end_word();
}

if paren_needed {
stream.push_literal(")".to_string(), SyntaxKind::CloseParen);
}
stream.trim_end(&PreToken::WordEnd);
}
Loading

0 comments on commit 6bf613f

Please sign in to comment.