@@ -65,22 +65,24 @@ pub(crate) fn replace_with_lazy_method(
6565 call. syntax ( ) . text_range ( ) ,
6666 |builder| {
6767 let editor = builder. make_editor ( call. syntax ( ) ) ;
68- let param_name = match & * method_name_lazy {
69- "and_then" => "it" ,
68+ let add_param = match & * method_name_lazy {
69+ "and_then" => true ,
7070 "or_else" | "unwrap_or_else" => {
71- if let Some ( result) = FamousDefs ( & ctx. sema , scope. krate ( ) ) . core_result_Result ( )
72- && result. ty ( ctx. db ( ) ) . could_unify_with ( ctx. db ( ) , & receiver_ty)
73- {
74- "e"
75- } else {
76- ""
77- }
71+ FamousDefs ( & ctx. sema , scope. krate ( ) ) . core_result_Result ( ) . is_some_and (
72+ |result| result. ty ( ctx. db ( ) ) . could_unify_with ( ctx. db ( ) , & receiver_ty) ,
73+ )
7874 }
79- _ => "" ,
75+ _ => false ,
8076 } ;
81- let closured = into_closure ( & last_arg, param_name , editor. make ( ) ) ;
77+ let closured = into_closure ( & last_arg, add_param , editor. make ( ) ) ;
8278 editor. replace ( method_name. syntax ( ) , editor. make ( ) . name ( & method_name_lazy) . syntax ( ) ) ;
8379 editor. replace ( last_arg. syntax ( ) , closured. syntax ( ) ) ;
80+ if let Some ( cap) = ctx. config . snippet_cap
81+ && let ast:: Expr :: ClosureExpr ( closured) = closured
82+ && let Some ( param) = closured. param_list ( ) . and_then ( |it| it. params ( ) . next ( ) )
83+ {
84+ editor. add_annotation ( param. syntax ( ) , builder. make_placeholder_snippet ( cap) ) ;
85+ }
8486 builder. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
8587 } ,
8688 )
@@ -98,7 +100,7 @@ fn lazy_method_name(name: &str) -> String {
98100 }
99101}
100102
101- fn into_closure ( param : & Expr , param_name : & str , make : & SyntaxFactory ) -> Expr {
103+ fn into_closure ( param : & Expr , add_param : bool , make : & SyntaxFactory ) -> Expr {
102104 ( || {
103105 if let ast:: Expr :: CallExpr ( call) = param {
104106 if call. arg_list ( ) ?. args ( ) . count ( ) == 0 { Some ( call. expr ( ) ?) } else { None }
@@ -107,8 +109,7 @@ fn into_closure(param: &Expr, param_name: &str, make: &SyntaxFactory) -> Expr {
107109 }
108110 } ) ( )
109111 . unwrap_or_else ( || {
110- let pats = ( !param_name. is_empty ( ) )
111- . then ( || make. untyped_param ( make. simple_ident_pat ( make. name ( param_name) ) . into ( ) ) ) ;
112+ let pats = add_param. then ( || make. untyped_param ( make. wildcard_pat ( ) . into ( ) ) ) ;
112113 make. expr_closure ( pats, param. clone ( ) ) . into ( )
113114 } )
114115}
@@ -259,7 +260,7 @@ fn foo() {
259260 r#"
260261fn foo() {
261262 let foo = Ok(1);
262- return foo.unwrap_or_else(|e | 2);
263+ return foo.unwrap_or_else(|${0:_} | 2);
263264}
264265"# ,
265266 )
@@ -395,7 +396,7 @@ fn foo() {
395396 r#"
396397fn foo() {
397398 let foo = Some("foo");
398- return foo.and_then(|it | Some("bar"));
399+ return foo.and_then(|${0:_} | Some("bar"));
399400}
400401"# ,
401402 )
0 commit comments