@@ -9,7 +9,10 @@ use ra_syntax::{
99 AstNode ,
1010} ;
1111
12- use crate :: { utils:: TryEnum , AssistContext , AssistId , Assists } ;
12+ use crate :: {
13+ utils:: { render_snippet, Cursor , TryEnum } ,
14+ AssistContext , AssistId , Assists ,
15+ } ;
1316
1417// Assist: replace_unwrap_with_match
1518//
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
2932// let x: Result<i32, i32> = Result::Ok(92);
3033// let y = match x {
3134// Ok(a) => a,
32- // _ => unreachable!(),
35+ // $0_ => unreachable!(),
3336// };
3437// }
3538// ```
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext)
4346 let ty = ctx. sema . type_of_expr ( & caller) ?;
4447 let happy_variant = TryEnum :: from_ty ( & ctx. sema , & ty) ?. happy_case ( ) ;
4548 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 | {
4750 let ok_path = make:: path_unqualified ( make:: path_segment ( make:: name_ref ( happy_variant) ) ) ;
4851 let it = make:: bind_pat ( make:: name ( "a" ) ) . into ( ) ;
4952 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)
5861 let match_expr = make:: expr_match ( caller. clone ( ) , match_arm_list)
5962 . indent ( IndentLevel :: from_node ( method_call. syntax ( ) ) ) ;
6063
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+ }
6379 } )
6480}
6581
6682#[ cfg( test) ]
6783mod tests {
68- use super :: * ;
6984 use crate :: tests:: { check_assist, check_assist_target} ;
7085
86+ use super :: * ;
87+
7188 #[ test]
7289 fn test_replace_result_unwrap_with_match ( ) {
7390 check_assist (
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) }
85102fn i<T>(a: T) -> T { a }
86103fn main() {
87104 let x: Result<i32, i32> = Result::Ok(92);
88- let y = <|> match i(x) {
105+ let y = match i(x) {
89106 Ok(a) => a,
90- _ => unreachable!(),
107+ $0_ => unreachable!(),
91108 };
92109}
93110 " ,
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None }
111128fn i<T>(a: T) -> T { a }
112129fn main() {
113130 let x = Option::Some(92);
114- let y = <|> match i(x) {
131+ let y = match i(x) {
115132 Some(a) => a,
116- _ => unreachable!(),
133+ $0_ => unreachable!(),
117134 };
118135}
119136 " ,
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) }
137154fn i<T>(a: T) -> T { a }
138155fn main() {
139156 let x: Result<i32, i32> = Result::Ok(92);
140- let y = <|> match i(x) {
157+ let y = match i(x) {
141158 Ok(a) => a,
142- _ => unreachable!(),
159+ $0_ => unreachable!(),
143160 }.count_zeroes();
144161}
145162 " ,
0 commit comments