From 5e3f291195b580580be7ce5622f54ebca75fb9f0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 2 Sep 2019 21:23:19 +0300 Subject: fix hir for new block syntax --- crates/ra_assists/src/move_guard.rs | 4 ++-- crates/ra_assists/src/replace_if_let_with_match.rs | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/move_guard.rs b/crates/ra_assists/src/move_guard.rs index 127c9e068..699221e33 100644 --- a/crates/ra_assists/src/move_guard.rs +++ b/crates/ra_assists/src/move_guard.rs @@ -65,9 +65,9 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) "move condition to match guard", |edit| { edit.target(if_expr.syntax().text_range()); - let then_only_expr = then_block.statements().next().is_none(); + let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none(); - match &then_block.expr() { + match &then_block.block().and_then(|it| it.expr()) { Some(then_expr) if then_only_expr => { edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) } diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs index c0bf6d235..401835c57 100644 --- a/crates/ra_assists/src/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/replace_if_let_with_match.rs @@ -1,3 +1,4 @@ +use format_buf::format; use hir::db::HirDatabase; use ra_fmt::extract_trivial_expression; use ra_syntax::{ast, AstNode}; @@ -25,16 +26,21 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> ctx.build() } -fn build_match_expr(expr: ast::Expr, pat1: ast::Pat, arm1: ast::Block, arm2: ast::Block) -> String { +fn build_match_expr( + expr: ast::Expr, + pat1: ast::Pat, + arm1: ast::BlockExpr, + arm2: ast::BlockExpr, +) -> String { let mut buf = String::new(); - buf.push_str(&format!("match {} {{\n", expr.syntax().text())); - buf.push_str(&format!(" {} => {}\n", pat1.syntax().text(), format_arm(&arm1))); - buf.push_str(&format!(" _ => {}\n", format_arm(&arm2))); + format!(buf, "match {} {{\n", expr.syntax().text()); + format!(buf, " {} => {}\n", pat1.syntax().text(), format_arm(&arm1)); + format!(buf, " _ => {}\n", format_arm(&arm2)); buf.push_str("}"); buf } -fn format_arm(block: &ast::Block) -> String { +fn format_arm(block: &ast::BlockExpr) -> String { match extract_trivial_expression(block) { None => block.syntax().text().to_string(), Some(e) => format!("{},", e.syntax().text()), -- cgit v1.2.3