aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-27 00:21:43 +0100
committerAleksey Kladov <[email protected]>2020-06-27 00:21:43 +0100
commitf5584668dba709a119f398de038b3f36085be5ff (patch)
treecadace8e7e0074454a7d9b428cf6af31443c8951
parent7488cd6a1b3604d6b4f11a7f83b6006a34cda0c0 (diff)
introduce_variable -> extract_variable
-rw-r--r--crates/ra_assists/src/handlers/extract_variable.rs (renamed from crates/ra_assists/src/handlers/introduce_variable.rs)106
-rw-r--r--crates/ra_assists/src/lib.rs4
-rw-r--r--crates/ra_assists/src/tests/generated.rs36
3 files changed, 73 insertions, 73 deletions
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/extract_variable.rs
index 96affe49d..c4150d2bb 100644
--- a/crates/ra_assists/src/handlers/introduce_variable.rs
+++ b/crates/ra_assists/src/handlers/extract_variable.rs
@@ -11,7 +11,7 @@ use test_utils::mark;
11 11
12use crate::{AssistContext, AssistId, Assists}; 12use crate::{AssistContext, AssistId, Assists};
13 13
14// Assist: introduce_variable 14// Assist: extract_variable
15// 15//
16// Extracts subexpression into a variable. 16// Extracts subexpression into a variable.
17// 17//
@@ -27,13 +27,13 @@ use crate::{AssistContext, AssistId, Assists};
27// var_name * 4; 27// var_name * 4;
28// } 28// }
29// ``` 29// ```
30pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 30pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
31 if ctx.frange.range.is_empty() { 31 if ctx.frange.range.is_empty() {
32 return None; 32 return None;
33 } 33 }
34 let node = ctx.covering_element(); 34 let node = ctx.covering_element();
35 if node.kind() == COMMENT { 35 if node.kind() == COMMENT {
36 mark::hit!(introduce_var_in_comment_is_not_applicable); 36 mark::hit!(extract_var_in_comment_is_not_applicable);
37 return None; 37 return None;
38 } 38 }
39 let expr = node.ancestors().find_map(valid_target_expr)?; 39 let expr = node.ancestors().find_map(valid_target_expr)?;
@@ -43,7 +43,7 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
43 return None; 43 return None;
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("extract_variable"), "Extract into variable", target, move |edit| {
47 let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) { 47 let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
48 Some(field) => field.name_ref(), 48 Some(field) => field.name_ref(),
49 None => None, 49 None => None,
@@ -74,7 +74,7 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
74 false 74 false
75 }; 75 };
76 if is_full_stmt { 76 if is_full_stmt {
77 mark::hit!(test_introduce_var_expr_stmt); 77 mark::hit!(test_extract_var_expr_stmt);
78 if full_stmt.unwrap().semicolon_token().is_none() { 78 if full_stmt.unwrap().semicolon_token().is_none() {
79 buf.push_str(";"); 79 buf.push_str(";");
80 } 80 }
@@ -133,7 +133,7 @@ fn valid_target_expr(node: SyntaxNode) -> Option<ast::Expr> {
133 } 133 }
134} 134}
135 135
136/// Returns the syntax node which will follow the freshly introduced var 136/// Returns the syntax node which will follow the freshly extractd var
137/// and a boolean indicating whether we have to wrap it within a { } block 137/// and a boolean indicating whether we have to wrap it within a { } block
138/// to produce correct code. 138/// to produce correct code.
139/// It can be a statement, the last in a block expression or a wanna be block 139/// It can be a statement, the last in a block expression or a wanna be block
@@ -142,7 +142,7 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
142 expr.syntax().ancestors().find_map(|node| { 142 expr.syntax().ancestors().find_map(|node| {
143 if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) { 143 if let Some(expr) = node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) {
144 if expr.syntax() == &node { 144 if expr.syntax() == &node {
145 mark::hit!(test_introduce_var_last_expr); 145 mark::hit!(test_extract_var_last_expr);
146 return Some((node, false)); 146 return Some((node, false));
147 } 147 }
148 } 148 }
@@ -170,9 +170,9 @@ mod tests {
170 use super::*; 170 use super::*;
171 171
172 #[test] 172 #[test]
173 fn test_introduce_var_simple() { 173 fn test_extract_var_simple() {
174 check_assist( 174 check_assist(
175 introduce_variable, 175 extract_variable,
176 r#" 176 r#"
177fn foo() { 177fn foo() {
178 foo(<|>1 + 1<|>); 178 foo(<|>1 + 1<|>);
@@ -186,16 +186,16 @@ fn foo() {
186 } 186 }
187 187
188 #[test] 188 #[test]
189 fn introduce_var_in_comment_is_not_applicable() { 189 fn extract_var_in_comment_is_not_applicable() {
190 mark::check!(introduce_var_in_comment_is_not_applicable); 190 mark::check!(extract_var_in_comment_is_not_applicable);
191 check_assist_not_applicable(introduce_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }"); 191 check_assist_not_applicable(extract_variable, "fn main() { 1 + /* <|>comment<|> */ 1; }");
192 } 192 }
193 193
194 #[test] 194 #[test]
195 fn test_introduce_var_expr_stmt() { 195 fn test_extract_var_expr_stmt() {
196 mark::check!(test_introduce_var_expr_stmt); 196 mark::check!(test_extract_var_expr_stmt);
197 check_assist( 197 check_assist(
198 introduce_variable, 198 extract_variable,
199 r#" 199 r#"
200fn foo() { 200fn foo() {
201 <|>1 + 1<|>; 201 <|>1 + 1<|>;
@@ -206,7 +206,7 @@ fn foo() {
206}"#, 206}"#,
207 ); 207 );
208 check_assist( 208 check_assist(
209 introduce_variable, 209 extract_variable,
210 " 210 "
211fn foo() { 211fn foo() {
212 <|>{ let x = 0; x }<|> 212 <|>{ let x = 0; x }<|>
@@ -221,9 +221,9 @@ fn foo() {
221 } 221 }
222 222
223 #[test] 223 #[test]
224 fn test_introduce_var_part_of_expr_stmt() { 224 fn test_extract_var_part_of_expr_stmt() {
225 check_assist( 225 check_assist(
226 introduce_variable, 226 extract_variable,
227 " 227 "
228fn foo() { 228fn foo() {
229 <|>1<|> + 1; 229 <|>1<|> + 1;
@@ -237,10 +237,10 @@ fn foo() {
237 } 237 }
238 238
239 #[test] 239 #[test]
240 fn test_introduce_var_last_expr() { 240 fn test_extract_var_last_expr() {
241 mark::check!(test_introduce_var_last_expr); 241 mark::check!(test_extract_var_last_expr);
242 check_assist( 242 check_assist(
243 introduce_variable, 243 extract_variable,
244 r#" 244 r#"
245fn foo() { 245fn foo() {
246 bar(<|>1 + 1<|>) 246 bar(<|>1 + 1<|>)
@@ -254,7 +254,7 @@ fn foo() {
254"#, 254"#,
255 ); 255 );
256 check_assist( 256 check_assist(
257 introduce_variable, 257 extract_variable,
258 r#" 258 r#"
259fn foo() { 259fn foo() {
260 <|>bar(1 + 1)<|> 260 <|>bar(1 + 1)<|>
@@ -270,9 +270,9 @@ fn foo() {
270 } 270 }
271 271
272 #[test] 272 #[test]
273 fn test_introduce_var_in_match_arm_no_block() { 273 fn test_extract_var_in_match_arm_no_block() {
274 check_assist( 274 check_assist(
275 introduce_variable, 275 extract_variable,
276 " 276 "
277fn main() { 277fn main() {
278 let x = true; 278 let x = true;
@@ -295,9 +295,9 @@ fn main() {
295 } 295 }
296 296
297 #[test] 297 #[test]
298 fn test_introduce_var_in_match_arm_with_block() { 298 fn test_extract_var_in_match_arm_with_block() {
299 check_assist( 299 check_assist(
300 introduce_variable, 300 extract_variable,
301 " 301 "
302fn main() { 302fn main() {
303 let x = true; 303 let x = true;
@@ -327,9 +327,9 @@ fn main() {
327 } 327 }
328 328
329 #[test] 329 #[test]
330 fn test_introduce_var_in_closure_no_block() { 330 fn test_extract_var_in_closure_no_block() {
331 check_assist( 331 check_assist(
332 introduce_variable, 332 extract_variable,
333 " 333 "
334fn main() { 334fn main() {
335 let lambda = |x: u32| <|>x * 2<|>; 335 let lambda = |x: u32| <|>x * 2<|>;
@@ -344,9 +344,9 @@ fn main() {
344 } 344 }
345 345
346 #[test] 346 #[test]
347 fn test_introduce_var_in_closure_with_block() { 347 fn test_extract_var_in_closure_with_block() {
348 check_assist( 348 check_assist(
349 introduce_variable, 349 extract_variable,
350 " 350 "
351fn main() { 351fn main() {
352 let lambda = |x: u32| { <|>x * 2<|> }; 352 let lambda = |x: u32| { <|>x * 2<|> };
@@ -361,9 +361,9 @@ fn main() {
361 } 361 }
362 362
363 #[test] 363 #[test]
364 fn test_introduce_var_path_simple() { 364 fn test_extract_var_path_simple() {
365 check_assist( 365 check_assist(
366 introduce_variable, 366 extract_variable,
367 " 367 "
368fn main() { 368fn main() {
369 let o = <|>Some(true)<|>; 369 let o = <|>Some(true)<|>;
@@ -379,9 +379,9 @@ fn main() {
379 } 379 }
380 380
381 #[test] 381 #[test]
382 fn test_introduce_var_path_method() { 382 fn test_extract_var_path_method() {
383 check_assist( 383 check_assist(
384 introduce_variable, 384 extract_variable,
385 " 385 "
386fn main() { 386fn main() {
387 let v = <|>bar.foo()<|>; 387 let v = <|>bar.foo()<|>;
@@ -397,9 +397,9 @@ fn main() {
397 } 397 }
398 398
399 #[test] 399 #[test]
400 fn test_introduce_var_return() { 400 fn test_extract_var_return() {
401 check_assist( 401 check_assist(
402 introduce_variable, 402 extract_variable,
403 " 403 "
404fn foo() -> u32 { 404fn foo() -> u32 {
405 <|>return 2 + 2<|>; 405 <|>return 2 + 2<|>;
@@ -415,9 +415,9 @@ fn foo() -> u32 {
415 } 415 }
416 416
417 #[test] 417 #[test]
418 fn test_introduce_var_does_not_add_extra_whitespace() { 418 fn test_extract_var_does_not_add_extra_whitespace() {
419 check_assist( 419 check_assist(
420 introduce_variable, 420 extract_variable,
421 " 421 "
422fn foo() -> u32 { 422fn foo() -> u32 {
423 423
@@ -436,7 +436,7 @@ fn foo() -> u32 {
436 ); 436 );
437 437
438 check_assist( 438 check_assist(
439 introduce_variable, 439 extract_variable,
440 " 440 "
441fn foo() -> u32 { 441fn foo() -> u32 {
442 442
@@ -453,7 +453,7 @@ fn foo() -> u32 {
453 ); 453 );
454 454
455 check_assist( 455 check_assist(
456 introduce_variable, 456 extract_variable,
457 " 457 "
458fn foo() -> u32 { 458fn foo() -> u32 {
459 let foo = 1; 459 let foo = 1;
@@ -479,9 +479,9 @@ fn foo() -> u32 {
479 } 479 }
480 480
481 #[test] 481 #[test]
482 fn test_introduce_var_break() { 482 fn test_extract_var_break() {
483 check_assist( 483 check_assist(
484 introduce_variable, 484 extract_variable,
485 " 485 "
486fn main() { 486fn main() {
487 let result = loop { 487 let result = loop {
@@ -501,9 +501,9 @@ fn main() {
501 } 501 }
502 502
503 #[test] 503 #[test]
504 fn test_introduce_var_for_cast() { 504 fn test_extract_var_for_cast() {
505 check_assist( 505 check_assist(
506 introduce_variable, 506 extract_variable,
507 " 507 "
508fn main() { 508fn main() {
509 let v = <|>0f32 as u32<|>; 509 let v = <|>0f32 as u32<|>;
@@ -519,9 +519,9 @@ fn main() {
519 } 519 }
520 520
521 #[test] 521 #[test]
522 fn introduce_var_field_shorthand() { 522 fn extract_var_field_shorthand() {
523 check_assist( 523 check_assist(
524 introduce_variable, 524 extract_variable,
525 r#" 525 r#"
526struct S { 526struct S {
527 foo: i32 527 foo: i32
@@ -545,22 +545,22 @@ fn main() {
545 } 545 }
546 546
547 #[test] 547 #[test]
548 fn test_introduce_var_for_return_not_applicable() { 548 fn test_extract_var_for_return_not_applicable() {
549 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); 549 check_assist_not_applicable(extract_variable, "fn foo() { <|>return<|>; } ");
550 } 550 }
551 551
552 #[test] 552 #[test]
553 fn test_introduce_var_for_break_not_applicable() { 553 fn test_extract_var_for_break_not_applicable() {
554 check_assist_not_applicable(introduce_variable, "fn main() { loop { <|>break<|>; }; }"); 554 check_assist_not_applicable(extract_variable, "fn main() { loop { <|>break<|>; }; }");
555 } 555 }
556 556
557 // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic 557 // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic
558 #[test] 558 #[test]
559 fn introduce_var_target() { 559 fn extract_var_target() {
560 check_assist_target(introduce_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2"); 560 check_assist_target(extract_variable, "fn foo() -> u32 { <|>return 2 + 2<|>; }", "2 + 2");
561 561
562 check_assist_target( 562 check_assist_target(
563 introduce_variable, 563 extract_variable,
564 " 564 "
565fn main() { 565fn main() {
566 let x = true; 566 let x = true;
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 185428bd5..1745f44a5 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -116,6 +116,7 @@ mod handlers {
116 mod change_visibility; 116 mod change_visibility;
117 mod early_return; 117 mod early_return;
118 mod extract_struct_from_enum_variant; 118 mod extract_struct_from_enum_variant;
119 mod extract_variable;
119 mod fill_match_arms; 120 mod fill_match_arms;
120 mod fix_visibility; 121 mod fix_visibility;
121 mod flip_binexpr; 122 mod flip_binexpr;
@@ -123,7 +124,6 @@ mod handlers {
123 mod flip_trait_bound; 124 mod flip_trait_bound;
124 mod inline_local_variable; 125 mod inline_local_variable;
125 mod introduce_named_lifetime; 126 mod introduce_named_lifetime;
126 mod introduce_variable;
127 mod invert_if; 127 mod invert_if;
128 mod merge_imports; 128 mod merge_imports;
129 mod merge_match_arms; 129 mod merge_match_arms;
@@ -157,6 +157,7 @@ mod handlers {
157 change_visibility::change_visibility, 157 change_visibility::change_visibility,
158 early_return::convert_to_guarded_return, 158 early_return::convert_to_guarded_return,
159 extract_struct_from_enum_variant::extract_struct_from_enum_variant, 159 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
160 extract_variable::extract_variable,
160 fill_match_arms::fill_match_arms, 161 fill_match_arms::fill_match_arms,
161 fix_visibility::fix_visibility, 162 fix_visibility::fix_visibility,
162 flip_binexpr::flip_binexpr, 163 flip_binexpr::flip_binexpr,
@@ -164,7 +165,6 @@ mod handlers {
164 flip_trait_bound::flip_trait_bound, 165 flip_trait_bound::flip_trait_bound,
165 inline_local_variable::inline_local_variable, 166 inline_local_variable::inline_local_variable,
166 introduce_named_lifetime::introduce_named_lifetime, 167 introduce_named_lifetime::introduce_named_lifetime,
167 introduce_variable::introduce_variable,
168 invert_if::invert_if, 168 invert_if::invert_if,
169 merge_imports::merge_imports, 169 merge_imports::merge_imports,
170 merge_match_arms::merge_match_arms, 170 merge_match_arms::merge_match_arms,
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs
index 40a223727..31ea888c5 100644
--- a/crates/ra_assists/src/tests/generated.rs
+++ b/crates/ra_assists/src/tests/generated.rs
@@ -353,6 +353,24 @@ enum A { One(One) }
353} 353}
354 354
355#[test] 355#[test]
356fn doctest_extract_variable() {
357 check_doc_test(
358 "extract_variable",
359 r#####"
360fn main() {
361 <|>(1 + 2)<|> * 4;
362}
363"#####,
364 r#####"
365fn main() {
366 let $0var_name = (1 + 2);
367 var_name * 4;
368}
369"#####,
370 )
371}
372
373#[test]
356fn doctest_fill_match_arms() { 374fn doctest_fill_match_arms() {
357 check_doc_test( 375 check_doc_test(
358 "fill_match_arms", 376 "fill_match_arms",
@@ -492,24 +510,6 @@ impl<'a> Cursor<'a> {
492} 510}
493 511
494#[test] 512#[test]
495fn doctest_introduce_variable() {
496 check_doc_test(
497 "introduce_variable",
498 r#####"
499fn main() {
500 <|>(1 + 2)<|> * 4;
501}
502"#####,
503 r#####"
504fn main() {
505 let $0var_name = (1 + 2);
506 var_name * 4;
507}
508"#####,
509 )
510}
511
512#[test]
513fn doctest_invert_if() { 513fn doctest_invert_if() {
514 check_doc_test( 514 check_doc_test(
515 "invert_if", 515 "invert_if",