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 523ec7034f..3e4434c237 100644 --- a/crates/ra_ide_api_light/src/assists/introduce_variable.rs +++ b/crates/ra_ide_api_light/src/assists/introduce_variable.rs @@ -20,12 +20,16 @@ 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 !full_stmt.unwrap().has_semi() { + buf.push_str(";"); + } edit.replace(expr.syntax().range(), buf); } else { buf.push_str(";"); @@ -141,4 +145,20 @@ 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 }; + something_else(); +}", + ); + } } diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 211ba31e57..000cfb981a 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),