@@ -7,11 +7,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
77// Replaces `impl Trait` function argument with the named generic.
88//
99// ```
10- // fn foo<G> (bar: <|>impl Bar) {}
10+ // fn foo(bar: <|>impl Bar) {}
1111// ```
1212// ->
1313// ```
14- // fn foo<B: Bar>(bar: B) {}
14+ // fn foo<B: Bar, >(bar: B) {}
1515// ```
1616pub ( crate ) fn replace_impl_trait_with_generic (
1717 acc : & mut Assists ,
@@ -21,21 +21,15 @@ pub(crate) fn replace_impl_trait_with_generic(
2121 let type_param = type_impl_trait. syntax ( ) . parent ( ) . and_then ( ast:: Param :: cast) ?;
2222 let type_fn = type_param. syntax ( ) . ancestors ( ) . find_map ( ast:: Fn :: cast) ?;
2323
24- let impl_trait_ty = type_impl_trait
25- . syntax ( )
26- . descendants ( )
27- . last ( )
28- . and_then ( ast:: NameRef :: cast) ?
29- . text ( )
30- . to_string ( ) ;
24+ let impl_trait_ty = type_impl_trait. type_bound_list ( ) ?;
3125
3226 let target = type_fn. syntax ( ) . text_range ( ) ;
3327 acc. add (
3428 AssistId ( "replace_impl_trait_with_generic" , AssistKind :: RefactorRewrite ) ,
3529 "Replace impl trait with generic" ,
3630 target,
3731 |edit| {
38- let generic_letter = impl_trait_ty. chars ( ) . next ( ) . unwrap ( ) . to_string ( ) ;
32+ let generic_letter = impl_trait_ty. to_string ( ) . chars ( ) . next ( ) . unwrap ( ) . to_string ( ) ;
3933
4034 let generic_param_list = type_fn
4135 . generic_param_list ( )
@@ -65,7 +59,7 @@ mod tests {
6559 fn foo<G>(bar: <|>impl Bar) {}
6660 "# ,
6761 r#"
68- fn foo<G, B: Bar>(bar: B) {}
62+ fn foo<G, B: Bar, >(bar: B) {}
6963 "# ,
7064 ) ;
7165 }
@@ -78,7 +72,7 @@ mod tests {
7872 fn foo(bar: <|>impl Bar) {}
7973 "# ,
8074 r#"
81- fn foo<B: Bar>(bar: B) {}
75+ fn foo<B: Bar, >(bar: B) {}
8276 "# ,
8377 ) ;
8478 }
@@ -91,7 +85,7 @@ mod tests {
9185 fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {}
9286 "# ,
9387 r#"
94- fn foo<G, B: Bar>(foo: impl Foo, bar: B) {}
88+ fn foo<G, B: Bar, >(foo: impl Foo, bar: B) {}
9589 "# ,
9690 ) ;
9791 }
@@ -104,7 +98,7 @@ mod tests {
10498 fn foo<>(bar: <|>impl Bar) {}
10599 "# ,
106100 r#"
107- fn foo<B: Bar>(bar: B) {}
101+ fn foo<B: Bar, >(bar: B) {}
108102 "# ,
109103 ) ;
110104 }
@@ -133,7 +127,7 @@ mod tests {
133127 fn foo<B>(bar: <|>impl Bar) {}
134128 "# ,
135129 r#"
136- fn foo<B, C: Bar>(bar: C) {}
130+ fn foo<B, C: Bar, >(bar: C) {}
137131 "# ,
138132 ) ;
139133 }
@@ -158,4 +152,17 @@ mod tests {
158152 "# ,
159153 ) ;
160154 }
155+
156+ #[ test]
157+ fn replace_impl_trait_multiple ( ) {
158+ check_assist (
159+ replace_impl_trait_with_generic,
160+ r#"
161+ fn foo(bar: <|>impl Foo + Bar) {}
162+ "# ,
163+ r#"
164+ fn foo<F: Foo + Bar,>(bar: F) {}
165+ "# ,
166+ ) ;
167+ }
161168}
0 commit comments