aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/replace_impl_trait_with_generic.rs19
1 files changed, 12 insertions, 7 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 5b0d5d971..612c48466 100644
--- a/crates/assists/src/handlers/replace_impl_trait_with_generic.rs
+++ b/crates/assists/src/handlers/replace_impl_trait_with_generic.rs
@@ -5,13 +5,21 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
5// Assist: replace_impl_trait_with_generic 5// Assist: replace_impl_trait_with_generic
6// 6//
7// Replaces `impl Trait` function argument with the named generic. 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// ```
8pub(crate) fn replace_impl_trait_with_generic( 16pub(crate) fn replace_impl_trait_with_generic(
9 acc: &mut Assists, 17 acc: &mut Assists,
10 ctx: &AssistContext, 18 ctx: &AssistContext,
11) -> Option<()> { 19) -> Option<()> {
12 let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?; 20 let type_impl_trait = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
13 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)?;
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 let impl_trait_ty = type_impl_trait 24 let impl_trait_ty = type_impl_trait
17 .syntax() 25 .syntax()
@@ -27,7 +35,7 @@ pub(crate) fn replace_impl_trait_with_generic(
27 "Replace impl trait with generic", 35 "Replace impl trait with generic",
28 target, 36 target,
29 |edit| { 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 let generic_param_list = type_fn 40 let generic_param_list = type_fn
33 .generic_param_list() 41 .generic_param_list()
@@ -36,7 +44,7 @@ pub(crate) fn replace_impl_trait_with_generic(
36 44
37 let new_type_fn = type_fn 45 let new_type_fn = type_fn
38 .replace_descendant::<ast::Type>(type_impl_trait.into(), make::ty(&generic_letter)) 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 edit.replace_ast(type_fn.clone(), new_type_fn); 49 edit.replace_ast(type_fn.clone(), new_type_fn);
42 }, 50 },
@@ -103,8 +111,6 @@ mod tests {
103 111
104 #[test] 112 #[test]
105 fn replace_impl_trait_with_empty_multiline_generic_params() { 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 check_assist( 114 check_assist(
109 replace_impl_trait_with_generic, 115 replace_impl_trait_with_generic,
110 r#" 116 r#"
@@ -147,8 +153,7 @@ mod tests {
147 fn foo< 153 fn foo<
148 G: Foo, 154 G: Foo,
149 F, 155 F,
150 H, 156 H, B: Bar,
151 B: Bar,
152 >(bar: B) {} 157 >(bar: B) {}
153 "#, 158 "#,
154 ); 159 );