aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r--crates/ra_assists/src/handlers/inline_local_variable.rs122
-rw-r--r--crates/ra_assists/src/lib.rs16
-rw-r--r--crates/ra_assists/src/marks.rs2
3 files changed, 84 insertions, 56 deletions
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs
index 53a72309b..eb5112343 100644
--- a/crates/ra_assists/src/handlers/inline_local_variable.rs
+++ b/crates/ra_assists/src/handlers/inline_local_variable.rs
@@ -2,9 +2,9 @@ use ra_syntax::{
2 ast::{self, AstNode, AstToken}, 2 ast::{self, AstNode, AstToken},
3 TextRange, 3 TextRange,
4}; 4};
5use test_utils::tested_by;
5 6
6use crate::assist_ctx::ActionBuilder; 7use crate::{assist_ctx::ActionBuilder, Assist, AssistCtx, AssistId};
7use crate::{Assist, AssistCtx, AssistId};
8 8
9// Assist: inline_local_variable 9// Assist: inline_local_variable
10// 10//
@@ -29,6 +29,11 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
29 _ => return None, 29 _ => return None,
30 }; 30 };
31 if bind_pat.is_mutable() { 31 if bind_pat.is_mutable() {
32 tested_by!(test_not_inline_mut_variable);
33 return None;
34 }
35 if !bind_pat.syntax().text_range().contains_inclusive(ctx.frange.range.start()) {
36 tested_by!(not_applicable_outside_of_bind_pat);
32 return None; 37 return None;
33 } 38 }
34 let initializer_expr = let_stmt.initializer()?; 39 let initializer_expr = let_stmt.initializer()?;
@@ -111,6 +116,8 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
111 116
112#[cfg(test)] 117#[cfg(test)]
113mod tests { 118mod tests {
119 use test_utils::covers;
120
114 use crate::helpers::{check_assist, check_assist_not_applicable}; 121 use crate::helpers::{check_assist, check_assist_not_applicable};
115 122
116 use super::*; 123 use super::*;
@@ -119,7 +126,7 @@ mod tests {
119 fn test_inline_let_bind_literal_expr() { 126 fn test_inline_let_bind_literal_expr() {
120 check_assist( 127 check_assist(
121 inline_local_variable, 128 inline_local_variable,
122 " 129 r"
123fn bar(a: usize) {} 130fn bar(a: usize) {}
124fn foo() { 131fn foo() {
125 let a<|> = 1; 132 let a<|> = 1;
@@ -133,7 +140,7 @@ fn foo() {
133 let b = a * 10; 140 let b = a * 10;
134 bar(a); 141 bar(a);
135}", 142}",
136 " 143 r"
137fn bar(a: usize) {} 144fn bar(a: usize) {}
138fn foo() { 145fn foo() {
139 <|>1 + 1; 146 <|>1 + 1;
@@ -153,7 +160,7 @@ fn foo() {
153 fn test_inline_let_bind_bin_expr() { 160 fn test_inline_let_bind_bin_expr() {
154 check_assist( 161 check_assist(
155 inline_local_variable, 162 inline_local_variable,
156 " 163 r"
157fn bar(a: usize) {} 164fn bar(a: usize) {}
158fn foo() { 165fn foo() {
159 let a<|> = 1 + 1; 166 let a<|> = 1 + 1;
@@ -167,7 +174,7 @@ fn foo() {
167 let b = a * 10; 174 let b = a * 10;
168 bar(a); 175 bar(a);
169}", 176}",
170 " 177 r"
171fn bar(a: usize) {} 178fn bar(a: usize) {}
172fn foo() { 179fn foo() {
173 <|>(1 + 1) + 1; 180 <|>(1 + 1) + 1;
@@ -187,7 +194,7 @@ fn foo() {
187 fn test_inline_let_bind_function_call_expr() { 194 fn test_inline_let_bind_function_call_expr() {
188 check_assist( 195 check_assist(
189 inline_local_variable, 196 inline_local_variable,
190 " 197 r"
191fn bar(a: usize) {} 198fn bar(a: usize) {}
192fn foo() { 199fn foo() {
193 let a<|> = bar(1); 200 let a<|> = bar(1);
@@ -201,7 +208,7 @@ fn foo() {
201 let b = a * 10; 208 let b = a * 10;
202 bar(a); 209 bar(a);
203}", 210}",
204 " 211 r"
205fn bar(a: usize) {} 212fn bar(a: usize) {}
206fn foo() { 213fn foo() {
207 <|>bar(1) + 1; 214 <|>bar(1) + 1;
@@ -221,7 +228,7 @@ fn foo() {
221 fn test_inline_let_bind_cast_expr() { 228 fn test_inline_let_bind_cast_expr() {
222 check_assist( 229 check_assist(
223 inline_local_variable, 230 inline_local_variable,
224 " 231 r"
225fn bar(a: usize): usize { a } 232fn bar(a: usize): usize { a }
226fn foo() { 233fn foo() {
227 let a<|> = bar(1) as u64; 234 let a<|> = bar(1) as u64;
@@ -235,7 +242,7 @@ fn foo() {
235 let b = a * 10; 242 let b = a * 10;
236 bar(a); 243 bar(a);
237}", 244}",
238 " 245 r"
239fn bar(a: usize): usize { a } 246fn bar(a: usize): usize { a }
240fn foo() { 247fn foo() {
241 <|>(bar(1) as u64) + 1; 248 <|>(bar(1) as u64) + 1;
@@ -255,7 +262,7 @@ fn foo() {
255 fn test_inline_let_bind_block_expr() { 262 fn test_inline_let_bind_block_expr() {
256 check_assist( 263 check_assist(
257 inline_local_variable, 264 inline_local_variable,
258 " 265 r"
259fn foo() { 266fn foo() {
260 let a<|> = { 10 + 1 }; 267 let a<|> = { 10 + 1 };
261 a + 1; 268 a + 1;
@@ -268,7 +275,7 @@ fn foo() {
268 let b = a * 10; 275 let b = a * 10;
269 bar(a); 276 bar(a);
270}", 277}",
271 " 278 r"
272fn foo() { 279fn foo() {
273 <|>{ 10 + 1 } + 1; 280 <|>{ 10 + 1 } + 1;
274 if { 10 + 1 } > 10 { 281 if { 10 + 1 } > 10 {
@@ -287,7 +294,7 @@ fn foo() {
287 fn test_inline_let_bind_paren_expr() { 294 fn test_inline_let_bind_paren_expr() {
288 check_assist( 295 check_assist(
289 inline_local_variable, 296 inline_local_variable,
290 " 297 r"
291fn foo() { 298fn foo() {
292 let a<|> = ( 10 + 1 ); 299 let a<|> = ( 10 + 1 );
293 a + 1; 300 a + 1;
@@ -300,7 +307,7 @@ fn foo() {
300 let b = a * 10; 307 let b = a * 10;
301 bar(a); 308 bar(a);
302}", 309}",
303 " 310 r"
304fn foo() { 311fn foo() {
305 <|>( 10 + 1 ) + 1; 312 <|>( 10 + 1 ) + 1;
306 if ( 10 + 1 ) > 10 { 313 if ( 10 + 1 ) > 10 {
@@ -317,9 +324,10 @@ fn foo() {
317 324
318 #[test] 325 #[test]
319 fn test_not_inline_mut_variable() { 326 fn test_not_inline_mut_variable() {
327 covers!(test_not_inline_mut_variable);
320 check_assist_not_applicable( 328 check_assist_not_applicable(
321 inline_local_variable, 329 inline_local_variable,
322 " 330 r"
323fn foo() { 331fn foo() {
324 let mut a<|> = 1 + 1; 332 let mut a<|> = 1 + 1;
325 a + 1; 333 a + 1;
@@ -331,13 +339,13 @@ fn foo() {
331 fn test_call_expr() { 339 fn test_call_expr() {
332 check_assist( 340 check_assist(
333 inline_local_variable, 341 inline_local_variable,
334 " 342 r"
335fn foo() { 343fn foo() {
336 let a<|> = bar(10 + 1); 344 let a<|> = bar(10 + 1);
337 let b = a * 10; 345 let b = a * 10;
338 let c = a as usize; 346 let c = a as usize;
339}", 347}",
340 " 348 r"
341fn foo() { 349fn foo() {
342 <|>let b = bar(10 + 1) * 10; 350 <|>let b = bar(10 + 1) * 10;
343 let c = bar(10 + 1) as usize; 351 let c = bar(10 + 1) as usize;
@@ -349,14 +357,14 @@ fn foo() {
349 fn test_index_expr() { 357 fn test_index_expr() {
350 check_assist( 358 check_assist(
351 inline_local_variable, 359 inline_local_variable,
352 " 360 r"
353fn foo() { 361fn foo() {
354 let x = vec![1, 2, 3]; 362 let x = vec![1, 2, 3];
355 let a<|> = x[0]; 363 let a<|> = x[0];
356 let b = a * 10; 364 let b = a * 10;
357 let c = a as usize; 365 let c = a as usize;
358}", 366}",
359 " 367 r"
360fn foo() { 368fn foo() {
361 let x = vec![1, 2, 3]; 369 let x = vec![1, 2, 3];
362 <|>let b = x[0] * 10; 370 <|>let b = x[0] * 10;
@@ -369,14 +377,14 @@ fn foo() {
369 fn test_method_call_expr() { 377 fn test_method_call_expr() {
370 check_assist( 378 check_assist(
371 inline_local_variable, 379 inline_local_variable,
372 " 380 r"
373fn foo() { 381fn foo() {
374 let bar = vec![1]; 382 let bar = vec![1];
375 let a<|> = bar.len(); 383 let a<|> = bar.len();
376 let b = a * 10; 384 let b = a * 10;
377 let c = a as usize; 385 let c = a as usize;
378}", 386}",
379 " 387 r"
380fn foo() { 388fn foo() {
381 let bar = vec![1]; 389 let bar = vec![1];
382 <|>let b = bar.len() * 10; 390 <|>let b = bar.len() * 10;
@@ -389,7 +397,7 @@ fn foo() {
389 fn test_field_expr() { 397 fn test_field_expr() {
390 check_assist( 398 check_assist(
391 inline_local_variable, 399 inline_local_variable,
392 " 400 r"
393struct Bar { 401struct Bar {
394 foo: usize 402 foo: usize
395} 403}
@@ -400,7 +408,7 @@ fn foo() {
400 let b = a * 10; 408 let b = a * 10;
401 let c = a as usize; 409 let c = a as usize;
402}", 410}",
403 " 411 r"
404struct Bar { 412struct Bar {
405 foo: usize 413 foo: usize
406} 414}
@@ -417,7 +425,7 @@ fn foo() {
417 fn test_try_expr() { 425 fn test_try_expr() {
418 check_assist( 426 check_assist(
419 inline_local_variable, 427 inline_local_variable,
420 " 428 r"
421fn foo() -> Option<usize> { 429fn foo() -> Option<usize> {
422 let bar = Some(1); 430 let bar = Some(1);
423 let a<|> = bar?; 431 let a<|> = bar?;
@@ -425,7 +433,7 @@ fn foo() -> Option<usize> {
425 let c = a as usize; 433 let c = a as usize;
426 None 434 None
427}", 435}",
428 " 436 r"
429fn foo() -> Option<usize> { 437fn foo() -> Option<usize> {
430 let bar = Some(1); 438 let bar = Some(1);
431 <|>let b = bar? * 10; 439 <|>let b = bar? * 10;
@@ -439,13 +447,13 @@ fn foo() -> Option<usize> {
439 fn test_ref_expr() { 447 fn test_ref_expr() {
440 check_assist( 448 check_assist(
441 inline_local_variable, 449 inline_local_variable,
442 " 450 r"
443fn foo() { 451fn foo() {
444 let bar = 10; 452 let bar = 10;
445 let a<|> = &bar; 453 let a<|> = &bar;
446 let b = a * 10; 454 let b = a * 10;
447}", 455}",
448 " 456 r"
449fn foo() { 457fn foo() {
450 let bar = 10; 458 let bar = 10;
451 <|>let b = &bar * 10; 459 <|>let b = &bar * 10;
@@ -457,12 +465,12 @@ fn foo() {
457 fn test_tuple_expr() { 465 fn test_tuple_expr() {
458 check_assist( 466 check_assist(
459 inline_local_variable, 467 inline_local_variable,
460 " 468 r"
461fn foo() { 469fn foo() {
462 let a<|> = (10, 20); 470 let a<|> = (10, 20);
463 let b = a[0]; 471 let b = a[0];
464}", 472}",
465 " 473 r"
466fn foo() { 474fn foo() {
467 <|>let b = (10, 20)[0]; 475 <|>let b = (10, 20)[0];
468}", 476}",
@@ -473,12 +481,12 @@ fn foo() {
473 fn test_array_expr() { 481 fn test_array_expr() {
474 check_assist( 482 check_assist(
475 inline_local_variable, 483 inline_local_variable,
476 " 484 r"
477fn foo() { 485fn foo() {
478 let a<|> = [1, 2, 3]; 486 let a<|> = [1, 2, 3];
479 let b = a.len(); 487 let b = a.len();
480}", 488}",
481 " 489 r"
482fn foo() { 490fn foo() {
483 <|>let b = [1, 2, 3].len(); 491 <|>let b = [1, 2, 3].len();
484}", 492}",
@@ -489,13 +497,13 @@ fn foo() {
489 fn test_paren() { 497 fn test_paren() {
490 check_assist( 498 check_assist(
491 inline_local_variable, 499 inline_local_variable,
492 " 500 r"
493fn foo() { 501fn foo() {
494 let a<|> = (10 + 20); 502 let a<|> = (10 + 20);
495 let b = a * 10; 503 let b = a * 10;
496 let c = a as usize; 504 let c = a as usize;
497}", 505}",
498 " 506 r"
499fn foo() { 507fn foo() {
500 <|>let b = (10 + 20) * 10; 508 <|>let b = (10 + 20) * 10;
501 let c = (10 + 20) as usize; 509 let c = (10 + 20) as usize;
@@ -507,14 +515,14 @@ fn foo() {
507 fn test_path_expr() { 515 fn test_path_expr() {
508 check_assist( 516 check_assist(
509 inline_local_variable, 517 inline_local_variable,
510 " 518 r"
511fn foo() { 519fn foo() {
512 let d = 10; 520 let d = 10;
513 let a<|> = d; 521 let a<|> = d;
514 let b = a * 10; 522 let b = a * 10;
515 let c = a as usize; 523 let c = a as usize;
516}", 524}",
517 " 525 r"
518fn foo() { 526fn foo() {
519 let d = 10; 527 let d = 10;
520 <|>let b = d * 10; 528 <|>let b = d * 10;
@@ -527,13 +535,13 @@ fn foo() {
527 fn test_block_expr() { 535 fn test_block_expr() {
528 check_assist( 536 check_assist(
529 inline_local_variable, 537 inline_local_variable,
530 " 538 r"
531fn foo() { 539fn foo() {
532 let a<|> = { 10 }; 540 let a<|> = { 10 };
533 let b = a * 10; 541 let b = a * 10;
534 let c = a as usize; 542 let c = a as usize;
535}", 543}",
536 " 544 r"
537fn foo() { 545fn foo() {
538 <|>let b = { 10 } * 10; 546 <|>let b = { 10 } * 10;
539 let c = { 10 } as usize; 547 let c = { 10 } as usize;
@@ -545,7 +553,7 @@ fn foo() {
545 fn test_used_in_different_expr1() { 553 fn test_used_in_different_expr1() {
546 check_assist( 554 check_assist(
547 inline_local_variable, 555 inline_local_variable,
548 " 556 r"
549fn foo() { 557fn foo() {
550 let a<|> = 10 + 20; 558 let a<|> = 10 + 20;
551 let b = a * 10; 559 let b = a * 10;
@@ -553,7 +561,7 @@ fn foo() {
553 let d = [a, 10]; 561 let d = [a, 10];
554 let e = (a); 562 let e = (a);
555}", 563}",
556 " 564 r"
557fn foo() { 565fn foo() {
558 <|>let b = (10 + 20) * 10; 566 <|>let b = (10 + 20) * 10;
559 let c = (10 + 20, 20); 567 let c = (10 + 20, 20);
@@ -567,12 +575,12 @@ fn foo() {
567 fn test_used_in_for_expr() { 575 fn test_used_in_for_expr() {
568 check_assist( 576 check_assist(
569 inline_local_variable, 577 inline_local_variable,
570 " 578 r"
571fn foo() { 579fn foo() {
572 let a<|> = vec![10, 20]; 580 let a<|> = vec![10, 20];
573 for i in a {} 581 for i in a {}
574}", 582}",
575 " 583 r"
576fn foo() { 584fn foo() {
577 <|>for i in vec![10, 20] {} 585 <|>for i in vec![10, 20] {}
578}", 586}",
@@ -583,12 +591,12 @@ fn foo() {
583 fn test_used_in_while_expr() { 591 fn test_used_in_while_expr() {
584 check_assist( 592 check_assist(
585 inline_local_variable, 593 inline_local_variable,
586 " 594 r"
587fn foo() { 595fn foo() {
588 let a<|> = 1 > 0; 596 let a<|> = 1 > 0;
589 while a {} 597 while a {}
590}", 598}",
591 " 599 r"
592fn foo() { 600fn foo() {
593 <|>while 1 > 0 {} 601 <|>while 1 > 0 {}
594}", 602}",
@@ -599,14 +607,14 @@ fn foo() {
599 fn test_used_in_break_expr() { 607 fn test_used_in_break_expr() {
600 check_assist( 608 check_assist(
601 inline_local_variable, 609 inline_local_variable,
602 " 610 r"
603fn foo() { 611fn foo() {
604 let a<|> = 1 + 1; 612 let a<|> = 1 + 1;
605 loop { 613 loop {
606 break a; 614 break a;
607 } 615 }
608}", 616}",
609 " 617 r"
610fn foo() { 618fn foo() {
611 <|>loop { 619 <|>loop {
612 break 1 + 1; 620 break 1 + 1;
@@ -619,12 +627,12 @@ fn foo() {
619 fn test_used_in_return_expr() { 627 fn test_used_in_return_expr() {
620 check_assist( 628 check_assist(
621 inline_local_variable, 629 inline_local_variable,
622 " 630 r"
623fn foo() { 631fn foo() {
624 let a<|> = 1 > 0; 632 let a<|> = 1 > 0;
625 return a; 633 return a;
626}", 634}",
627 " 635 r"
628fn foo() { 636fn foo() {
629 <|>return 1 > 0; 637 <|>return 1 > 0;
630}", 638}",
@@ -635,12 +643,12 @@ fn foo() {
635 fn test_used_in_match_expr() { 643 fn test_used_in_match_expr() {
636 check_assist( 644 check_assist(
637 inline_local_variable, 645 inline_local_variable,
638 " 646 r"
639fn foo() { 647fn foo() {
640 let a<|> = 1 > 0; 648 let a<|> = 1 > 0;
641 match a {} 649 match a {}
642}", 650}",
643 " 651 r"
644fn foo() { 652fn foo() {
645 <|>match 1 > 0 {} 653 <|>match 1 > 0 {}
646}", 654}",
@@ -651,11 +659,25 @@ fn foo() {
651 fn test_not_applicable_if_variable_unused() { 659 fn test_not_applicable_if_variable_unused() {
652 check_assist_not_applicable( 660 check_assist_not_applicable(
653 inline_local_variable, 661 inline_local_variable,
654 " 662 r"
655fn foo() { 663fn foo() {
656 let <|>a = 0; 664 let <|>a = 0;
657} 665}
658 ", 666 ",
659 ) 667 )
660 } 668 }
669
670 #[test]
671 fn not_applicable_outside_of_bind_pat() {
672 covers!(not_applicable_outside_of_bind_pat);
673 check_assist_not_applicable(
674 inline_local_variable,
675 r"
676fn main() {
677 let x = <|>1 + 2;
678 x * 4;
679}
680",
681 )
682 }
661} 683}
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index deeada2de..50a15f978 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -178,19 +178,23 @@ mod helpers {
178 (db, file_id) 178 (db, file_id)
179 } 179 }
180 180
181 pub(crate) fn check_assist(assist: AssistHandler, before: &str, after: &str) { 181 pub(crate) fn check_assist(
182 check(assist, before, ExpectedResult::After(after)); 182 assist: AssistHandler,
183 ra_fixture_before: &str,
184 ra_fixture_after: &str,
185 ) {
186 check(assist, ra_fixture_before, ExpectedResult::After(ra_fixture_after));
183 } 187 }
184 188
185 // FIXME: instead of having a separate function here, maybe use 189 // FIXME: instead of having a separate function here, maybe use
186 // `extract_ranges` and mark the target as `<target> </target>` in the 190 // `extract_ranges` and mark the target as `<target> </target>` in the
187 // fixuture? 191 // fixuture?
188 pub(crate) fn check_assist_target(assist: AssistHandler, before: &str, target: &str) { 192 pub(crate) fn check_assist_target(assist: AssistHandler, ra_fixture: &str, target: &str) {
189 check(assist, before, ExpectedResult::Target(target)); 193 check(assist, ra_fixture, ExpectedResult::Target(target));
190 } 194 }
191 195
192 pub(crate) fn check_assist_not_applicable(assist: AssistHandler, before: &str) { 196 pub(crate) fn check_assist_not_applicable(assist: AssistHandler, ra_fixture: &str) {
193 check(assist, before, ExpectedResult::NotApplicable); 197 check(assist, ra_fixture, ExpectedResult::NotApplicable);
194 } 198 }
195 199
196 enum ExpectedResult<'a> { 200 enum ExpectedResult<'a> {
diff --git a/crates/ra_assists/src/marks.rs b/crates/ra_assists/src/marks.rs
index c20e4db9e..cef3df4e5 100644
--- a/crates/ra_assists/src/marks.rs
+++ b/crates/ra_assists/src/marks.rs
@@ -4,4 +4,6 @@ test_utils::marks!(
4 introduce_var_in_comment_is_not_applicable 4 introduce_var_in_comment_is_not_applicable
5 test_introduce_var_expr_stmt 5 test_introduce_var_expr_stmt
6 test_introduce_var_last_expr 6 test_introduce_var_last_expr
7 not_applicable_outside_of_bind_pat
8 test_not_inline_mut_variable
7); 9);