Skip to content

Commit 33e1114

Browse files
committed
Use snippets in change_return_type_to_result
1 parent d790a44 commit 33e1114

File tree

3 files changed

+37
-48
lines changed

3 files changed

+37
-48
lines changed

crates/ra_assists/src/handlers/change_return_type_to_result.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use ra_syntax::{
22
ast::{self, BlockExpr, Expr, LoopBodyOwner},
3-
AstNode,
4-
SyntaxKind::{COMMENT, WHITESPACE},
5-
SyntaxNode, TextSize,
3+
AstNode, SyntaxNode,
64
};
75

86
use crate::{AssistContext, AssistId, Assists};
@@ -16,7 +14,7 @@ use crate::{AssistContext, AssistId, Assists};
1614
// ```
1715
// ->
1816
// ```
19-
// fn foo() -> Result<i32, > { Ok(42i32) }
17+
// fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
2018
// ```
2119
pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2220
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
4240
for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
4341
builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
4442
}
45-
match ctx.config.snippet_cap {
46-
Some(_) => {}
47-
None => {}
48-
}
49-
builder.replace_node_and_indent(type_ref.syntax(), format!("Result<{}, >", type_ref));
5043

51-
if let Some(node_start) = result_insertion_offset(&type_ref) {
52-
builder.set_cursor(node_start + TextSize::of(&format!("Result<{}, ", type_ref)));
44+
match ctx.config.snippet_cap {
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)),
5351
}
5452
},
5553
)
@@ -251,17 +249,8 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> {
251249
}
252250
}
253251

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-
262252
#[cfg(test)]
263253
mod tests {
264-
265254
use crate::tests::{check_assist, check_assist_not_applicable};
266255

267256
use super::*;
@@ -274,7 +263,7 @@ mod tests {
274263
let test = "test";
275264
return 42i32;
276265
}"#,
277-
r#"fn foo() -> Result<i32, <|>> {
266+
r#"fn foo() -> Result<i32, ${0:_}> {
278267
let test = "test";
279268
return Ok(42i32);
280269
}"#,
@@ -289,7 +278,7 @@ mod tests {
289278
let test = "test";
290279
return 42i32;
291280
}"#,
292-
r#"fn foo() -> Result<i32, <|>> {
281+
r#"fn foo() -> Result<i32, ${0:_}> {
293282
let test = "test";
294283
return Ok(42i32);
295284
}"#,
@@ -315,7 +304,7 @@ mod tests {
315304
let test = "test";
316305
return 42i32;
317306
}"#,
318-
r#"fn foo() -> Result<i32, <|>> {
307+
r#"fn foo() -> Result<i32, ${0:_}> {
319308
let test = "test";
320309
return Ok(42i32);
321310
}"#,
@@ -330,7 +319,7 @@ mod tests {
330319
let test = "test";
331320
42i32
332321
}"#,
333-
r#"fn foo() -> Result<i32, <|>> {
322+
r#"fn foo() -> Result<i32, ${0:_}> {
334323
let test = "test";
335324
Ok(42i32)
336325
}"#,
@@ -344,7 +333,7 @@ mod tests {
344333
r#"fn foo() -> i32<|> {
345334
42i32
346335
}"#,
347-
r#"fn foo() -> Result<i32, <|>> {
336+
r#"fn foo() -> Result<i32, ${0:_}> {
348337
Ok(42i32)
349338
}"#,
350339
);
@@ -360,7 +349,7 @@ mod tests {
360349
24i32
361350
}
362351
}"#,
363-
r#"fn foo() -> Result<i32, <|>> {
352+
r#"fn foo() -> Result<i32, ${0:_}> {
364353
if true {
365354
Ok(42i32)
366355
} else {
@@ -385,7 +374,7 @@ mod tests {
385374
24i32
386375
}
387376
}"#,
388-
r#"fn foo() -> Result<i32, <|>> {
377+
r#"fn foo() -> Result<i32, ${0:_}> {
389378
if true {
390379
if false {
391380
Ok(1)
@@ -414,7 +403,7 @@ mod tests {
414403
24i32.await
415404
}
416405
}"#,
417-
r#"async fn foo() -> Result<i32, <|>> {
406+
r#"async fn foo() -> Result<i32, ${0:_}> {
418407
if true {
419408
if false {
420409
Ok(1.await)
@@ -435,7 +424,7 @@ mod tests {
435424
r#"fn foo() -> [i32;<|> 3] {
436425
[1, 2, 3]
437426
}"#,
438-
r#"fn foo() -> Result<[i32; 3], <|>> {
427+
r#"fn foo() -> Result<[i32; 3], ${0:_}> {
439428
Ok([1, 2, 3])
440429
}"#,
441430
);
@@ -456,7 +445,7 @@ mod tests {
456445
24 as i32
457446
}
458447
}"#,
459-
r#"fn foo() -> Result<i32, <|>> {
448+
r#"fn foo() -> Result<i32, ${0:_}> {
460449
if true {
461450
if false {
462451
Ok(1 as i32)
@@ -481,7 +470,7 @@ mod tests {
481470
_ => 24i32,
482471
}
483472
}"#,
484-
r#"fn foo() -> Result<i32, <|>> {
473+
r#"fn foo() -> Result<i32, ${0:_}> {
485474
let my_var = 5;
486475
match my_var {
487476
5 => Ok(42i32),
@@ -504,7 +493,7 @@ mod tests {
504493
505494
my_var
506495
}"#,
507-
r#"fn foo() -> Result<i32, <|>> {
496+
r#"fn foo() -> Result<i32, ${0:_}> {
508497
let my_var = 5;
509498
loop {
510499
println!("test");
@@ -527,7 +516,7 @@ mod tests {
527516
528517
my_var
529518
}"#,
530-
r#"fn foo() -> Result<i32, <|>> {
519+
r#"fn foo() -> Result<i32, ${0:_}> {
531520
let my_var = let x = loop {
532521
break 1;
533522
};
@@ -550,7 +539,7 @@ mod tests {
550539
551540
res
552541
}"#,
553-
r#"fn foo() -> Result<i32, <|>> {
542+
r#"fn foo() -> Result<i32, ${0:_}> {
554543
let my_var = 5;
555544
let res = match my_var {
556545
5 => 42i32,
@@ -573,7 +562,7 @@ mod tests {
573562
574563
res
575564
}"#,
576-
r#"fn foo() -> Result<i32, <|>> {
565+
r#"fn foo() -> Result<i32, ${0:_}> {
577566
let my_var = 5;
578567
let res = if my_var == 5 {
579568
42i32
@@ -609,7 +598,7 @@ mod tests {
609598
},
610599
}
611600
}"#,
612-
r#"fn foo() -> Result<i32, <|>> {
601+
r#"fn foo() -> Result<i32, ${0:_}> {
613602
let my_var = 5;
614603
match my_var {
615604
5 => {
@@ -642,7 +631,7 @@ mod tests {
642631
}
643632
53i32
644633
}"#,
645-
r#"fn foo() -> Result<i32, <|>> {
634+
r#"fn foo() -> Result<i32, ${0:_}> {
646635
let test = "test";
647636
if test == "test" {
648637
return Ok(24i32);
@@ -673,7 +662,7 @@ mod tests {
673662
674663
the_field
675664
}"#,
676-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
665+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
677666
let true_closure = || {
678667
return true;
679668
};
@@ -712,7 +701,7 @@ mod tests {
712701
713702
t.unwrap_or_else(|| the_field)
714703
}"#,
715-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
704+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
716705
let true_closure = || {
717706
return true;
718707
};
@@ -750,7 +739,7 @@ mod tests {
750739
i += 1;
751740
}
752741
}"#,
753-
r#"fn foo() -> Result<i32, <|>> {
742+
r#"fn foo() -> Result<i32, ${0:_}> {
754743
let test = "test";
755744
if test == "test" {
756745
return Ok(24i32);
@@ -782,7 +771,7 @@ mod tests {
782771
}
783772
}
784773
}"#,
785-
r#"fn foo() -> Result<i32, <|>> {
774+
r#"fn foo() -> Result<i32, ${0:_}> {
786775
let test = "test";
787776
if test == "test" {
788777
return Ok(24i32);
@@ -820,7 +809,7 @@ mod tests {
820809
}
821810
}
822811
}"#,
823-
r#"fn foo() -> Result<i32, <|>> {
812+
r#"fn foo() -> Result<i32, ${0:_}> {
824813
let test = "test";
825814
let other = 5;
826815
if test == "test" {
@@ -861,7 +850,7 @@ mod tests {
861850
862851
the_field
863852
}"#,
864-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
853+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
865854
if the_field < 5 {
866855
let mut i = 0;
867856
loop {
@@ -895,7 +884,7 @@ mod tests {
895884
896885
the_field
897886
}"#,
898-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
887+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
899888
if the_field < 5 {
900889
let mut i = 0;
901890
@@ -924,7 +913,7 @@ mod tests {
924913
925914
the_field
926915
}"#,
927-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
916+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
928917
if the_field < 5 {
929918
let mut i = 0;
930919
@@ -954,7 +943,7 @@ mod tests {
954943
955944
the_field
956945
}"#,
957-
r#"fn foo(the_field: u32) -> Result<u32, <|>> {
946+
r#"fn foo(the_field: u32) -> Result<u32, ${0:_}> {
958947
if the_field < 5 {
959948
let mut i = 0;
960949

crates/ra_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ fn doctest_change_return_type_to_result() {
276276
fn foo() -> i32<|> { 42i32 }
277277
"#####,
278278
r#####"
279-
fn foo() -> Result<i32, > { Ok(42i32) }
279+
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
280280
"#####,
281281
)
282282
}

docs/user/assists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Change the function's return type to Result.
268268
fn foo() -> i32┃ { 42i32 }
269269

270270
// AFTER
271-
fn foo() -> Result<i32, > { Ok(42i32) }
271+
fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
272272
```
273273

274274
## `change_visibility`

0 commit comments

Comments
 (0)