use ra_syntax::{
ast::{self, BlockExpr, Expr, LoopBodyOwner},
- AstNode,
- SyntaxKind::{COMMENT, WHITESPACE},
- SyntaxNode, TextSize,
+ AstNode, SyntaxNode,
};
use crate::{AssistContext, AssistId, Assists};
// ```
// ->
// ```
-// 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>()?;
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)),
}
},
)
}
}
-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::*;
let test = "test";
return 42i32;
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
return Ok(42i32);
}"#,
let test = "test";
return 42i32;
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
return Ok(42i32);
}"#,
let test = "test";
return 42i32;
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
return Ok(42i32);
}"#,
let test = "test";
42i32
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
Ok(42i32)
}"#,
r#"fn foo() -> i32<|> {
42i32
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
Ok(42i32)
}"#,
);
24i32
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
if true {
Ok(42i32)
} else {
24i32
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
if true {
if false {
Ok(1)
24i32.await
}
}"#,
- r#"async fn foo() -> Result<i32, <|>> {
+ r#"async fn foo() -> Result<i32, ${0:_}> {
if true {
if false {
Ok(1.await)
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])
}"#,
);
24 as i32
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
if true {
if false {
Ok(1 as i32)
_ => 24i32,
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = 5;
match my_var {
5 => Ok(42i32),
my_var
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = 5;
loop {
println!("test");
my_var
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = let x = loop {
break 1;
};
res
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = 5;
let res = match my_var {
5 => 42i32,
res
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = 5;
let res = if my_var == 5 {
42i32
},
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let my_var = 5;
match my_var {
5 => {
}
53i32
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
if test == "test" {
return Ok(24i32);
the_field
}"#,
- r#"fn foo(the_field: u32) -> Result<u32, <|>> {
+ r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
let true_closure = || {
return true;
};
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;
};
i += 1;
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
if test == "test" {
return Ok(24i32);
}
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
if test == "test" {
return Ok(24i32);
}
}
}"#,
- r#"fn foo() -> Result<i32, <|>> {
+ r#"fn foo() -> Result<i32, ${0:_}> {
let test = "test";
let other = 5;
if test == "test" {
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 {
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;
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;
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;