@@ -5,13 +5,21 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
5
5
// Assist: replace_impl_trait_with_generic
6
6
//
7
7
// 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
+ // ```
8
16
pub ( crate ) fn replace_impl_trait_with_generic (
9
17
acc : & mut Assists ,
10
18
ctx : & AssistContext ,
11
19
) -> Option < ( ) > {
12
20
let type_impl_trait = ctx. find_node_at_offset :: < ast:: ImplTraitType > ( ) ?;
13
21
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) ?;
15
23
16
24
let impl_trait_ty = type_impl_trait
17
25
. syntax ( )
@@ -27,7 +35,7 @@ pub(crate) fn replace_impl_trait_with_generic(
27
35
"Replace impl trait with generic" ,
28
36
target,
29
37
|edit| {
30
- let generic_letter = impl_trait_ty[ .. 1 ] . to_string ( ) ;
38
+ let generic_letter = impl_trait_ty. chars ( ) . next ( ) . unwrap ( ) . to_string ( ) ;
31
39
32
40
let generic_param_list = type_fn
33
41
. generic_param_list ( )
@@ -36,7 +44,7 @@ pub(crate) fn replace_impl_trait_with_generic(
36
44
37
45
let new_type_fn = type_fn
38
46
. 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) ;
40
48
41
49
edit. replace_ast ( type_fn. clone ( ) , new_type_fn) ;
42
50
} ,
@@ -103,8 +111,6 @@ mod tests {
103
111
104
112
#[ test]
105
113
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.
108
114
check_assist (
109
115
replace_impl_trait_with_generic,
110
116
r#"
@@ -147,8 +153,7 @@ mod tests {
147
153
fn foo<
148
154
G: Foo,
149
155
F,
150
- H,
151
- B: Bar,
156
+ H, B: Bar,
152
157
>(bar: B) {}
153
158
"# ,
154
159
) ;
0 commit comments