2021-05-09 10:20:37 -05:00
|
|
|
use syntax::{
|
2023-03-26 17:09:36 -05:00
|
|
|
ast::{self, edit_in_place::GenericParamsOwnerEdit, make, AstNode, HasGenericParams},
|
2021-05-09 10:20:37 -05:00
|
|
|
ted,
|
|
|
|
};
|
2020-09-02 17:32:18 -05:00
|
|
|
|
2021-05-09 09:58:03 -05:00
|
|
|
use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
|
2020-09-02 17:32:18 -05:00
|
|
|
|
2021-09-20 16:53:05 -05:00
|
|
|
// Assist: introduce_named_generic
|
2020-09-02 17:32:18 -05:00
|
|
|
//
|
|
|
|
// Replaces `impl Trait` function argument with the named generic.
|
2020-09-04 07:24:36 -05:00
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// fn foo(bar: $0impl Bar) {}
|
2020-09-04 07:24:36 -05:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2023-03-26 17:09:36 -05:00
|
|
|
// fn foo<$0B: Bar>(bar: B) {}
|
2020-09-04 07:24:36 -05:00
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn introduce_named_generic(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-05-09 09:58:03 -05:00
|
|
|
let impl_trait_type = ctx.find_node_at_offset::<ast::ImplTraitType>()?;
|
|
|
|
let param = impl_trait_type.syntax().parent().and_then(ast::Param::cast)?;
|
|
|
|
let fn_ = param.syntax().ancestors().find_map(ast::Fn::cast)?;
|
2020-09-02 17:32:18 -05:00
|
|
|
|
2021-05-09 09:58:03 -05:00
|
|
|
let type_bound_list = impl_trait_type.type_bound_list()?;
|
2020-09-02 17:32:18 -05:00
|
|
|
|
2021-05-09 09:58:03 -05:00
|
|
|
let target = fn_.syntax().text_range();
|
2020-09-02 17:32:18 -05:00
|
|
|
acc.add(
|
2021-09-20 16:53:05 -05:00
|
|
|
AssistId("introduce_named_generic", AssistKind::RefactorRewrite),
|
2020-09-02 17:32:18 -05:00
|
|
|
"Replace impl trait with generic",
|
|
|
|
target,
|
|
|
|
|edit| {
|
2021-05-16 06:18:49 -05:00
|
|
|
let impl_trait_type = edit.make_mut(impl_trait_type);
|
|
|
|
let fn_ = edit.make_mut(fn_);
|
2020-09-02 17:32:18 -05:00
|
|
|
|
2021-05-09 10:20:37 -05:00
|
|
|
let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type);
|
2020-09-03 06:46:28 -05:00
|
|
|
|
2021-05-09 11:11:42 -05:00
|
|
|
let type_param = make::type_param(make::name(&type_param_name), Some(type_bound_list))
|
|
|
|
.clone_for_update();
|
2021-05-09 10:20:37 -05:00
|
|
|
let new_ty = make::ty(&type_param_name).clone_for_update();
|
2020-09-03 06:46:28 -05:00
|
|
|
|
2021-05-09 10:20:37 -05:00
|
|
|
ted::replace(impl_trait_type.syntax(), new_ty.syntax());
|
2023-03-26 17:09:36 -05:00
|
|
|
fn_.get_or_create_generic_param_list().add_generic_param(type_param.into());
|
|
|
|
|
|
|
|
if let Some(cap) = ctx.config.snippet_cap {
|
|
|
|
if let Some(generic_param) =
|
|
|
|
fn_.generic_param_list().and_then(|it| it.generic_params().last())
|
|
|
|
{
|
|
|
|
edit.add_tabstop_before(cap, generic_param);
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 17:32:18 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::tests::check_assist;
|
|
|
|
|
|
|
|
#[test]
|
2021-09-20 16:53:05 -05:00
|
|
|
fn introduce_named_generic_params() {
|
2020-09-02 17:32:18 -05:00
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo<G>(bar: $0impl Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<G, $0B: Bar>(bar: B) {}"#,
|
2020-09-02 17:32:18 -05:00
|
|
|
);
|
|
|
|
}
|
2020-09-03 06:46:28 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_without_generic_params() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo(bar: $0impl Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<$0B: Bar>(bar: B) {}"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_two_impl_trait_with_generic_params() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo<G>(foo: impl Foo, bar: $0impl Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<G, $0B: Bar>(foo: impl Foo, bar: B) {}"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_with_empty_generic_params() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo<>(bar: $0impl Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<$0B: Bar>(bar: B) {}"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_with_empty_multiline_generic_params() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2020-09-03 06:46:28 -05:00
|
|
|
r#"
|
2021-05-09 09:41:25 -05:00
|
|
|
fn foo<
|
|
|
|
>(bar: $0impl Bar) {}
|
|
|
|
"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
r#"
|
2023-03-26 17:09:36 -05:00
|
|
|
fn foo<$0B: Bar
|
2021-05-09 09:41:25 -05:00
|
|
|
>(bar: B) {}
|
|
|
|
"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_with_exist_generic_letter() {
|
2021-06-15 04:48:05 -05:00
|
|
|
// FIXME: This is wrong, we should pick a different name if the one we
|
|
|
|
// want is already bound.
|
2020-09-03 06:46:28 -05:00
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo<B>(bar: $0impl Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<B, $0B: Bar>(bar: B) {}"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_with_multiline_generic_params() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2020-09-03 06:46:28 -05:00
|
|
|
r#"
|
2021-05-09 09:41:25 -05:00
|
|
|
fn foo<
|
|
|
|
G: Foo,
|
|
|
|
F,
|
|
|
|
H,
|
|
|
|
>(bar: $0impl Bar) {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo<
|
|
|
|
G: Foo,
|
|
|
|
F,
|
2023-03-26 17:09:36 -05:00
|
|
|
H, $0B: Bar,
|
2021-05-09 09:41:25 -05:00
|
|
|
>(bar: B) {}
|
|
|
|
"#,
|
2020-09-03 06:46:28 -05:00
|
|
|
);
|
|
|
|
}
|
2020-09-04 09:55:27 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn replace_impl_trait_multiple() {
|
|
|
|
check_assist(
|
2021-09-20 16:53:05 -05:00
|
|
|
introduce_named_generic,
|
2021-05-09 09:41:25 -05:00
|
|
|
r#"fn foo(bar: $0impl Foo + Bar) {}"#,
|
2023-03-26 17:09:36 -05:00
|
|
|
r#"fn foo<$0F: Foo + Bar>(bar: F) {}"#,
|
2020-09-04 09:55:27 -05:00
|
|
|
);
|
|
|
|
}
|
2020-09-02 17:32:18 -05:00
|
|
|
}
|