aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/handlers/introduce_variable.rs57
1 files changed, 49 insertions, 8 deletions
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs
index 88b62278f..96affe49d 100644
--- a/crates/ra_assists/src/handlers/introduce_variable.rs
+++ b/crates/ra_assists/src/handlers/introduce_variable.rs
@@ -44,12 +44,26 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
44 } 44 }
45 let target = expr.syntax().text_range(); 45 let target = expr.syntax().text_range();
46 acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { 46 acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| {
47 let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
48 Some(field) => field.name_ref(),
49 None => None,
50 };
51
47 let mut buf = String::new(); 52 let mut buf = String::new();
48 53
54 let var_name = match &field_shorthand {
55 Some(it) => it.to_string(),
56 None => "var_name".to_string(),
57 };
58 let expr_range = match &field_shorthand {
59 Some(it) => it.syntax().text_range().cover(expr.syntax().text_range()),
60 None => expr.syntax().text_range(),
61 };
62
49 if wrap_in_block { 63 if wrap_in_block {
50 buf.push_str("{ let var_name = "); 64 format_to!(buf, "{{ let {} = ", var_name);
51 } else { 65 } else {
52 buf.push_str("let var_name = "); 66 format_to!(buf, "let {} = ", var_name);
53 }; 67 };
54 format_to!(buf, "{}", expr.syntax()); 68 format_to!(buf, "{}", expr.syntax());
55 69
@@ -64,13 +78,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
64 if full_stmt.unwrap().semicolon_token().is_none() { 78 if full_stmt.unwrap().semicolon_token().is_none() {
65 buf.push_str(";"); 79 buf.push_str(";");
66 } 80 }
67 let offset = expr.syntax().text_range();
68 match ctx.config.snippet_cap { 81 match ctx.config.snippet_cap {
69 Some(cap) => { 82 Some(cap) => {
70 let snip = buf.replace("let var_name", "let $0var_name"); 83 let snip =
71 edit.replace_snippet(cap, offset, snip) 84 buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
85 edit.replace_snippet(cap, expr_range, snip)
72 } 86 }
73 None => edit.replace(offset, buf), 87 None => edit.replace(expr_range, buf),
74 } 88 }
75 return; 89 return;
76 } 90 }
@@ -88,11 +102,12 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
88 buf.push_str(text); 102 buf.push_str(text);
89 } 103 }
90 104
91 edit.replace(expr.syntax().text_range(), "var_name".to_string()); 105 edit.replace(expr_range, var_name.clone());
92 let offset = anchor_stmt.text_range().start(); 106 let offset = anchor_stmt.text_range().start();
93 match ctx.config.snippet_cap { 107 match ctx.config.snippet_cap {
94 Some(cap) => { 108 Some(cap) => {
95 let snip = buf.replace("let var_name", "let $0var_name"); 109 let snip =
110 buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
96 edit.insert_snippet(cap, offset, snip) 111 edit.insert_snippet(cap, offset, snip)
97 } 112 }
98 None => edit.insert(offset, buf), 113 None => edit.insert(offset, buf),
@@ -504,6 +519,32 @@ fn main() {
504 } 519 }
505 520
506 #[test] 521 #[test]
522 fn introduce_var_field_shorthand() {
523 check_assist(
524 introduce_variable,
525 r#"
526struct S {
527 foo: i32
528}
529
530fn main() {
531 S { foo: <|>1 + 1<|> }
532}
533"#,
534 r#"
535struct S {
536 foo: i32
537}
538
539fn main() {
540 let $0foo = 1 + 1;
541 S { foo }
542}
543"#,
544 )
545 }
546
547 #[test]
507 fn test_introduce_var_for_return_not_applicable() { 548 fn test_introduce_var_for_return_not_applicable() {
508 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); 549 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
509 } 550 }