aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/ast_editor.rs2
-rw-r--r--crates/ra_assists/src/introduce_variable.rs9
-rw-r--r--crates/ra_assists/src/move_guard.rs4
-rw-r--r--crates/ra_assists/src/replace_if_let_with_match.rs16
4 files changed, 20 insertions, 11 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index 6815638dc..048478662 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -274,7 +274,7 @@ impl AstBuilder<ast::Block> {
274 274
275impl AstBuilder<ast::Expr> { 275impl AstBuilder<ast::Expr> {
276 fn from_text(text: &str) -> ast::Expr { 276 fn from_text(text: &str) -> ast::Expr {
277 ast_node_from_file_text(&format!("fn f() {{ {}; }}", text)) 277 ast_node_from_file_text(&format!("const C: () = {};", text))
278 } 278 }
279 279
280 pub fn unit() -> ast::Expr { 280 pub fn unit() -> ast::Expr {
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs
index 95c18d0e3..470ffe120 100644
--- a/crates/ra_assists/src/introduce_variable.rs
+++ b/crates/ra_assists/src/introduce_variable.rs
@@ -3,7 +3,8 @@ use hir::db::HirDatabase;
3use ra_syntax::{ 3use ra_syntax::{
4 ast::{self, AstNode}, 4 ast::{self, AstNode},
5 SyntaxKind::{ 5 SyntaxKind::{
6 BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR, WHITESPACE, 6 BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
7 WHITESPACE,
7 }, 8 },
8 SyntaxNode, TextUnit, 9 SyntaxNode, TextUnit,
9}; 10};
@@ -80,10 +81,12 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
80/// In general that's true for any expression, but in some cases that would produce invalid code. 81/// In general that's true for any expression, but in some cases that would produce invalid code.
81fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> { 82fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
82 match node.kind() { 83 match node.kind() {
83 PATH_EXPR => None, 84 PATH_EXPR | LOOP_EXPR => None,
84 BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()), 85 BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()),
85 RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), 86 RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
86 LOOP_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()), 87 BLOCK_EXPR => {
88 ast::BlockExpr::cast(node).filter(|it| it.is_standalone()).map(ast::Expr::from)
89 }
87 _ => ast::Expr::cast(node), 90 _ => ast::Expr::cast(node),
88 } 91 }
89} 92}
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<impl HirDatabase>)
65 "move condition to match guard", 65 "move condition to match guard",
66 |edit| { 66 |edit| {
67 edit.target(if_expr.syntax().text_range()); 67 edit.target(if_expr.syntax().text_range());
68 let then_only_expr = then_block.statements().next().is_none(); 68 let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none();
69 69
70 match &then_block.expr() { 70 match &then_block.block().and_then(|it| it.expr()) {
71 Some(then_expr) if then_only_expr => { 71 Some(then_expr) if then_only_expr => {
72 edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) 72 edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text())
73 } 73 }
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 @@
1use format_buf::format;
1use hir::db::HirDatabase; 2use hir::db::HirDatabase;
2use ra_fmt::extract_trivial_expression; 3use ra_fmt::extract_trivial_expression;
3use ra_syntax::{ast, AstNode}; 4use ra_syntax::{ast, AstNode};
@@ -25,16 +26,21 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) ->
25 ctx.build() 26 ctx.build()
26} 27}
27 28
28fn build_match_expr(expr: ast::Expr, pat1: ast::Pat, arm1: ast::Block, arm2: ast::Block) -> String { 29fn build_match_expr(
30 expr: ast::Expr,
31 pat1: ast::Pat,
32 arm1: ast::BlockExpr,
33 arm2: ast::BlockExpr,
34) -> String {
29 let mut buf = String::new(); 35 let mut buf = String::new();
30 buf.push_str(&format!("match {} {{\n", expr.syntax().text())); 36 format!(buf, "match {} {{\n", expr.syntax().text());
31 buf.push_str(&format!(" {} => {}\n", pat1.syntax().text(), format_arm(&arm1))); 37 format!(buf, " {} => {}\n", pat1.syntax().text(), format_arm(&arm1));
32 buf.push_str(&format!(" _ => {}\n", format_arm(&arm2))); 38 format!(buf, " _ => {}\n", format_arm(&arm2));
33 buf.push_str("}"); 39 buf.push_str("}");
34 buf 40 buf
35} 41}
36 42
37fn format_arm(block: &ast::Block) -> String { 43fn format_arm(block: &ast::BlockExpr) -> String {
38 match extract_trivial_expression(block) { 44 match extract_trivial_expression(block) {
39 None => block.syntax().text().to_string(), 45 None => block.syntax().text().to_string(),
40 Some(e) => format!("{},", e.syntax().text()), 46 Some(e) => format!("{},", e.syntax().text()),