diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/handlers/change_return_type_to_result.rs | 81 | ||||
-rw-r--r-- | crates/ra_assists/src/tests/generated.rs | 2 |
2 files changed, 36 insertions, 47 deletions
diff --git a/crates/ra_assists/src/handlers/change_return_type_to_result.rs b/crates/ra_assists/src/handlers/change_return_type_to_result.rs index c9f909f9c..c6baa0a57 100644 --- a/crates/ra_assists/src/handlers/change_return_type_to_result.rs +++ b/crates/ra_assists/src/handlers/change_return_type_to_result.rs | |||
@@ -1,8 +1,6 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast::{self, BlockExpr, Expr, LoopBodyOwner}, | 2 | ast::{self, BlockExpr, Expr, LoopBodyOwner}, |
3 | AstNode, | 3 | AstNode, SyntaxNode, |
4 | SyntaxKind::{COMMENT, WHITESPACE}, | ||
5 | SyntaxNode, TextSize, | ||
6 | }; | 4 | }; |
7 | 5 | ||
8 | use crate::{AssistContext, AssistId, Assists}; | 6 | use crate::{AssistContext, AssistId, Assists}; |
@@ -16,7 +14,7 @@ use crate::{AssistContext, AssistId, Assists}; | |||
16 | // ``` | 14 | // ``` |
17 | // -> | 15 | // -> |
18 | // ``` | 16 | // ``` |
19 | // fn foo() -> Result<i32, > { Ok(42i32) } | 17 | // fn foo() -> Result<i32, ${0:_}> { Ok(42i32) } |
20 | // ``` | 18 | // ``` |
21 | pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 19 | pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
22 | let ret_type = ctx.find_node_at_offset::<ast::RetType>()?; | 20 | let ret_type = ctx.find_node_at_offset::<ast::RetType>()?; |
@@ -42,14 +40,14 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex | |||
42 | for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap { | 40 | for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap { |
43 | builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg)); | 41 | builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg)); |
44 | } | 42 | } |
45 | match ctx.config.snippet_cap { | ||
46 | Some(_) => {} | ||
47 | None => {} | ||
48 | } | ||
49 | builder.replace_node_and_indent(type_ref.syntax(), format!("Result<{}, >", type_ref)); | ||
50 | 43 | ||
51 | if let Some(node_start) = result_insertion_offset(&type_ref) { | 44 | match ctx.config.snippet_cap { |
52 | builder.set_cursor(node_start + TextSize::of(&format!("Result<{}, ", type_ref))); | 45 | Some(cap) => { |
46 | let snippet = format!("Result<{}, ${{0:_}}>", type_ref); | ||
47 | builder.replace_snippet(cap, type_ref.syntax().text_range(), snippet) | ||
48 | } | ||
49 | None => builder | ||
50 | .replace(type_ref.syntax().text_range(), format!("Result<{}, _>", type_ref)), | ||
53 | } | 51 | } |
54 | }, | 52 | }, |
55 | ) | 53 | ) |
@@ -251,17 +249,8 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> { | |||
251 | } | 249 | } |
252 | } | 250 | } |
253 | 251 | ||
254 | fn result_insertion_offset(ret_type: &ast::TypeRef) -> Option<TextSize> { | ||
255 | let non_ws_child = ret_type | ||
256 | .syntax() | ||
257 | .children_with_tokens() | ||
258 | .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; | ||
259 | Some(non_ws_child.text_range().start()) | ||
260 | } | ||
261 | |||
262 | #[cfg(test)] | 252 | #[cfg(test)] |
263 | mod tests { | 253 | mod tests { |
264 | |||
265 | use crate::tests::{check_assist, check_assist_not_applicable}; | 254 | use crate::tests::{check_assist, check_assist_not_applicable}; |
266 | 255 | ||
267 | use super::*; | 256 | use super::*; |
@@ -274,7 +263,7 @@ mod tests { | |||
274 | let test = "test"; | 263 | let test = "test"; |
275 | return 42i32; | 264 | return 42i32; |
276 | }"#, | 265 | }"#, |
277 | r#"fn foo() -> Result<i32, <|>> { | 266 | r#"fn foo() -> Result<i32, ${0:_}> { |
278 | let test = "test"; | 267 | let test = "test"; |
279 | return Ok(42i32); | 268 | return Ok(42i32); |
280 | }"#, | 269 | }"#, |
@@ -289,7 +278,7 @@ mod tests { | |||
289 | let test = "test"; | 278 | let test = "test"; |
290 | return 42i32; | 279 | return 42i32; |
291 | }"#, | 280 | }"#, |
292 | r#"fn foo() -> Result<i32, <|>> { | 281 | r#"fn foo() -> Result<i32, ${0:_}> { |
293 | let test = "test"; | 282 | let test = "test"; |
294 | return Ok(42i32); | 283 | return Ok(42i32); |
295 | }"#, | 284 | }"#, |
@@ -315,7 +304,7 @@ mod tests { | |||
315 | let test = "test"; | 304 | let test = "test"; |
316 | return 42i32; | 305 | return 42i32; |
317 | }"#, | 306 | }"#, |
318 | r#"fn foo() -> Result<i32, <|>> { | 307 | r#"fn foo() -> Result<i32, ${0:_}> { |
319 | let test = "test"; | 308 | let test = "test"; |
320 | return Ok(42i32); | 309 | return Ok(42i32); |
321 | }"#, | 310 | }"#, |
@@ -330,7 +319,7 @@ mod tests { | |||
330 | let test = "test"; | 319 | let test = "test"; |
331 | 42i32 | 320 | 42i32 |
332 | }"#, | 321 | }"#, |
333 | r#"fn foo() -> Result<i32, <|>> { | 322 | r#"fn foo() -> Result<i32, ${0:_}> { |
334 | let test = "test"; | 323 | let test = "test"; |
335 | Ok(42i32) | 324 | Ok(42i32) |
336 | }"#, | 325 | }"#, |
@@ -344,7 +333,7 @@ mod tests { | |||
344 | r#"fn foo() -> i32<|> { | 333 | r#"fn foo() -> i32<|> { |
345 | 42i32 | 334 | 42i32 |
346 | }"#, | 335 | }"#, |
347 | r#"fn foo() -> Result<i32, <|>> { | 336 | r#"fn foo() -> Result<i32, ${0:_}> { |
348 | Ok(42i32) | 337 | Ok(42i32) |
349 | }"#, | 338 | }"#, |
350 | ); | 339 | ); |
@@ -360,7 +349,7 @@ mod tests { | |||
360 | 24i32 | 349 | 24i32 |
361 | } | 350 | } |
362 | }"#, | 351 | }"#, |
363 | r#"fn foo() -> Result<i32, <|>> { | 352 | r#"fn foo() -> Result<i32, ${0:_}> { |
364 | if true { | 353 | if true { |
365 | Ok(42i32) | 354 | Ok(42i32) |
366 | } else { | 355 | } else { |
@@ -385,7 +374,7 @@ mod tests { | |||
385 | 24i32 | 374 | 24i32 |
386 | } | 375 | } |
387 | }"#, | 376 | }"#, |
388 | r#"fn foo() -> Result<i32, <|>> { | 377 | r#"fn foo() -> Result<i32, ${0:_}> { |
389 | if true { | 378 | if true { |
390 | if false { | 379 | if false { |
391 | Ok(1) | 380 | Ok(1) |
@@ -414,7 +403,7 @@ mod tests { | |||
414 | 24i32.await | 403 | 24i32.await |
415 | } | 404 | } |
416 | }"#, | 405 | }"#, |
417 | r#"async fn foo() -> Result<i32, <|>> { | 406 | r#"async fn foo() -> Result<i32, ${0:_}> { |
418 | if true { | 407 | if true { |
419 | if false { | 408 | if false { |
420 | Ok(1.await) | 409 | Ok(1.await) |
@@ -435,7 +424,7 @@ mod tests { | |||
435 | r#"fn foo() -> [i32;<|> 3] { | 424 | r#"fn foo() -> [i32;<|> 3] { |
436 | [1, 2, 3] | 425 | [1, 2, 3] |
437 | }"#, | 426 | }"#, |
438 | r#"fn foo() -> Result<[i32; 3], <|>> { | 427 | r#"fn foo() -> Result<[i32; 3], ${0:_}> { |
439 | Ok([1, 2, 3]) | 428 | Ok([1, 2, 3]) |
440 | }"#, | 429 | }"#, |
441 | ); | 430 | ); |
@@ -456,7 +445,7 @@ mod tests { | |||
456 | 24 as i32 | 445 | 24 as i32 |
457 | } | 446 | } |
458 | }"#, | 447 | }"#, |
459 | r#"fn foo() -> Result<i32, <|>> { | 448 | r#"fn foo() -> Result<i32, ${0:_}> { |
460 | if true { | 449 | if true { |
461 | if false { | 450 | if false { |
462 | Ok(1 as i32) | 451 | Ok(1 as i32) |
@@ -481,7 +470,7 @@ mod tests { | |||
481 | _ => 24i32, | 470 | _ => 24i32, |
482 | } | 471 | } |
483 | }"#, | 472 | }"#, |
484 | r#"fn foo() -> Result<i32, <|>> { | 473 | r#"fn foo() -> Result<i32, ${0:_}> { |
485 | let my_var = 5; | 474 | let my_var = 5; |
486 | match my_var { | 475 | match my_var { |
487 | 5 => Ok(42i32), | 476 | 5 => Ok(42i32), |
@@ -504,7 +493,7 @@ mod tests { | |||
504 | 493 | ||
505 | my_var | 494 | my_var |
506 | }"#, | 495 | }"#, |
507 | r#"fn foo() -> Result<i32, <|>> { | 496 | r#"fn foo() -> Result<i32, ${0:_}> { |
508 | let my_var = 5; | 497 | let my_var = 5; |
509 | loop { | 498 | loop { |
510 | println!("test"); | 499 | println!("test"); |
@@ -527,7 +516,7 @@ mod tests { | |||
527 | 516 | ||
528 | my_var | 517 | my_var |
529 | }"#, | 518 | }"#, |
530 | r#"fn foo() -> Result<i32, <|>> { | 519 | r#"fn foo() -> Result<i32, ${0:_}> { |
531 | let my_var = let x = loop { | 520 | let my_var = let x = loop { |
532 | break 1; | 521 | break 1; |
533 | }; | 522 | }; |
@@ -550,7 +539,7 @@ mod tests { | |||
550 | 539 | ||
551 | res | 540 | res |
552 | }"#, | 541 | }"#, |
553 | r#"fn foo() -> Result<i32, <|>> { | 542 | r#"fn foo() -> Result<i32, ${0:_}> { |
554 | let my_var = 5; | 543 | let my_var = 5; |
555 | let res = match my_var { | 544 | let res = match my_var { |
556 | 5 => 42i32, | 545 | 5 => 42i32, |
@@ -573,7 +562,7 @@ mod tests { | |||
573 | 562 | ||
574 | res | 563 | res |
575 | }"#, | 564 | }"#, |
576 | r#"fn foo() -> Result<i32, <|>> { | 565 | r#"fn foo() -> Result<i32, ${0:_}> { |
577 | let my_var = 5; | 566 | let my_var = 5; |
578 | let res = if my_var == 5 { | 567 | let res = if my_var == 5 { |
579 | 42i32 | 568 | 42i32 |
@@ -609,7 +598,7 @@ mod tests { | |||
609 | }, | 598 | }, |
610 | } | 599 | } |
611 | }"#, | 600 | }"#, |
612 | r#"fn foo() -> Result<i32, <|>> { | 601 | r#"fn foo() -> Result<i32, ${0:_}> { |
613 | let my_var = 5; | 602 | let my_var = 5; |
614 | match my_var { | 603 | match my_var { |
615 | 5 => { | 604 | 5 => { |
@@ -642,7 +631,7 @@ mod tests { | |||
642 | } | 631 | } |
643 | 53i32 | 632 | 53i32 |
644 | }"#, | 633 | }"#, |
645 | r#"fn foo() -> Result<i32, <|>> { | 634 | r#"fn foo() -> Result<i32, ${0:_}> { |
646 | let test = "test"; | 635 | let test = "test"; |
647 | if test == "test" { | 636 | if test == "test" { |
648 | return Ok(24i32); | 637 | return Ok(24i32); |
@@ -673,7 +662,7 @@ mod tests { | |||
673 | 662 | ||
674 | the_field | 663 | the_field |
675 | }"#, | 664 | }"#, |
676 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 665 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
677 | let true_closure = || { | 666 | let true_closure = || { |
678 | return true; | 667 | return true; |
679 | }; | 668 | }; |
@@ -712,7 +701,7 @@ mod tests { | |||
712 | 701 | ||
713 | t.unwrap_or_else(|| the_field) | 702 | t.unwrap_or_else(|| the_field) |
714 | }"#, | 703 | }"#, |
715 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 704 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
716 | let true_closure = || { | 705 | let true_closure = || { |
717 | return true; | 706 | return true; |
718 | }; | 707 | }; |
@@ -750,7 +739,7 @@ mod tests { | |||
750 | i += 1; | 739 | i += 1; |
751 | } | 740 | } |
752 | }"#, | 741 | }"#, |
753 | r#"fn foo() -> Result<i32, <|>> { | 742 | r#"fn foo() -> Result<i32, ${0:_}> { |
754 | let test = "test"; | 743 | let test = "test"; |
755 | if test == "test" { | 744 | if test == "test" { |
756 | return Ok(24i32); | 745 | return Ok(24i32); |
@@ -782,7 +771,7 @@ mod tests { | |||
782 | } | 771 | } |
783 | } | 772 | } |
784 | }"#, | 773 | }"#, |
785 | r#"fn foo() -> Result<i32, <|>> { | 774 | r#"fn foo() -> Result<i32, ${0:_}> { |
786 | let test = "test"; | 775 | let test = "test"; |
787 | if test == "test" { | 776 | if test == "test" { |
788 | return Ok(24i32); | 777 | return Ok(24i32); |
@@ -820,7 +809,7 @@ mod tests { | |||
820 | } | 809 | } |
821 | } | 810 | } |
822 | }"#, | 811 | }"#, |
823 | r#"fn foo() -> Result<i32, <|>> { | 812 | r#"fn foo() -> Result<i32, ${0:_}> { |
824 | let test = "test"; | 813 | let test = "test"; |
825 | let other = 5; | 814 | let other = 5; |
826 | if test == "test" { | 815 | if test == "test" { |
@@ -861,7 +850,7 @@ mod tests { | |||
861 | 850 | ||
862 | the_field | 851 | the_field |
863 | }"#, | 852 | }"#, |
864 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 853 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
865 | if the_field < 5 { | 854 | if the_field < 5 { |
866 | let mut i = 0; | 855 | let mut i = 0; |
867 | loop { | 856 | loop { |
@@ -895,7 +884,7 @@ mod tests { | |||
895 | 884 | ||
896 | the_field | 885 | the_field |
897 | }"#, | 886 | }"#, |
898 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 887 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
899 | if the_field < 5 { | 888 | if the_field < 5 { |
900 | let mut i = 0; | 889 | let mut i = 0; |
901 | 890 | ||
@@ -924,7 +913,7 @@ mod tests { | |||
924 | 913 | ||
925 | the_field | 914 | the_field |
926 | }"#, | 915 | }"#, |
927 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 916 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
928 | if the_field < 5 { | 917 | if the_field < 5 { |
929 | let mut i = 0; | 918 | let mut i = 0; |
930 | 919 | ||
@@ -954,7 +943,7 @@ mod tests { | |||
954 | 943 | ||
955 | the_field | 944 | the_field |
956 | }"#, | 945 | }"#, |
957 | r#"fn foo(the_field: u32) -> Result<u32, <|>> { | 946 | r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> { |
958 | if the_field < 5 { | 947 | if the_field < 5 { |
959 | let mut i = 0; | 948 | let mut i = 0; |
960 | 949 | ||
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index d860cfefc..cd6129dc5 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs | |||
@@ -276,7 +276,7 @@ fn doctest_change_return_type_to_result() { | |||
276 | fn foo() -> i32<|> { 42i32 } | 276 | fn foo() -> i32<|> { 42i32 } |
277 | "#####, | 277 | "#####, |
278 | r#####" | 278 | r#####" |
279 | fn foo() -> Result<i32, > { Ok(42i32) } | 279 | fn foo() -> Result<i32, ${0:_}> { Ok(42i32) } |
280 | "#####, | 280 | "#####, |
281 | ) | 281 | ) |
282 | } | 282 | } |