diff options
Diffstat (limited to 'crates/ra_ide_api_light/src/assists')
-rw-r--r-- | crates/ra_ide_api_light/src/assists/introduce_variable.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/crates/ra_ide_api_light/src/assists/introduce_variable.rs b/crates/ra_ide_api_light/src/assists/introduce_variable.rs index 150baa542..e51cb22f0 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
3 | SyntaxKind::{WHITESPACE, BLOCK_EXPR}, | 3 | SyntaxKind::{WHITESPACE, SEMI}, |
4 | SyntaxNode, TextUnit, | 4 | SyntaxNode, TextUnit, |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -26,8 +26,10 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> { | |||
26 | false | 26 | false |
27 | }; | 27 | }; |
28 | if is_full_stmt { | 28 | if is_full_stmt { |
29 | if expr.syntax().kind() == BLOCK_EXPR { | 29 | if let Some(last_child) = expr.syntax().last_child() { |
30 | buf.push_str(";"); | 30 | if last_child.kind() != SEMI && !is_semi_right_after(expr.syntax()) { |
31 | buf.push_str(";"); | ||
32 | } | ||
31 | } | 33 | } |
32 | edit.replace(expr.syntax().range(), buf); | 34 | edit.replace(expr.syntax().range(), buf); |
33 | } else { | 35 | } else { |
@@ -60,6 +62,20 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> { | |||
60 | }) | 62 | }) |
61 | } | 63 | } |
62 | 64 | ||
65 | fn is_semi_right_after(node: &SyntaxNode) -> bool { | ||
66 | let mut node = node; | ||
67 | loop { | ||
68 | if let Some(next) = node.next_sibling() { | ||
69 | if next.kind() == WHITESPACE { | ||
70 | node = next; | ||
71 | continue; | ||
72 | } | ||
73 | return next.kind() == SEMI; | ||
74 | } | ||
75 | return false; | ||
76 | } | ||
77 | } | ||
78 | |||
63 | #[cfg(test)] | 79 | #[cfg(test)] |
64 | mod tests { | 80 | mod tests { |
65 | use super::*; | 81 | use super::*; |