// Replaces `impl Trait` function argument with the named generic.
//
// ```
-// fn foo<G>(bar: <|>impl Bar) {}
+// fn foo(bar: <|>impl Bar) {}
// ```
// ->
// ```
-// fn foo<B: Bar>(bar: B) {}
+// fn foo<B: Bar,>(bar: B) {}
// ```
pub(crate) fn replace_impl_trait_with_generic(
acc: &mut Assists,
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
- let impl_trait_ty = type_impl_trait
- .syntax()
- .descendants()
- .last()
- .and_then(ast::NameRef::cast)?
- .text()
- .to_string();
+ let impl_trait_ty = type_impl_trait.type_bound_list()?;
let target = type_fn.syntax().text_range();
acc.add(
"Replace impl trait with generic",
target,
|edit| {
- let generic_letter = impl_trait_ty.chars().next().unwrap().to_string();
+ let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string();
let generic_param_list = type_fn
.generic_param_list()
fn foo<G>(bar: <|>impl Bar) {}
"#,
r#"
- fn foo<G, B: Bar>(bar: B) {}
+ fn foo<G, B: Bar,>(bar: B) {}
"#,
);
}
fn foo(bar: <|>impl Bar) {}
"#,
r#"
- fn foo<B: Bar>(bar: B) {}
+ fn foo<B: Bar,>(bar: B) {}
"#,
);
}
fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {}
"#,
r#"
- fn foo<G, B: Bar>(foo: impl Foo, bar: B) {}
+ fn foo<G, B: Bar,>(foo: impl Foo, bar: B) {}
"#,
);
}
fn foo<>(bar: <|>impl Bar) {}
"#,
r#"
- fn foo<B: Bar>(bar: B) {}
+ fn foo<B: Bar,>(bar: B) {}
"#,
);
}
fn foo<B>(bar: <|>impl Bar) {}
"#,
r#"
- fn foo<B, C: Bar>(bar: C) {}
+ fn foo<B, C: Bar,>(bar: C) {}
"#,
);
}
"#,
);
}
+
+ #[test]
+ fn replace_impl_trait_multiple() {
+ check_assist(
+ replace_impl_trait_with_generic,
+ r#"
+ fn foo(bar: <|>impl Foo + Bar) {}
+ "#,
+ r#"
+ fn foo<F: Foo + Bar,>(bar: F) {}
+ "#,
+ );
+ }
}