From: Aleksey Kladov Date: Wed, 20 May 2020 09:10:15 +0000 (+0200) Subject: Use snippets in change_return_type_to_result X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=33e111483fbc80c017037e0b158ee652ed41b3e8;p=rust.git Use snippets in change_return_type_to_result --- 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 c9f909f9c51..c6baa0a57c6 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 @@ 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 { Ok(42i32) } +// fn foo() -> Result { Ok(42i32) } // ``` pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let ret_type = ctx.find_node_at_offset::()?; @@ -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> { } } -fn result_insertion_offset(ret_type: &ast::TypeRef) -> Option { - 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { Ok(42i32) }"#, ); @@ -360,7 +349,7 @@ fn change_return_type_to_result_simple_with_tail_block_like() { 24i32 } }"#, - r#"fn foo() -> Result> { + r#"fn foo() -> Result { if true { Ok(42i32) } else { @@ -385,7 +374,7 @@ fn change_return_type_to_result_simple_with_nested_if() { 24i32 } }"#, - r#"fn foo() -> Result> { + r#"fn foo() -> Result { 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> { + r#"async fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo() -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { 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> { + r#"fn foo(the_field: u32) -> Result { if the_field < 5 { let mut i = 0; diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index d860cfefcc7..cd6129dc589 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() { fn foo() -> i32<|> { 42i32 } "#####, r#####" -fn foo() -> Result { Ok(42i32) } +fn foo() -> Result { Ok(42i32) } "#####, ) } diff --git a/docs/user/assists.md b/docs/user/assists.md index 03c01d6c05a..006ec4d547e 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -268,7 +268,7 @@ Change the function's return type to Result. fn foo() -> i32┃ { 42i32 } // AFTER -fn foo() -> Result { Ok(42i32) } +fn foo() -> Result { Ok(42i32) } ``` ## `change_visibility`