@@ -102,9 +102,11 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
102
102
return None ;
103
103
}
104
104
105
+ let let_ = if pat_seen { " let" } else { "" } ;
106
+
105
107
acc. add (
106
108
AssistId ( "replace_if_let_with_match" , AssistKind :: RefactorRewrite ) ,
107
- "Replace if let with match" ,
109
+ format ! ( "Replace if{let_} with match" ) ,
108
110
available_range,
109
111
move |edit| {
110
112
let match_expr = {
@@ -210,8 +212,17 @@ fn make_else_arm(
210
212
// ```
211
213
pub ( crate ) fn replace_match_with_if_let ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
212
214
let match_expr: ast:: MatchExpr = ctx. find_node_at_offset ( ) ?;
215
+ let match_arm_list = match_expr. match_arm_list ( ) ?;
216
+ let available_range = TextRange :: new (
217
+ match_expr. syntax ( ) . text_range ( ) . start ( ) ,
218
+ match_arm_list. syntax ( ) . text_range ( ) . start ( ) ,
219
+ ) ;
220
+ let cursor_in_range = available_range. contains_range ( ctx. selection_trimmed ( ) ) ;
221
+ if !cursor_in_range {
222
+ return None ;
223
+ }
213
224
214
- let mut arms = match_expr . match_arm_list ( ) ? . arms ( ) ;
225
+ let mut arms = match_arm_list. arms ( ) ;
215
226
let ( first_arm, second_arm) = ( arms. next ( ) ?, arms. next ( ) ?) ;
216
227
if arms. next ( ) . is_some ( ) || first_arm. guard ( ) . is_some ( ) || second_arm. guard ( ) . is_some ( ) {
217
228
return None ;
@@ -226,10 +237,20 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
226
237
) ?;
227
238
let scrutinee = match_expr. expr ( ) ?;
228
239
240
+ let let_ = match & if_let_pat {
241
+ ast:: Pat :: LiteralPat ( p)
242
+ if p. literal ( )
243
+ . map ( |it| it. token ( ) . kind ( ) )
244
+ . map_or ( false , |it| it == T ! [ true ] || it == T ! [ false ] ) =>
245
+ {
246
+ ""
247
+ }
248
+ _ => " let" ,
249
+ } ;
229
250
let target = match_expr. syntax ( ) . text_range ( ) ;
230
251
acc. add (
231
252
AssistId ( "replace_match_with_if_let" , AssistKind :: RefactorRewrite ) ,
232
- "Replace match with if let" ,
253
+ format ! ( "Replace match with if{let_}" ) ,
233
254
target,
234
255
move |edit| {
235
256
fn make_block_expr ( expr : ast:: Expr ) -> ast:: BlockExpr {
0 commit comments