aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light
diff options
context:
space:
mode:
authorYerkebulan Tulibergenov <[email protected]>2019-01-15 09:21:04 +0000
committerYerkebulan Tulibergenov <[email protected]>2019-01-15 09:21:04 +0000
commit4149285bf55c4a7c10b6e72c83addb675b48a687 (patch)
tree023733bd115aa71f91b14d45ca9f58d7353c18ce /crates/ra_ide_api_light
parent5d6cf59f608228ff6c50dedf1c7b32323b835e11 (diff)
add semi only if it wasn't present before or after
Diffstat (limited to 'crates/ra_ide_api_light')
-rw-r--r--crates/ra_ide_api_light/src/assists/introduce_variable.rs22
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 @@
1use ra_syntax::{ 1use 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
65fn 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)]
64mod tests { 80mod tests {
65 use super::*; 81 use super::*;