From 22e5194396c2d26e870cda6e4712f1ec56efb04e Mon Sep 17 00:00:00 2001 From: Domantas Jadenkus Date: Mon, 24 May 2021 21:53:42 +0300 Subject: generate match arms with todo!() as placeholder body --- crates/syntax/src/ast/edit_in_place.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'crates/syntax/src') diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index ca8103668..f7ee29d14 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -356,13 +356,17 @@ impl ast::MatchArm { impl ast::MatchArmList { pub fn add_arm(&self, arm: ast::MatchArm) { normalize_ws_between_braces(self.syntax()); + let mut elements = Vec::new(); let position = match self.arms().last() { Some(last_arm) => { - let curly = last_arm + let comma = last_arm .syntax() .siblings_with_tokens(Direction::Next) .find(|it| it.kind() == T![,]); - Position::after(curly.unwrap_or_else(|| last_arm.syntax().clone().into())) + if needs_comma(&last_arm) && comma.is_none() { + elements.push(make::token(SyntaxKind::COMMA).into()); + } + Position::after(comma.unwrap_or_else(|| last_arm.syntax().clone().into())) } None => match self.l_curly_token() { Some(it) => Position::after(it), @@ -370,11 +374,16 @@ impl ast::MatchArmList { }, }; let indent = IndentLevel::from_node(self.syntax()) + 1; - let elements = vec![ - make::tokens::whitespace(&format!("\n{}", indent)).into(), - arm.syntax().clone().into(), - ]; + elements.push(make::tokens::whitespace(&format!("\n{}", indent)).into()); + elements.push(arm.syntax().clone().into()); + if needs_comma(&arm) { + elements.push(make::token(SyntaxKind::COMMA).into()); + } ted::insert_all(position, elements); + + fn needs_comma(arm: &ast::MatchArm) -> bool { + arm.expr().map_or(false, |e| !e.is_block_like()) + } } } -- cgit v1.2.3