From a996b66cc65741d51270f4ebf97c5fc35e957f17 Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Sun, 13 Jan 2019 17:59:56 -0800 Subject: add failing test test_introduce_var_block_expr_second_to_last --- .../ra_ide_api_light/src/assists/introduce_variable.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'crates') 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 523ec7034..d5b7487e0 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs @@ -141,4 +141,21 @@ fn foo() { ); } + #[test] + fn test_introduce_var_block_expr_second_to_last() { + check_assist_range( + introduce_variable, + " +fn foo() { + <|>{ let x = 0; x }<|> + something_else(); +}", + " +fn foo() { + let <|>var_name = { let x = 0; x }; + var_name + something_else(); +}", + ); + } } -- cgit v1.2.3 From 1538ca3ae5d99c93c1098261bdd0fdd30f67a278 Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Sun, 13 Jan 2019 18:12:01 -0800 Subject: we don't need to write var_name after block expr? --- crates/ra_ide_api_light/src/assists/introduce_variable.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'crates') 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 d5b7487e0..310c16960 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs @@ -153,7 +153,6 @@ fn foo() { " fn foo() { let <|>var_name = { let x = 0; x }; - var_name something_else(); }", ); -- cgit v1.2.3 From 5d6cf59f608228ff6c50dedf1c7b32323b835e11 Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Sun, 13 Jan 2019 18:22:53 -0800 Subject: add semicolon for block expr in introduce_variable --- crates/ra_ide_api_light/src/assists/introduce_variable.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates') 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 310c16960..150baa542 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 @@ use ra_syntax::{ ast::{self, AstNode}, - SyntaxKind::WHITESPACE, + SyntaxKind::{WHITESPACE, BLOCK_EXPR}, SyntaxNode, TextUnit, }; @@ -26,6 +26,9 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option { false }; if is_full_stmt { + if expr.syntax().kind() == BLOCK_EXPR { + buf.push_str(";"); + } edit.replace(expr.syntax().range(), buf); } else { buf.push_str(";"); -- cgit v1.2.3 From 4149285bf55c4a7c10b6e72c83addb675b48a687 Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Tue, 15 Jan 2019 01:21:04 -0800 Subject: add semi only if it wasn't present before or after --- .../src/assists/introduce_variable.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'crates') 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 @@ use ra_syntax::{ ast::{self, AstNode}, - SyntaxKind::{WHITESPACE, BLOCK_EXPR}, + SyntaxKind::{WHITESPACE, SEMI}, SyntaxNode, TextUnit, }; @@ -26,8 +26,10 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option { false }; if is_full_stmt { - if expr.syntax().kind() == BLOCK_EXPR { - buf.push_str(";"); + if let Some(last_child) = expr.syntax().last_child() { + if last_child.kind() != SEMI && !is_semi_right_after(expr.syntax()) { + buf.push_str(";"); + } } edit.replace(expr.syntax().range(), buf); } else { @@ -60,6 +62,20 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> { }) } +fn is_semi_right_after(node: &SyntaxNode) -> bool { + let mut node = node; + loop { + if let Some(next) = node.next_sibling() { + if next.kind() == WHITESPACE { + node = next; + continue; + } + return next.kind() == SEMI; + } + return false; + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 From 5e35f191fc5e1055f8696c82c39cfa2a3ac70bff Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Tue, 15 Jan 2019 20:26:46 -0800 Subject: add has_semi to ExprStmt --- crates/ra_syntax/src/ast.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'crates') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 8bf439b60..e6b5e407d 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -296,6 +296,15 @@ impl IfExpr { } } +impl ExprStmt { + pub fn has_semi(&self) -> bool { + match self.syntax().last_child() { + None => false, + Some(node) => node.kind() == SEMI, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum PathSegmentKind<'a> { Name(&'a NameRef), -- cgit v1.2.3 From 4dd7ec94bc92055f09fe2aa6a8920a4e32075cef Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Tue, 15 Jan 2019 20:27:15 -0800 Subject: use has_semi --- crates/ra_ide_api_light/src/assists/introduce_variable.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'crates') 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 e51cb22f0..230d23039 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs @@ -20,16 +20,15 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option { buf.push_str("let var_name = "); expr.syntax().text().push_to(&mut buf); - let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) { + let full_stmt = ast::ExprStmt::cast(anchor_stmt); + let is_full_stmt = if let Some(expr_stmt) = full_stmt { Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax()) } else { false }; if is_full_stmt { - if let Some(last_child) = expr.syntax().last_child() { - if last_child.kind() != SEMI && !is_semi_right_after(expr.syntax()) { - buf.push_str(";"); - } + if !full_stmt.unwrap().has_semi() { + buf.push_str(";"); } edit.replace(expr.syntax().range(), buf); } else { -- cgit v1.2.3 From de7c0f814c650799f92fdd42384f7b93740633dc Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Tue, 15 Jan 2019 20:28:01 -0800 Subject: delete unused fn --- crates/ra_ide_api_light/src/assists/introduce_variable.rs | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'crates') 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 230d23039..922f71254 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs @@ -61,20 +61,6 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> { }) } -fn is_semi_right_after(node: &SyntaxNode) -> bool { - let mut node = node; - loop { - if let Some(next) = node.next_sibling() { - if next.kind() == WHITESPACE { - node = next; - continue; - } - return next.kind() == SEMI; - } - return false; - } -} - #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 From 67ddd3359817f0a1254005e4c2dc921eed5e7ad9 Mon Sep 17 00:00:00 2001 From: Yerkebulan Tulibergenov Date: Tue, 15 Jan 2019 20:29:37 -0800 Subject: don't need SEMI any more --- crates/ra_ide_api_light/src/assists/introduce_variable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') 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 922f71254..3e4434c23 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 @@ use ra_syntax::{ ast::{self, AstNode}, - SyntaxKind::{WHITESPACE, SEMI}, + SyntaxKind::WHITESPACE, SyntaxNode, TextUnit, }; -- cgit v1.2.3