diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/assists/src/handlers/replace_impl_trait_with_generic.rs | 37 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 4 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 2 |
3 files changed, 25 insertions, 18 deletions
diff --git a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs index 612c48466..748c528d4 100644 --- a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs +++ b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs | |||
@@ -7,11 +7,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; | |||
7 | // Replaces `impl Trait` function argument with the named generic. | 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 | pub(crate) fn replace_impl_trait_with_generic( | 16 | pub(crate) fn replace_impl_trait_with_generic( |
17 | acc: &mut Assists, | 17 | acc: &mut Assists, |
@@ -21,13 +21,7 @@ pub(crate) fn replace_impl_trait_with_generic( | |||
21 | let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; | 21 | let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; |
22 | let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; | 22 | let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; |
23 | 23 | ||
24 | let impl_trait_ty = type_impl_trait | 24 | let impl_trait_ty = type_impl_trait.type_bound_list()?; |
25 | .syntax() | ||
26 | .descendants() | ||
27 | .last() | ||
28 | .and_then(ast::NameRef::cast)? | ||
29 | .text() | ||
30 | .to_string(); | ||
31 | 25 | ||
32 | let target = type_fn.syntax().text_range(); | 26 | let target = type_fn.syntax().text_range(); |
33 | acc.add( | 27 | acc.add( |
@@ -35,7 +29,7 @@ pub(crate) fn replace_impl_trait_with_generic( | |||
35 | "Replace impl trait with generic", | 29 | "Replace impl trait with generic", |
36 | target, | 30 | target, |
37 | |edit| { | 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 | let generic_param_list = type_fn | 34 | let generic_param_list = type_fn |
41 | .generic_param_list() | 35 | .generic_param_list() |
@@ -65,7 +59,7 @@ mod tests { | |||
65 | fn foo<G>(bar: <|>impl Bar) {} | 59 | fn foo<G>(bar: <|>impl Bar) {} |
66 | "#, | 60 | "#, |
67 | r#" | 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 | fn foo(bar: <|>impl Bar) {} | 72 | fn foo(bar: <|>impl Bar) {} |
79 | "#, | 73 | "#, |
80 | r#" | 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 | fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {} | 85 | fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {} |
92 | "#, | 86 | "#, |
93 | r#" | 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 | fn foo<>(bar: <|>impl Bar) {} | 98 | fn foo<>(bar: <|>impl Bar) {} |
105 | "#, | 99 | "#, |
106 | r#" | 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 | fn foo<B>(bar: <|>impl Bar) {} | 127 | fn foo<B>(bar: <|>impl Bar) {} |
134 | "#, | 128 | "#, |
135 | r#" | 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 | } |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index 3963136d8..4e5ca3825 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -819,10 +819,10 @@ fn doctest_replace_impl_trait_with_generic() { | |||
819 | check_doc_test( | 819 | check_doc_test( |
820 | "replace_impl_trait_with_generic", | 820 | "replace_impl_trait_with_generic", |
821 | r#####" | 821 | r#####" |
822 | fn foo<G>(bar: <|>impl Bar) {} | 822 | fn foo(bar: <|>impl Bar) {} |
823 | "#####, | 823 | "#####, |
824 | r#####" | 824 | r#####" |
825 | fn foo<B: Bar>(bar: B) {} | 825 | fn foo<B: Bar,>(bar: B) {} |
826 | "#####, | 826 | "#####, |
827 | ) | 827 | ) |
828 | } | 828 | } |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 7329e3039..dac4174cd 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -294,7 +294,7 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList | |||
294 | ast_from_text(&format!("fn f({}) {{ }}", args)) | 294 | ast_from_text(&format!("fn f({}) {{ }}", args)) |
295 | } | 295 | } |
296 | 296 | ||
297 | pub fn generic_param(name: String, ty: Option<String>) -> ast::GenericParam { | 297 | pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { |
298 | let bound = match ty { | 298 | let bound = match ty { |
299 | Some(it) => format!(": {}", it), | 299 | Some(it) => format!(": {}", it), |
300 | None => String::new(), | 300 | None => String::new(), |