Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extract_struct_from_enum_variant assist should resolve Self generic arg #16199

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ide_db::{
defs::Definition,
helpers::mod_path_to_ast,
imports::insert_use::{insert_use, ImportScope, InsertUseConfig},
path_transform::PathTransform,
search::FileReference,
FxHashSet, RootDatabase,
};
Expand Down Expand Up @@ -105,6 +106,16 @@ pub(crate) fn extract_struct_from_enum_variant(
.generic_param_list()
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());

// resolve GenericArg in field_list to actual type
let field_list = field_list.clone_for_update();
if let Some((target_scope, source_scope)) =
ctx.sema.scope(enum_ast.syntax()).zip(ctx.sema.scope(field_list.syntax()))
{
PathTransform::generic_transformation(&target_scope, &source_scope)
.apply(field_list.syntax());
}

let def =
create_struct_def(variant_name.clone(), &variant, &field_list, generics, &enum_ast);

Expand Down Expand Up @@ -244,8 +255,6 @@ fn create_struct_def(
// for fields without any existing visibility, use visibility of enum
let field_list: ast::FieldList = match field_list {
Either::Left(field_list) => {
let field_list = field_list.clone_for_update();

if let Some(vis) = &enum_vis {
field_list
.fields()
Expand All @@ -254,11 +263,9 @@ fn create_struct_def(
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
}

field_list.into()
field_list.clone().into()
}
Either::Right(field_list) => {
let field_list = field_list.clone_for_update();

if let Some(vis) = &enum_vis {
field_list
.fields()
Expand All @@ -267,7 +274,7 @@ fn create_struct_def(
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
}

field_list.into()
field_list.clone().into()
}
};
field_list.reindent_to(IndentLevel::single());
Expand Down Expand Up @@ -425,6 +432,59 @@ mod tests {

use super::*;

#[test]
fn issue_16197() {
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Bar $0{ node: Box<Self> },
Nil,
}
"#,
r#"
struct Bar{ node: Box<Foo> }

enum Foo {
Bar(Bar),
Nil,
}
"#,
);
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Bar $0{ node: Box<Self>, a: Arc<Box<Self>> },
Nil,
}
"#,
r#"
struct Bar{ node: Box<Foo>, a: Arc<Box<Foo>> }

enum Foo {
Bar(Bar),
Nil,
}
"#,
);
check_assist(
extract_struct_from_enum_variant,
r#"
enum Foo {
Nil(Box$0<Self>, Arc<Box<Self>>),
}
"#,
r#"
struct Nil(Box<Foo>, Arc<Box<Foo>>);

enum Foo {
Nil(Nil),
}
"#,
);
}

#[test]
fn test_extract_struct_several_fields_tuple() {
check_assist(
Expand Down