aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-16 06:20:09 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-16 06:20:09 +0000
commitd75a0368f5048243d6561e42e77835f6f574b321 (patch)
tree17ffe16abbf568232e50696d8dcfbd798eef7009
parent68d320a680b5df802b2c3e7dad5d890e3309ed60 (diff)
parent67ddd3359817f0a1254005e4c2dc921eed5e7ad9 (diff)
Merge #536
536: Introduce variable semicolon block expr r=matklad a=yerke Fix for https://github.com/rust-analyzer/rust-analyzer/issues/504 Feels a bit hacky... Co-authored-by: Yerkebulan Tulibergenov <[email protected]>
-rw-r--r--crates/ra_ide_api_light/src/assists/introduce_variable.rs22
-rw-r--r--crates/ra_syntax/src/ast.rs9
2 files changed, 30 insertions, 1 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 523ec7034..3e4434c23 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<Assist> {
20 20
21 buf.push_str("let var_name = "); 21 buf.push_str("let var_name = ");
22 expr.syntax().text().push_to(&mut buf); 22 expr.syntax().text().push_to(&mut buf);
23 let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) { 23 let full_stmt = ast::ExprStmt::cast(anchor_stmt);
24 let is_full_stmt = if let Some(expr_stmt) = full_stmt {
24 Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax()) 25 Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
25 } else { 26 } else {
26 false 27 false
27 }; 28 };
28 if is_full_stmt { 29 if is_full_stmt {
30 if !full_stmt.unwrap().has_semi() {
31 buf.push_str(";");
32 }
29 edit.replace(expr.syntax().range(), buf); 33 edit.replace(expr.syntax().range(), buf);
30 } else { 34 } else {
31 buf.push_str(";"); 35 buf.push_str(";");
@@ -141,4 +145,20 @@ fn foo() {
141 ); 145 );
142 } 146 }
143 147
148 #[test]
149 fn test_introduce_var_block_expr_second_to_last() {
150 check_assist_range(
151 introduce_variable,
152 "
153fn foo() {
154 <|>{ let x = 0; x }<|>
155 something_else();
156}",
157 "
158fn foo() {
159 let <|>var_name = { let x = 0; x };
160 something_else();
161}",
162 );
163 }
144} 164}
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index 211ba31e5..000cfb981 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -296,6 +296,15 @@ impl IfExpr {
296 } 296 }
297} 297}
298 298
299impl ExprStmt {
300 pub fn has_semi(&self) -> bool {
301 match self.syntax().last_child() {
302 None => false,
303 Some(node) => node.kind() == SEMI,
304 }
305 }
306}
307
299#[derive(Debug, Clone, Copy, PartialEq, Eq)] 308#[derive(Debug, Clone, Copy, PartialEq, Eq)]
300pub enum PathSegmentKind<'a> { 309pub enum PathSegmentKind<'a> {
301 Name(&'a NameRef), 310 Name(&'a NameRef),