@@ -9,7 +9,10 @@ use ra_syntax::{
9
9
AstNode ,
10
10
} ;
11
11
12
- use crate :: { utils:: TryEnum , AssistContext , AssistId , Assists } ;
12
+ use crate :: {
13
+ utils:: { render_snippet, Cursor , TryEnum } ,
14
+ AssistContext , AssistId , Assists ,
15
+ } ;
13
16
14
17
// Assist: replace_unwrap_with_match
15
18
//
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
29
32
// let x: Result<i32, i32> = Result::Ok(92);
30
33
// let y = match x {
31
34
// Ok(a) => a,
32
- // _ => unreachable!(),
35
+ // $0_ => unreachable!(),
33
36
// };
34
37
// }
35
38
// ```
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
43
46
let ty = ctx. sema . type_of_expr ( & caller) ?;
44
47
let happy_variant = TryEnum :: from_ty ( & ctx. sema , & ty) ?. happy_case ( ) ;
45
48
let target = method_call. syntax ( ) . text_range ( ) ;
46
- acc. add ( AssistId ( "replace_unwrap_with_match" ) , "Replace unwrap with match" , target, |edit | {
49
+ acc. add ( AssistId ( "replace_unwrap_with_match" ) , "Replace unwrap with match" , target, |builder | {
47
50
let ok_path = make:: path_unqualified ( make:: path_segment ( make:: name_ref ( happy_variant) ) ) ;
48
51
let it = make:: bind_pat ( make:: name ( "a" ) ) . into ( ) ;
49
52
let ok_tuple = make:: tuple_struct_pat ( ok_path, iter:: once ( it) ) . into ( ) ;
@@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
58
61
let match_expr = make:: expr_match ( caller. clone ( ) , match_arm_list)
59
62
. indent ( IndentLevel :: from_node ( method_call. syntax ( ) ) ) ;
60
63
61
- edit. set_cursor ( caller. syntax ( ) . text_range ( ) . start ( ) ) ;
62
- edit. replace_ast :: < ast:: Expr > ( method_call. into ( ) , match_expr) ;
64
+ let range = method_call. syntax ( ) . text_range ( ) ;
65
+ match ctx. config . snippet_cap {
66
+ Some ( cap) => {
67
+ let err_arm = match_expr
68
+ . syntax ( )
69
+ . descendants ( )
70
+ . filter_map ( ast:: MatchArm :: cast)
71
+ . last ( )
72
+ . unwrap ( ) ;
73
+ let snippet =
74
+ render_snippet ( cap, match_expr. syntax ( ) , Cursor :: Before ( err_arm. syntax ( ) ) ) ;
75
+ builder. replace_snippet ( cap, range, snippet)
76
+ }
77
+ None => builder. replace ( range, match_expr. to_string ( ) ) ,
78
+ }
63
79
} )
64
80
}
65
81
66
82
#[ cfg( test) ]
67
83
mod tests {
68
- use super :: * ;
69
84
use crate :: tests:: { check_assist, check_assist_target} ;
70
85
86
+ use super :: * ;
87
+
71
88
#[ test]
72
89
fn test_replace_result_unwrap_with_match ( ) {
73
90
check_assist (
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
85
102
fn i<T>(a: T) -> T { a }
86
103
fn main() {
87
104
let x: Result<i32, i32> = Result::Ok(92);
88
- let y = <|> match i(x) {
105
+ let y = match i(x) {
89
106
Ok(a) => a,
90
- _ => unreachable!(),
107
+ $0_ => unreachable!(),
91
108
};
92
109
}
93
110
" ,
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
111
128
fn i<T>(a: T) -> T { a }
112
129
fn main() {
113
130
let x = Option::Some(92);
114
- let y = <|> match i(x) {
131
+ let y = match i(x) {
115
132
Some(a) => a,
116
- _ => unreachable!(),
133
+ $0_ => unreachable!(),
117
134
};
118
135
}
119
136
" ,
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) }
137
154
fn i<T>(a: T) -> T { a }
138
155
fn main() {
139
156
let x: Result<i32, i32> = Result::Ok(92);
140
- let y = <|> match i(x) {
157
+ let y = match i(x) {
141
158
Ok(a) => a,
142
- _ => unreachable!(),
159
+ $0_ => unreachable!(),
143
160
}.count_zeroes();
144
161
}
145
162
" ,
0 commit comments