From a4c6e8c4e22ddea9668eb3380603ad53d8ce6a5e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 5 Feb 2020 10:50:07 +0100 Subject: Refactor if-let -> match assist to use ast::make --- crates/ra_syntax/src/ast/expr_extensions.rs | 15 +++++++++++++++ crates/ra_syntax/src/ast/make.rs | 13 ++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast/expr_extensions.rs b/crates/ra_syntax/src/ast/expr_extensions.rs index 539759450..2e50a095c 100644 --- a/crates/ra_syntax/src/ast/expr_extensions.rs +++ b/crates/ra_syntax/src/ast/expr_extensions.rs @@ -7,6 +7,21 @@ use crate::{ SyntaxToken, T, }; +impl ast::Expr { + pub fn is_block_like(&self) -> bool { + match self { + ast::Expr::IfExpr(_) + | ast::Expr::LoopExpr(_) + | ast::Expr::ForExpr(_) + | ast::Expr::WhileExpr(_) + | ast::Expr::BlockExpr(_) + | ast::Expr::MatchExpr(_) + | ast::Expr::TryBlockExpr(_) => true, + _ => false, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub enum ElseBranch { Block(ast::BlockExpr), diff --git a/crates/ra_syntax/src/ast/make.rs b/crates/ra_syntax/src/ast/make.rs index 38c0e9a66..629503dc5 100644 --- a/crates/ra_syntax/src/ast/make.rs +++ b/crates/ra_syntax/src/ast/make.rs @@ -122,11 +122,18 @@ pub fn match_arm(pats: impl IntoIterator, expr: ast::Expr) -> a } pub fn match_arm_list(arms: impl IntoIterator) -> ast::MatchArmList { - let arms_str = arms.into_iter().map(|arm| format!("\n {}", arm.syntax())).join(","); - return from_text(&format!("{},\n", arms_str)); + let arms_str = arms + .into_iter() + .map(|arm| { + let needs_comma = arm.expr().map_or(true, |it| !it.is_block_like()); + let comma = if needs_comma { "," } else { "" }; + format!(" {}{}\n", arm.syntax(), comma) + }) + .collect::(); + return from_text(&format!("{}", arms_str)); fn from_text(text: &str) -> ast::MatchArmList { - ast_from_text(&format!("fn f() {{ match () {{{}}} }}", text)) + ast_from_text(&format!("fn f() {{ match () {{\n{}}} }}", text)) } } -- cgit v1.2.3