Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #22545 from WeiTheShinobi/inline_type_alias_adt_def
Make assist `inline_type_alias` work on ADT definitions
A4-Tacks 5 weeks ago
parent 4fde9dd · parent ef51602 · commit 7fff352
-rw-r--r--crates/ide-assists/src/handlers/inline_type_alias.rs77
1 files changed, 76 insertions, 1 deletions
diff --git a/crates/ide-assists/src/handlers/inline_type_alias.rs b/crates/ide-assists/src/handlers/inline_type_alias.rs
index e4a1314f5b..bb76e2743c 100644
--- a/crates/ide-assists/src/handlers/inline_type_alias.rs
+++ b/crates/ide-assists/src/handlers/inline_type_alias.rs
@@ -135,7 +135,20 @@ pub(crate) fn inline_type_alias(acc: &mut Assists, ctx: &AssistContext<'_, '_>)
PathResolution::SelfType(imp) => {
concrete_type = imp.source(ctx.db())?.value.self_ty()?;
}
- // FIXME: should also work in ADT definitions
+ PathResolution::Def(hir::ModuleDef::Adt(adt)) => {
+ let make = SyntaxFactory::without_mappings();
+ let src = adt.source(ctx.db())?.value;
+ let name = src.name()?;
+ let generic_params = src.generic_param_list();
+ let name_ref = make.name_ref(&name.text());
+ let segment = match generic_params {
+ Some(params) => {
+ make.path_segment_generics(name_ref, params.to_generic_args(&make))
+ }
+ None => make.path_segment(name_ref),
+ };
+ concrete_type = make.ty_path_from_segments([segment], false);
+ }
_ => return None,
}
@@ -997,6 +1010,68 @@ trait Tr {
}
#[test]
+ fn inline_self_type_in_adt_definition() {
+ check_assist(
+ inline_type_alias,
+ r#"
+enum Foo {
+ A(i32),
+ B(Box<Self$0>),
+}
+"#,
+ r#"
+enum Foo {
+ A(i32),
+ B(Box<Foo>),
+}
+"#,
+ );
+ check_assist(
+ inline_type_alias,
+ r#"
+struct Foo {
+ a: Box<Self$0>,
+}
+"#,
+ r#"
+struct Foo {
+ a: Box<Foo>,
+}
+"#,
+ );
+ check_assist(
+ inline_type_alias,
+ r#"
+struct Foo<T> {
+ a: T,
+ b: Box<Self$0>,
+}
+"#,
+ r#"
+struct Foo<T> {
+ a: T,
+ b: Box<Foo<T>>,
+}
+"#,
+ );
+ check_assist(
+ inline_type_alias,
+ r#"
+union Foo {
+ a: u32,
+ b: std::mem::ManuallyDrop<Box<Self$0>>,
+}
+"#,
+ r#"
+union Foo {
+ a: u32,
+ b: std::mem::ManuallyDrop<Box<Foo>>,
+}
+"#,
+ );
+ }
+
+ #[test]
fn inline_types_with_lifetime() {
check_assist(
inline_type_alias_uses,