@@ -7,11 +7,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
7
7
// Replaces `impl Trait` function argument with the named generic.
8
8
//
9
9
// ```
10
- // fn foo<G> (bar: <|>impl Bar) {}
10
+ // fn foo(bar: <|>impl Bar) {}
11
11
// ```
12
12
// ->
13
13
// ```
14
- // fn foo<B: Bar>(bar: B) {}
14
+ // fn foo<B: Bar, >(bar: B) {}
15
15
// ```
16
16
pub ( crate ) fn replace_impl_trait_with_generic (
17
17
acc : & mut Assists ,
@@ -21,21 +21,15 @@ pub(crate) fn replace_impl_trait_with_generic(
21
21
let type_param = type_impl_trait. syntax ( ) . parent ( ) . and_then ( ast:: Param :: cast) ?;
22
22
let type_fn = type_param. syntax ( ) . ancestors ( ) . find_map ( ast:: Fn :: cast) ?;
23
23
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 ( ) ?;
31
25
32
26
let target = type_fn. syntax ( ) . text_range ( ) ;
33
27
acc. add (
34
28
AssistId ( "replace_impl_trait_with_generic" , AssistKind :: RefactorRewrite ) ,
35
29
"Replace impl trait with generic" ,
36
30
target,
37
31
|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 ( ) ;
39
33
40
34
let generic_param_list = type_fn
41
35
. generic_param_list ( )
@@ -65,7 +59,7 @@ mod tests {
65
59
fn foo<G>(bar: <|>impl Bar) {}
66
60
"# ,
67
61
r#"
68
- fn foo<G, B: Bar>(bar: B) {}
62
+ fn foo<G, B: Bar, >(bar: B) {}
69
63
"# ,
70
64
) ;
71
65
}
@@ -78,7 +72,7 @@ mod tests {
78
72
fn foo(bar: <|>impl Bar) {}
79
73
"# ,
80
74
r#"
81
- fn foo<B: Bar>(bar: B) {}
75
+ fn foo<B: Bar, >(bar: B) {}
82
76
"# ,
83
77
) ;
84
78
}
@@ -91,7 +85,7 @@ mod tests {
91
85
fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {}
92
86
"# ,
93
87
r#"
94
- fn foo<G, B: Bar>(foo: impl Foo, bar: B) {}
88
+ fn foo<G, B: Bar, >(foo: impl Foo, bar: B) {}
95
89
"# ,
96
90
) ;
97
91
}
@@ -104,7 +98,7 @@ mod tests {
104
98
fn foo<>(bar: <|>impl Bar) {}
105
99
"# ,
106
100
r#"
107
- fn foo<B: Bar>(bar: B) {}
101
+ fn foo<B: Bar, >(bar: B) {}
108
102
"# ,
109
103
) ;
110
104
}
@@ -133,7 +127,7 @@ mod tests {
133
127
fn foo<B>(bar: <|>impl Bar) {}
134
128
"# ,
135
129
r#"
136
- fn foo<B, C: Bar>(bar: C) {}
130
+ fn foo<B, C: Bar, >(bar: C) {}
137
131
"# ,
138
132
) ;
139
133
}
@@ -158,4 +152,17 @@ mod tests {
158
152
"# ,
159
153
) ;
160
154
}
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
+ }
161
168
}
0 commit comments