]> git.lizzy.rs Git - rust.git/commitdiff
Use snippets in change_return_type_to_result
authorAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 20 May 2020 09:10:15 +0000 (11:10 +0200)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Wed, 20 May 2020 09:10:15 +0000 (11:10 +0200)
crates/ra_assists/src/handlers/change_return_type_to_result.rs
crates/ra_assists/src/tests/generated.rs
docs/user/assists.md

index c9f909f9c5111561c4b8e415b0217af33f320753..c6baa0a57c6c6d6eb5dd84c4ca1f93e9437e3b73 100644 (file)
@@ -1,8 +1,6 @@
 use ra_syntax::{
     ast::{self, BlockExpr, Expr, LoopBodyOwner},
-    AstNode,
-    SyntaxKind::{COMMENT, WHITESPACE},
-    SyntaxNode, TextSize,
+    AstNode, SyntaxNode,
 };
 
 use crate::{AssistContext, AssistId, Assists};
@@ -16,7 +14,7 @@
 // ```
 // ->
 // ```
-// fn foo() -> Result<i32, > { Ok(42i32) }
+// fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
 // ```
 pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
     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
             for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
                 builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
             }
-            match ctx.config.snippet_cap {
-                Some(_) => {}
-                None => {}
-            }
-            builder.replace_node_and_indent(type_ref.syntax(), format!("Result<{}, >", type_ref));
 
-            if let Some(node_start) = result_insertion_offset(&type_ref) {
-                builder.set_cursor(node_start + TextSize::of(&format!("Result<{}, ", type_ref)));
+            match ctx.config.snippet_cap {
+                Some(cap) => {
+                    let snippet = format!("Result<{}, ${{0:_}}>", type_ref);
+                    builder.replace_snippet(cap, type_ref.syntax().text_range(), snippet)
+                }
+                None => builder
+                    .replace(type_ref.syntax().text_range(), format!("Result<{}, _>", type_ref)),
             }
         },
     )
@@ -251,17 +249,8 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> {
     }
 }
 
-fn result_insertion_offset(ret_type: &ast::TypeRef) -> Option<TextSize> {
-    let non_ws_child = ret_type
-        .syntax()
-        .children_with_tokens()
-        .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
-    Some(non_ws_child.text_range().start())
-}
-
 #[cfg(test)]
 mod tests {
-
     use crate::tests::{check_assist, check_assist_not_applicable};
 
     use super::*;
@@ -274,7 +263,7 @@ fn change_return_type_to_result_simple() {
                 let test = "test";
                 return 42i32;
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 return Ok(42i32);
             }"#,
@@ -289,7 +278,7 @@ fn change_return_type_to_result_simple_return_type() {
                 let test = "test";
                 return 42i32;
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 return Ok(42i32);
             }"#,
@@ -315,7 +304,7 @@ fn change_return_type_to_result_simple_with_cursor() {
                 let test = "test";
                 return 42i32;
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 return Ok(42i32);
             }"#,
@@ -330,7 +319,7 @@ fn change_return_type_to_result_simple_with_tail() {
                 let test = "test";
                 42i32
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 Ok(42i32)
             }"#,
@@ -344,7 +333,7 @@ fn change_return_type_to_result_simple_with_tail_only() {
             r#"fn foo() -> i32<|> {
                 42i32
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 Ok(42i32)
             }"#,
         );
@@ -360,7 +349,7 @@ fn change_return_type_to_result_simple_with_tail_block_like() {
                     24i32
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 if true {
                     Ok(42i32)
                 } else {
@@ -385,7 +374,7 @@ fn change_return_type_to_result_simple_with_nested_if() {
                     24i32
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 if true {
                     if false {
                         Ok(1)
@@ -414,7 +403,7 @@ fn change_return_type_to_result_simple_with_await() {
                     24i32.await
                 }
             }"#,
-            r#"async fn foo() -> Result<i32, <|>> {
+            r#"async fn foo() -> Result<i32, ${0:_}> {
                 if true {
                     if false {
                         Ok(1.await)
@@ -435,7 +424,7 @@ fn change_return_type_to_result_simple_with_array() {
             r#"fn foo() -> [i32;<|> 3] {
                 [1, 2, 3]
             }"#,
-            r#"fn foo() -> Result<[i32; 3], <|>> {
+            r#"fn foo() -> Result<[i32; 3], ${0:_}> {
                 Ok([1, 2, 3])
             }"#,
         );
@@ -456,7 +445,7 @@ fn change_return_type_to_result_simple_with_cast() {
                     24 as i32
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 if true {
                     if false {
                         Ok(1 as i32)
@@ -481,7 +470,7 @@ fn change_return_type_to_result_simple_with_tail_block_like_match() {
                     _ => 24i32,
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = 5;
                 match my_var {
                     5 => Ok(42i32),
@@ -504,7 +493,7 @@ fn change_return_type_to_result_simple_with_loop_with_tail() {
 
                 my_var
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = 5;
                 loop {
                     println!("test");
@@ -527,7 +516,7 @@ fn change_return_type_to_result_simple_with_loop_in_let_stmt() {
 
                 my_var
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = let x = loop {
                     break 1;
                 };
@@ -550,7 +539,7 @@ fn change_return_type_to_result_simple_with_tail_block_like_match_return_expr()
 
                 res
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = 5;
                 let res = match my_var {
                     5 => 42i32,
@@ -573,7 +562,7 @@ fn change_return_type_to_result_simple_with_tail_block_like_match_return_expr()
 
                 res
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = 5;
                 let res = if my_var == 5 {
                     42i32
@@ -609,7 +598,7 @@ fn change_return_type_to_result_simple_with_tail_block_like_match_deeper() {
                     },
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let my_var = 5;
                 match my_var {
                     5 => {
@@ -642,7 +631,7 @@ fn change_return_type_to_result_simple_with_tail_block_like_early_return() {
                 }
                 53i32
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 if test == "test" {
                     return Ok(24i32);
@@ -673,7 +662,7 @@ fn change_return_type_to_result_simple_with_closure() {
 
                 the_field
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 let true_closure = || {
                     return true;
                 };
@@ -712,7 +701,7 @@ fn change_return_type_to_result_simple_with_closure() {
 
                 t.unwrap_or_else(|| the_field)
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 let true_closure = || {
                     return true;
                 };
@@ -750,7 +739,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
                     i += 1;
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 if test == "test" {
                     return Ok(24i32);
@@ -782,7 +771,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
                     }
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 if test == "test" {
                     return Ok(24i32);
@@ -820,7 +809,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
                     }
                 }
             }"#,
-            r#"fn foo() -> Result<i32, <|>> {
+            r#"fn foo() -> Result<i32, ${0:_}> {
                 let test = "test";
                 let other = 5;
                 if test == "test" {
@@ -861,7 +850,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
 
                 the_field
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 if the_field < 5 {
                     let mut i = 0;
                     loop {
@@ -895,7 +884,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
 
                 the_field
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 if the_field < 5 {
                     let mut i = 0;
 
@@ -924,7 +913,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
 
                 the_field
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 if the_field < 5 {
                     let mut i = 0;
 
@@ -954,7 +943,7 @@ fn change_return_type_to_result_simple_with_weird_forms() {
 
                 the_field
             }"#,
-            r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+            r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
                 if the_field < 5 {
                     let mut i = 0;
 
index d860cfefcc7ef834928a54331e31b1d94b51e613..cd6129dc58916aa10bd44cc5b2865ae8a29beaf6 100644 (file)
@@ -276,7 +276,7 @@ fn doctest_change_return_type_to_result() {
 fn foo() -> i32<|> { 42i32 }
 "#####,
         r#####"
-fn foo() -> Result<i32, > { Ok(42i32) }
+fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
 "#####,
     )
 }
index 03c01d6c05aaf64dcfa51952c435950e00411fea..006ec4d547e591c82bffa94c723cb54a44fe4ea4 100644 (file)
@@ -268,7 +268,7 @@ Change the function's return type to Result.
 fn foo() -> i32┃ { 42i32 }
 
 // AFTER
-fn foo() -> Result<i32, > { Ok(42i32) }
+fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
 ```
 
 ## `change_visibility`