@@ -5,13 +5,21 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
55// Assist: replace_impl_trait_with_generic
66//
77// Replaces `impl Trait` function argument with the named generic.
8+ //
9+ // ```
10+ // fn foo<G>(bar: <|>impl Bar) {}
11+ // ```
12+ // ->
13+ // ```
14+ // fn foo<B: Bar>(bar: B) {}
15+ // ```
816pub ( crate ) fn replace_impl_trait_with_generic (
917 acc : & mut Assists ,
1018 ctx : & AssistContext ,
1119) -> Option < ( ) > {
1220 let type_impl_trait = ctx. find_node_at_offset :: < ast:: ImplTraitType > ( ) ?;
1321 let type_param = type_impl_trait. syntax ( ) . parent ( ) . and_then ( ast:: Param :: cast) ?;
14- let type_fn = type_param. syntax ( ) . ancestors ( ) . nth ( 2 ) . and_then ( ast:: Fn :: cast) ?;
22+ let type_fn = type_param. syntax ( ) . ancestors ( ) . find_map ( ast:: Fn :: cast) ?;
1523
1624 let impl_trait_ty = type_impl_trait
1725 . syntax ( )
@@ -27,7 +35,7 @@ pub(crate) fn replace_impl_trait_with_generic(
2735 "Replace impl trait with generic" ,
2836 target,
2937 |edit| {
30- let generic_letter = impl_trait_ty[ .. 1 ] . to_string ( ) ;
38+ let generic_letter = impl_trait_ty. chars ( ) . next ( ) . unwrap ( ) . to_string ( ) ;
3139
3240 let generic_param_list = type_fn
3341 . generic_param_list ( )
@@ -36,7 +44,7 @@ pub(crate) fn replace_impl_trait_with_generic(
3644
3745 let new_type_fn = type_fn
3846 . replace_descendant :: < ast:: Type > ( type_impl_trait. into ( ) , make:: ty ( & generic_letter) )
39- . with_generic_params ( generic_param_list) ;
47+ . with_generic_param_list ( generic_param_list) ;
4048
4149 edit. replace_ast ( type_fn. clone ( ) , new_type_fn) ;
4250 } ,
@@ -103,8 +111,6 @@ mod tests {
103111
104112 #[ test]
105113 fn replace_impl_trait_with_empty_multiline_generic_params ( ) {
106- // FIXME: It would be more correct to place the generic parameter
107- // on the next line after the left angle.
108114 check_assist (
109115 replace_impl_trait_with_generic,
110116 r#"
@@ -147,8 +153,7 @@ mod tests {
147153 fn foo<
148154 G: Foo,
149155 F,
150- H,
151- B: Bar,
156+ H, B: Bar,
152157 >(bar: B) {}
153158 "# ,
154159 ) ;
0 commit comments