Skip to content

Commit 099c320

Browse files
committed
fix: use PathTransform to resolve GenericArg
1 parent 6a7d3f1 commit 099c320

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

+13-29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ide_db::{
66
defs::Definition,
77
helpers::mod_path_to_ast,
88
imports::insert_use::{insert_use, ImportScope, InsertUseConfig},
9+
path_transform::PathTransform,
910
search::FileReference,
1011
FxHashSet, RootDatabase,
1112
};
@@ -105,6 +106,16 @@ pub(crate) fn extract_struct_from_enum_variant(
105106
.generic_param_list()
106107
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
107108
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());
109+
110+
// resolve GenericArg in field_list to actual type
111+
let field_list = field_list.clone_for_update();
112+
if let Some((target_scope, source_scope)) =
113+
ctx.sema.scope(enum_ast.syntax()).zip(ctx.sema.scope(field_list.syntax()))
114+
{
115+
PathTransform::generic_transformation(&target_scope, &source_scope)
116+
.apply(field_list.syntax());
117+
}
118+
108119
let def =
109120
create_struct_def(variant_name.clone(), &variant, &field_list, generics, &enum_ast);
110121

@@ -244,31 +255,6 @@ fn create_struct_def(
244255
// for fields without any existing visibility, use visibility of enum
245256
let field_list: ast::FieldList = match field_list {
246257
Either::Left(field_list) => {
247-
let field_list = field_list.clone_for_update();
248-
249-
// replace `Self` with the enum name when construct struct def
250-
field_list
251-
.fields()
252-
.filter_map(|field| match field.ty()? {
253-
ast::Type::PathType(p) => Some(
254-
p.syntax()
255-
.descendants_with_tokens()
256-
.filter_map(|it| {
257-
if it.kind() == T![Self] {
258-
let type_arg =
259-
make::type_arg(make::ty(&enum_.name()?.to_string()))
260-
.clone_for_update();
261-
Some(ted::replace(it, type_arg.syntax()))
262-
} else {
263-
None
264-
}
265-
})
266-
.count(),
267-
),
268-
_ => None,
269-
})
270-
.count();
271-
272258
if let Some(vis) = &enum_vis {
273259
field_list
274260
.fields()
@@ -277,11 +263,9 @@ fn create_struct_def(
277263
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
278264
}
279265

280-
field_list.into()
266+
field_list.clone().into()
281267
}
282268
Either::Right(field_list) => {
283-
let field_list = field_list.clone_for_update();
284-
285269
if let Some(vis) = &enum_vis {
286270
field_list
287271
.fields()
@@ -290,7 +274,7 @@ fn create_struct_def(
290274
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
291275
}
292276

293-
field_list.into()
277+
field_list.clone().into()
294278
}
295279
};
296280
field_list.reindent_to(IndentLevel::single());

0 commit comments

Comments
 (0)