diff options
Diffstat (limited to 'crates/ra_assists/src/introduce_variable.rs')
-rw-r--r-- | crates/ra_assists/src/introduce_variable.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 353bc4105..fb7333c8c 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs | |||
@@ -2,9 +2,8 @@ use test_utils::tested_by; | |||
2 | use hir::db::HirDatabase; | 2 | use hir::db::HirDatabase; |
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | ast::{self, AstNode}, | 4 | ast::{self, AstNode}, |
5 | SyntaxKind::{ | 5 | SyntaxNode, TextUnit, |
6 | WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT | 6 | SyntaxKind::{WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT}, |
7 | }, SyntaxNode, TextUnit, | ||
8 | }; | 7 | }; |
9 | 8 | ||
10 | use crate::{AssistCtx, Assist, AssistId}; | 9 | use crate::{AssistCtx, Assist, AssistId}; |
@@ -13,14 +12,14 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
13 | if ctx.frange.range.is_empty() { | 12 | if ctx.frange.range.is_empty() { |
14 | return None; | 13 | return None; |
15 | } | 14 | } |
16 | let node = ctx.covering_node(); | 15 | let node = ctx.covering_element(); |
17 | if node.kind() == COMMENT { | 16 | if node.kind() == COMMENT { |
18 | tested_by!(introduce_var_in_comment_is_not_applicable); | 17 | tested_by!(introduce_var_in_comment_is_not_applicable); |
19 | return None; | 18 | return None; |
20 | } | 19 | } |
21 | let expr = node.ancestors().find_map(valid_target_expr)?; | 20 | let expr = node.ancestors().find_map(valid_target_expr)?; |
22 | let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?; | 21 | let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?; |
23 | let indent = anchor_stmt.prev_sibling()?; | 22 | let indent = anchor_stmt.prev_sibling_or_token()?.as_token()?; |
24 | if indent.kind() != WHITESPACE { | 23 | if indent.kind() != WHITESPACE { |
25 | return None; | 24 | return None; |
26 | } | 25 | } |
@@ -54,16 +53,15 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
54 | // We want to maintain the indent level, | 53 | // We want to maintain the indent level, |
55 | // but we do not want to duplicate possible | 54 | // but we do not want to duplicate possible |
56 | // extra newlines in the indent block | 55 | // extra newlines in the indent block |
57 | for chunk in indent.text().chunks() { | 56 | let text = indent.text(); |
58 | if chunk.starts_with("\r\n") { | 57 | if text.starts_with("\r\n") { |
59 | buf.push_str("\r\n"); | 58 | buf.push_str("\r\n"); |
60 | buf.push_str(chunk.trim_start_matches("\r\n")); | 59 | buf.push_str(text.trim_start_matches("\r\n")); |
61 | } else if chunk.starts_with("\n") { | 60 | } else if text.starts_with("\n") { |
62 | buf.push_str("\n"); | 61 | buf.push_str("\n"); |
63 | buf.push_str(chunk.trim_start_matches("\n")); | 62 | buf.push_str(text.trim_start_matches("\n")); |
64 | } else { | 63 | } else { |
65 | buf.push_str(chunk); | 64 | buf.push_str(text); |
66 | } | ||
67 | } | 65 | } |
68 | 66 | ||
69 | edit.target(expr.syntax().range()); | 67 | edit.target(expr.syntax().range()); |