diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-19 12:15:55 +0100 |
commit | f209843e31af7f0e0212aa28ffec2efad2a70c6f (patch) | |
tree | 548227da78a3bea644f57714d075410c0bdf7469 /crates/ra_assists/src/introduce_variable.rs | |
parent | 58d4983ba5745975446d60f2886d96f8d2adf0f2 (diff) | |
parent | d4a66166c002f0a49e41d856a49cb5685ac93202 (diff) |
Merge #1545
1545: migrate ra_syntax to the new rowan API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/introduce_variable.rs')
-rw-r--r-- | crates/ra_assists/src/introduce_variable.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index f7f5ccafa..ce28132c9 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs | |||
@@ -20,8 +20,8 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
20 | return None; | 20 | return None; |
21 | } | 21 | } |
22 | let expr = node.ancestors().find_map(valid_target_expr)?; | 22 | let expr = node.ancestors().find_map(valid_target_expr)?; |
23 | let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?; | 23 | let (anchor_stmt, wrap_in_block) = anchor_stmt(expr.clone())?; |
24 | let indent = anchor_stmt.prev_sibling_or_token()?.as_token()?; | 24 | let indent = anchor_stmt.prev_sibling_or_token()?.as_token()?.clone(); |
25 | if indent.kind() != WHITESPACE { | 25 | if indent.kind() != WHITESPACE { |
26 | return None; | 26 | return None; |
27 | } | 27 | } |
@@ -37,9 +37,9 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
37 | }; | 37 | }; |
38 | 38 | ||
39 | expr.syntax().text().push_to(&mut buf); | 39 | expr.syntax().text().push_to(&mut buf); |
40 | let full_stmt = ast::ExprStmt::cast(anchor_stmt); | 40 | let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone()); |
41 | let is_full_stmt = if let Some(expr_stmt) = full_stmt { | 41 | let is_full_stmt = if let Some(expr_stmt) = &full_stmt { |
42 | Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax()) | 42 | Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone()) |
43 | } else { | 43 | } else { |
44 | false | 44 | false |
45 | }; | 45 | }; |
@@ -81,7 +81,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
81 | 81 | ||
82 | /// Check whether the node is a valid expression which can be extracted to a variable. | 82 | /// Check whether the node is a valid expression which can be extracted to a variable. |
83 | /// In general that's true for any expression, but in some cases that would produce invalid code. | 83 | /// In general that's true for any expression, but in some cases that would produce invalid code. |
84 | fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { | 84 | fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> { |
85 | match node.kind() { | 85 | match node.kind() { |
86 | PATH_EXPR => None, | 86 | PATH_EXPR => None, |
87 | BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()), | 87 | BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()), |
@@ -96,14 +96,10 @@ fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> { | |||
96 | /// to produce correct code. | 96 | /// to produce correct code. |
97 | /// It can be a statement, the last in a block expression or a wanna be block | 97 | /// It can be a statement, the last in a block expression or a wanna be block |
98 | /// expression like a lambda or match arm. | 98 | /// expression like a lambda or match arm. |
99 | fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | 99 | fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> { |
100 | expr.syntax().ancestors().find_map(|node| { | 100 | expr.syntax().ancestors().find_map(|node| { |
101 | if ast::Stmt::cast(node).is_some() { | ||
102 | return Some((node, false)); | ||
103 | } | ||
104 | |||
105 | if let Some(expr) = node.parent().and_then(ast::Block::cast).and_then(|it| it.expr()) { | 101 | if let Some(expr) = node.parent().and_then(ast::Block::cast).and_then(|it| it.expr()) { |
106 | if expr.syntax() == node { | 102 | if expr.syntax() == &node { |
107 | tested_by!(test_introduce_var_last_expr); | 103 | tested_by!(test_introduce_var_last_expr); |
108 | return Some((node, false)); | 104 | return Some((node, false)); |
109 | } | 105 | } |
@@ -115,6 +111,10 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | |||
115 | } | 111 | } |
116 | } | 112 | } |
117 | 113 | ||
114 | if ast::Stmt::cast(node.clone()).is_some() { | ||
115 | return Some((node, false)); | ||
116 | } | ||
117 | |||
118 | None | 118 | None |
119 | }) | 119 | }) |
120 | } | 120 | } |