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