1
1
use ra_syntax:: {
2
2
ast:: { self , BlockExpr , Expr , LoopBodyOwner } ,
3
- AstNode ,
4
- SyntaxKind :: { COMMENT , WHITESPACE } ,
5
- SyntaxNode , TextSize ,
3
+ AstNode , SyntaxNode ,
6
4
} ;
7
5
8
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
19
pub ( crate ) fn change_return_type_to_result ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
22
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
40
for ret_expr_arg in tail_return_expr_collector. exprs_to_wrap {
43
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) {
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) ) ,
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
252
#[ cfg( test) ]
263
253
mod tests {
264
-
265
254
use crate :: tests:: { check_assist, check_assist_not_applicable} ;
266
255
267
256
use super :: * ;
@@ -274,7 +263,7 @@ mod tests {
274
263
let test = "test";
275
264
return 42i32;
276
265
}"# ,
277
- r#"fn foo() -> Result<i32, <|> > {
266
+ r#"fn foo() -> Result<i32, ${0:_} > {
278
267
let test = "test";
279
268
return Ok(42i32);
280
269
}"# ,
@@ -289,7 +278,7 @@ mod tests {
289
278
let test = "test";
290
279
return 42i32;
291
280
}"# ,
292
- r#"fn foo() -> Result<i32, <|> > {
281
+ r#"fn foo() -> Result<i32, ${0:_} > {
293
282
let test = "test";
294
283
return Ok(42i32);
295
284
}"# ,
@@ -315,7 +304,7 @@ mod tests {
315
304
let test = "test";
316
305
return 42i32;
317
306
}"# ,
318
- r#"fn foo() -> Result<i32, <|> > {
307
+ r#"fn foo() -> Result<i32, ${0:_} > {
319
308
let test = "test";
320
309
return Ok(42i32);
321
310
}"# ,
@@ -330,7 +319,7 @@ mod tests {
330
319
let test = "test";
331
320
42i32
332
321
}"# ,
333
- r#"fn foo() -> Result<i32, <|> > {
322
+ r#"fn foo() -> Result<i32, ${0:_} > {
334
323
let test = "test";
335
324
Ok(42i32)
336
325
}"# ,
@@ -344,7 +333,7 @@ mod tests {
344
333
r#"fn foo() -> i32<|> {
345
334
42i32
346
335
}"# ,
347
- r#"fn foo() -> Result<i32, <|> > {
336
+ r#"fn foo() -> Result<i32, ${0:_} > {
348
337
Ok(42i32)
349
338
}"# ,
350
339
) ;
@@ -360,7 +349,7 @@ mod tests {
360
349
24i32
361
350
}
362
351
}"# ,
363
- r#"fn foo() -> Result<i32, <|> > {
352
+ r#"fn foo() -> Result<i32, ${0:_} > {
364
353
if true {
365
354
Ok(42i32)
366
355
} else {
@@ -385,7 +374,7 @@ mod tests {
385
374
24i32
386
375
}
387
376
}"# ,
388
- r#"fn foo() -> Result<i32, <|> > {
377
+ r#"fn foo() -> Result<i32, ${0:_} > {
389
378
if true {
390
379
if false {
391
380
Ok(1)
@@ -414,7 +403,7 @@ mod tests {
414
403
24i32.await
415
404
}
416
405
}"# ,
417
- r#"async fn foo() -> Result<i32, <|> > {
406
+ r#"async fn foo() -> Result<i32, ${0:_} > {
418
407
if true {
419
408
if false {
420
409
Ok(1.await)
@@ -435,7 +424,7 @@ mod tests {
435
424
r#"fn foo() -> [i32;<|> 3] {
436
425
[1, 2, 3]
437
426
}"# ,
438
- r#"fn foo() -> Result<[i32; 3], <|> > {
427
+ r#"fn foo() -> Result<[i32; 3], ${0:_} > {
439
428
Ok([1, 2, 3])
440
429
}"# ,
441
430
) ;
@@ -456,7 +445,7 @@ mod tests {
456
445
24 as i32
457
446
}
458
447
}"# ,
459
- r#"fn foo() -> Result<i32, <|> > {
448
+ r#"fn foo() -> Result<i32, ${0:_} > {
460
449
if true {
461
450
if false {
462
451
Ok(1 as i32)
@@ -481,7 +470,7 @@ mod tests {
481
470
_ => 24i32,
482
471
}
483
472
}"# ,
484
- r#"fn foo() -> Result<i32, <|> > {
473
+ r#"fn foo() -> Result<i32, ${0:_} > {
485
474
let my_var = 5;
486
475
match my_var {
487
476
5 => Ok(42i32),
@@ -504,7 +493,7 @@ mod tests {
504
493
505
494
my_var
506
495
}"# ,
507
- r#"fn foo() -> Result<i32, <|> > {
496
+ r#"fn foo() -> Result<i32, ${0:_} > {
508
497
let my_var = 5;
509
498
loop {
510
499
println!("test");
@@ -527,7 +516,7 @@ mod tests {
527
516
528
517
my_var
529
518
}"# ,
530
- r#"fn foo() -> Result<i32, <|> > {
519
+ r#"fn foo() -> Result<i32, ${0:_} > {
531
520
let my_var = let x = loop {
532
521
break 1;
533
522
};
@@ -550,7 +539,7 @@ mod tests {
550
539
551
540
res
552
541
}"# ,
553
- r#"fn foo() -> Result<i32, <|> > {
542
+ r#"fn foo() -> Result<i32, ${0:_} > {
554
543
let my_var = 5;
555
544
let res = match my_var {
556
545
5 => 42i32,
@@ -573,7 +562,7 @@ mod tests {
573
562
574
563
res
575
564
}"# ,
576
- r#"fn foo() -> Result<i32, <|> > {
565
+ r#"fn foo() -> Result<i32, ${0:_} > {
577
566
let my_var = 5;
578
567
let res = if my_var == 5 {
579
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
602
let my_var = 5;
614
603
match my_var {
615
604
5 => {
@@ -642,7 +631,7 @@ mod tests {
642
631
}
643
632
53i32
644
633
}"# ,
645
- r#"fn foo() -> Result<i32, <|> > {
634
+ r#"fn foo() -> Result<i32, ${0:_} > {
646
635
let test = "test";
647
636
if test == "test" {
648
637
return Ok(24i32);
@@ -673,7 +662,7 @@ mod tests {
673
662
674
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
666
let true_closure = || {
678
667
return true;
679
668
};
@@ -712,7 +701,7 @@ mod tests {
712
701
713
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
705
let true_closure = || {
717
706
return true;
718
707
};
@@ -750,7 +739,7 @@ mod tests {
750
739
i += 1;
751
740
}
752
741
}"# ,
753
- r#"fn foo() -> Result<i32, <|> > {
742
+ r#"fn foo() -> Result<i32, ${0:_} > {
754
743
let test = "test";
755
744
if test == "test" {
756
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
775
let test = "test";
787
776
if test == "test" {
788
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
813
let test = "test";
825
814
let other = 5;
826
815
if test == "test" {
@@ -861,7 +850,7 @@ mod tests {
861
850
862
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
854
if the_field < 5 {
866
855
let mut i = 0;
867
856
loop {
@@ -895,7 +884,7 @@ mod tests {
895
884
896
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
888
if the_field < 5 {
900
889
let mut i = 0;
901
890
@@ -924,7 +913,7 @@ mod tests {
924
913
925
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
917
if the_field < 5 {
929
918
let mut i = 0;
930
919
@@ -954,7 +943,7 @@ mod tests {
954
943
955
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
947
if the_field < 5 {
959
948
let mut i = 0;
960
949
0 commit comments