aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-27 00:25:05 +0100
committerGitHub <[email protected]>2020-06-27 00:25:05 +0100
commit2dcab133e116ef691d45a32ab5eef8ae9cfa9477 (patch)
treecadace8e7e0074454a7d9b428cf6af31443c8951 /crates/ra_assists/src/handlers
parent7488cd6a1b3604d6b4f11a7f83b6006a34cda0c0 (diff)
parentf5584668dba709a119f398de038b3f36085be5ff (diff)
Merge #5086
5086: introduce_variable -> extract_variable r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/extract_variable.rs (renamed from crates/ra_assists/src/handlers/introduce_variable.rs)106
1 files changed, 53 insertions, 53 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;