2020-11-03 13:54:44 -06:00
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
use either::Either;
|
2021-03-08 13:14:52 -06:00
|
|
|
use hir::{Module, ModuleDef, Name, Variant};
|
2021-01-11 17:05:07 -06:00
|
|
|
use ide_db::{
|
|
|
|
defs::Definition,
|
2022-03-06 12:01:30 -06:00
|
|
|
helpers::mod_path_to_ast,
|
|
|
|
imports::insert_use::{insert_use, ImportScope, InsertUseConfig},
|
2021-01-12 08:51:02 -06:00
|
|
|
search::FileReference,
|
2022-04-25 11:51:59 -05:00
|
|
|
FxHashSet, RootDatabase,
|
2020-11-24 15:25:13 -06:00
|
|
|
};
|
2022-02-22 12:38:34 -06:00
|
|
|
use itertools::{Itertools, Position};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2021-06-02 10:54:57 -05:00
|
|
|
ast::{
|
2021-09-27 05:54:24 -05:00
|
|
|
self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasAttrs, HasGenericParams,
|
2022-02-12 16:21:41 -06:00
|
|
|
HasName, HasVisibility,
|
2021-06-02 10:54:57 -05:00
|
|
|
},
|
2022-02-22 12:38:34 -06:00
|
|
|
match_ast, ted, SyntaxElement,
|
2021-08-30 19:47:42 -05:00
|
|
|
SyntaxKind::*,
|
2021-06-02 10:54:57 -05:00
|
|
|
SyntaxNode, T,
|
2020-05-22 15:28:30 -05:00
|
|
|
};
|
|
|
|
|
2021-06-02 11:16:59 -05:00
|
|
|
use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists};
|
2020-05-22 15:28:30 -05:00
|
|
|
|
2020-06-03 13:43:57 -05:00
|
|
|
// Assist: extract_struct_from_enum_variant
|
2020-05-22 15:28:30 -05:00
|
|
|
//
|
2020-06-03 13:43:57 -05:00
|
|
|
// Extracts a struct from enum variant.
|
2020-05-22 15:28:30 -05:00
|
|
|
//
|
|
|
|
// ```
|
2021-01-06 14:15:48 -06:00
|
|
|
// enum A { $0One(u32, u32) }
|
2020-05-22 15:28:30 -05:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2021-08-30 20:18:28 -05:00
|
|
|
// struct One(u32, u32);
|
2020-05-22 15:28:30 -05:00
|
|
|
//
|
2020-06-03 13:43:57 -05:00
|
|
|
// enum A { One(One) }
|
2020-05-22 15:28:30 -05:00
|
|
|
// ```
|
2020-06-03 13:43:57 -05:00
|
|
|
pub(crate) fn extract_struct_from_enum_variant(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
|
|
|
) -> Option<()> {
|
2020-07-30 10:56:53 -05:00
|
|
|
let variant = ctx.find_node_at_offset::<ast::Variant>()?;
|
2020-11-03 13:54:44 -06:00
|
|
|
let field_list = extract_field_list_if_applicable(&variant)?;
|
2020-10-26 16:38:23 -05:00
|
|
|
|
2020-11-02 14:40:52 -06:00
|
|
|
let variant_name = variant.name()?;
|
2020-05-22 15:28:30 -05:00
|
|
|
let variant_hir = ctx.sema.to_def(&variant)?;
|
2020-11-03 12:55:14 -06:00
|
|
|
if existing_definition(ctx.db(), &variant_name, &variant_hir) {
|
2021-06-17 16:36:25 -05:00
|
|
|
cov_mark::hit!(test_extract_enum_not_applicable_if_struct_exists);
|
2020-05-22 15:28:30 -05:00
|
|
|
return None;
|
|
|
|
}
|
2020-11-03 13:54:44 -06:00
|
|
|
|
2020-05-23 04:53:02 -05:00
|
|
|
let enum_ast = variant.parent_enum();
|
2020-06-05 06:17:17 -05:00
|
|
|
let enum_hir = ctx.sema.to_def(&enum_ast)?;
|
2020-05-22 15:28:30 -05:00
|
|
|
let target = variant.syntax().text_range();
|
2020-06-08 17:01:40 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("extract_struct_from_enum_variant", AssistKind::RefactorRewrite),
|
2020-05-22 15:28:30 -05:00
|
|
|
"Extract struct from enum variant",
|
|
|
|
target,
|
2020-06-08 17:01:40 -05:00
|
|
|
|builder| {
|
2020-11-03 13:54:44 -06:00
|
|
|
let variant_hir_name = variant_hir.name(ctx.db());
|
|
|
|
let enum_module_def = ModuleDef::from(enum_hir);
|
2021-11-10 15:02:50 -06:00
|
|
|
let usages = Definition::Variant(variant_hir).usages(&ctx.sema).all();
|
2020-11-02 14:40:52 -06:00
|
|
|
|
2020-05-24 07:53:12 -05:00
|
|
|
let mut visited_modules_set = FxHashSet::default();
|
2020-11-03 13:54:44 -06:00
|
|
|
let current_module = enum_hir.module(ctx.db());
|
2020-05-23 04:53:02 -05:00
|
|
|
visited_modules_set.insert(current_module);
|
2021-04-20 10:36:36 -05:00
|
|
|
// record file references of the file the def resides in, we only want to swap to the edited file in the builder once
|
2021-04-19 19:05:22 -05:00
|
|
|
let mut def_file_references = None;
|
2021-01-12 08:51:02 -06:00
|
|
|
for (file_id, references) in usages {
|
2021-10-13 07:39:37 -05:00
|
|
|
if file_id == ctx.file_id() {
|
2021-04-19 19:05:22 -05:00
|
|
|
def_file_references = Some(references);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
builder.edit_file(file_id);
|
2021-04-20 10:36:36 -05:00
|
|
|
let processed = process_references(
|
|
|
|
ctx,
|
2021-06-02 11:16:59 -05:00
|
|
|
builder,
|
2021-04-20 10:36:36 -05:00
|
|
|
&mut visited_modules_set,
|
|
|
|
&enum_module_def,
|
|
|
|
&variant_hir_name,
|
|
|
|
references,
|
|
|
|
);
|
2021-04-20 12:28:18 -05:00
|
|
|
processed.into_iter().for_each(|(path, node, import)| {
|
|
|
|
apply_references(ctx.config.insert_use, path, node, import)
|
2021-04-20 10:36:36 -05:00
|
|
|
});
|
2020-11-02 14:40:52 -06:00
|
|
|
}
|
2021-10-13 07:39:37 -05:00
|
|
|
builder.edit_file(ctx.file_id());
|
2021-08-30 18:09:29 -05:00
|
|
|
|
2021-05-16 06:18:49 -05:00
|
|
|
let variant = builder.make_mut(variant.clone());
|
2021-04-20 10:36:36 -05:00
|
|
|
if let Some(references) = def_file_references {
|
|
|
|
let processed = process_references(
|
2021-04-19 19:05:22 -05:00
|
|
|
ctx,
|
2021-06-02 11:16:59 -05:00
|
|
|
builder,
|
2021-04-20 10:36:36 -05:00
|
|
|
&mut visited_modules_set,
|
2021-04-19 19:05:22 -05:00
|
|
|
&enum_module_def,
|
|
|
|
&variant_hir_name,
|
2021-04-20 10:36:36 -05:00
|
|
|
references,
|
2021-04-19 19:05:22 -05:00
|
|
|
);
|
2021-04-20 12:28:18 -05:00
|
|
|
processed.into_iter().for_each(|(path, node, import)| {
|
|
|
|
apply_references(ctx.config.insert_use, path, node, import)
|
2021-04-20 10:36:36 -05:00
|
|
|
});
|
2021-04-19 19:05:22 -05:00
|
|
|
}
|
2021-04-20 10:36:36 -05:00
|
|
|
|
2021-08-30 18:09:29 -05:00
|
|
|
let indent = enum_ast.indent_level();
|
2022-02-12 16:21:41 -06:00
|
|
|
let generic_params = enum_ast
|
|
|
|
.generic_param_list()
|
2022-02-21 16:00:16 -06:00
|
|
|
.and_then(|known_generics| extract_generic_params(&known_generics, &field_list));
|
|
|
|
let generics = generic_params.as_ref().map(|generics| generics.clone_for_update());
|
2022-02-12 16:21:41 -06:00
|
|
|
let def =
|
|
|
|
create_struct_def(variant_name.clone(), &variant, &field_list, generics, &enum_ast);
|
2021-08-30 18:09:29 -05:00
|
|
|
def.reindent_to(indent);
|
|
|
|
|
2021-04-20 10:36:36 -05:00
|
|
|
let start_offset = &variant.parent_enum().syntax().clone();
|
2021-08-30 18:09:29 -05:00
|
|
|
ted::insert_all_raw(
|
|
|
|
ted::Position::before(start_offset),
|
|
|
|
vec![
|
|
|
|
def.syntax().clone().into(),
|
|
|
|
make::tokens::whitespace(&format!("\n\n{}", indent)).into(),
|
|
|
|
],
|
|
|
|
);
|
2021-04-20 10:36:36 -05:00
|
|
|
|
2022-02-21 16:00:16 -06:00
|
|
|
update_variant(&variant, generic_params.map(|g| g.clone_for_update()));
|
2020-05-22 15:28:30 -05:00
|
|
|
},
|
2020-06-05 04:45:41 -05:00
|
|
|
)
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
|
2020-11-03 13:54:44 -06:00
|
|
|
fn extract_field_list_if_applicable(
|
|
|
|
variant: &ast::Variant,
|
|
|
|
) -> Option<Either<ast::RecordFieldList, ast::TupleFieldList>> {
|
|
|
|
match variant.kind() {
|
|
|
|
ast::StructKind::Record(field_list) if field_list.fields().next().is_some() => {
|
|
|
|
Some(Either::Left(field_list))
|
|
|
|
}
|
|
|
|
ast::StructKind::Tuple(field_list) if field_list.fields().count() > 1 => {
|
|
|
|
Some(Either::Right(field_list))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
fn existing_definition(db: &RootDatabase, variant_name: &ast::Name, variant: &Variant) -> bool {
|
2020-05-22 18:41:08 -05:00
|
|
|
variant
|
|
|
|
.parent_enum(db)
|
|
|
|
.module(db)
|
|
|
|
.scope(db, None)
|
|
|
|
.into_iter()
|
2020-11-03 12:55:14 -06:00
|
|
|
.filter(|(_, def)| match def {
|
|
|
|
// only check type-namespace
|
2021-01-05 06:45:46 -06:00
|
|
|
hir::ScopeDef::ModuleDef(def) => matches!(
|
|
|
|
def,
|
|
|
|
ModuleDef::Module(_)
|
|
|
|
| ModuleDef::Adt(_)
|
|
|
|
| ModuleDef::Variant(_)
|
|
|
|
| ModuleDef::Trait(_)
|
|
|
|
| ModuleDef::TypeAlias(_)
|
|
|
|
| ModuleDef::BuiltinType(_)
|
2020-11-03 12:55:14 -06:00
|
|
|
),
|
|
|
|
_ => false,
|
|
|
|
})
|
2021-03-08 13:14:52 -06:00
|
|
|
.any(|(name, _)| name.to_string() == variant_name.to_string())
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:21:41 -06:00
|
|
|
fn extract_generic_params(
|
|
|
|
known_generics: &ast::GenericParamList,
|
|
|
|
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
|
2022-02-21 16:00:16 -06:00
|
|
|
) -> Option<ast::GenericParamList> {
|
|
|
|
let mut generics = known_generics.generic_params().map(|param| (param, false)).collect_vec();
|
2022-02-12 16:21:41 -06:00
|
|
|
|
2022-02-21 16:00:16 -06:00
|
|
|
let tagged_one = match field_list {
|
2022-02-12 16:21:41 -06:00
|
|
|
Either::Left(field_list) => field_list
|
|
|
|
.fields()
|
|
|
|
.filter_map(|f| f.ty())
|
2022-02-21 16:00:16 -06:00
|
|
|
.fold(false, |tagged, ty| tag_generics_in_variant(&ty, &mut generics) || tagged),
|
2022-02-12 16:21:41 -06:00
|
|
|
Either::Right(field_list) => field_list
|
|
|
|
.fields()
|
|
|
|
.filter_map(|f| f.ty())
|
2022-02-21 16:00:16 -06:00
|
|
|
.fold(false, |tagged, ty| tag_generics_in_variant(&ty, &mut generics) || tagged),
|
|
|
|
};
|
2022-02-12 16:21:41 -06:00
|
|
|
|
2022-02-21 16:00:16 -06:00
|
|
|
let generics = generics.into_iter().filter_map(|(param, tag)| tag.then(|| param));
|
|
|
|
tagged_one.then(|| make::generic_param_list(generics))
|
2022-02-12 16:21:41 -06:00
|
|
|
}
|
|
|
|
|
2022-02-21 16:00:16 -06:00
|
|
|
fn tag_generics_in_variant(ty: &ast::Type, generics: &mut [(ast::GenericParam, bool)]) -> bool {
|
|
|
|
let mut tagged_one = false;
|
|
|
|
|
2022-02-21 12:51:09 -06:00
|
|
|
for token in ty.syntax().descendants_with_tokens().filter_map(SyntaxElement::into_token) {
|
2022-02-21 16:00:16 -06:00
|
|
|
for (param, tag) in generics.iter_mut().filter(|(_, tag)| !tag) {
|
|
|
|
match param {
|
|
|
|
ast::GenericParam::LifetimeParam(lt)
|
|
|
|
if matches!(token.kind(), T![lifetime_ident]) =>
|
|
|
|
{
|
2022-02-12 16:21:41 -06:00
|
|
|
if let Some(lt) = lt.lifetime() {
|
|
|
|
if lt.text().as_str() == token.text() {
|
2022-02-21 16:00:16 -06:00
|
|
|
*tag = true;
|
|
|
|
tagged_one = true;
|
2022-02-12 16:21:41 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 16:00:16 -06:00
|
|
|
param if matches!(token.kind(), T![ident]) => {
|
|
|
|
if match param {
|
|
|
|
ast::GenericParam::ConstParam(konst) => konst
|
|
|
|
.name()
|
|
|
|
.map(|name| name.text().as_str() == token.text())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
ast::GenericParam::TypeParam(ty) => ty
|
|
|
|
.name()
|
|
|
|
.map(|name| name.text().as_str() == token.text())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
ast::GenericParam::LifetimeParam(lt) => lt
|
|
|
|
.lifetime()
|
|
|
|
.map(|lt| lt.text().as_str() == token.text())
|
|
|
|
.unwrap_or_default(),
|
|
|
|
} {
|
|
|
|
*tag = true;
|
|
|
|
tagged_one = true;
|
|
|
|
break;
|
2022-02-12 16:21:41 -06:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 16:00:16 -06:00
|
|
|
_ => (),
|
2022-02-12 16:21:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 16:00:16 -06:00
|
|
|
|
|
|
|
tagged_one
|
2022-02-12 16:21:41 -06:00
|
|
|
}
|
|
|
|
|
2021-04-20 10:36:36 -05:00
|
|
|
fn create_struct_def(
|
2020-11-02 14:40:52 -06:00
|
|
|
variant_name: ast::Name,
|
2021-08-30 19:47:42 -05:00
|
|
|
variant: &ast::Variant,
|
2020-11-03 13:54:44 -06:00
|
|
|
field_list: &Either<ast::RecordFieldList, ast::TupleFieldList>,
|
2022-02-12 16:21:41 -06:00
|
|
|
generics: Option<ast::GenericParamList>,
|
2021-06-02 10:44:00 -05:00
|
|
|
enum_: &ast::Enum,
|
2021-04-20 12:28:18 -05:00
|
|
|
) -> ast::Struct {
|
2021-08-30 20:18:28 -05:00
|
|
|
let enum_vis = enum_.visibility();
|
2021-05-11 01:54:48 -05:00
|
|
|
|
2021-08-30 20:18:28 -05:00
|
|
|
let insert_vis = |node: &'_ SyntaxNode, vis: &'_ SyntaxNode| {
|
|
|
|
let vis = vis.clone_for_update();
|
|
|
|
ted::insert(ted::Position::before(node), vis);
|
2021-05-11 01:54:48 -05:00
|
|
|
};
|
|
|
|
|
2021-08-30 20:18:28 -05:00
|
|
|
// for fields without any existing visibility, use visibility of enum
|
2021-08-30 18:09:29 -05:00
|
|
|
let field_list: ast::FieldList = match field_list {
|
2020-11-03 13:54:44 -06:00
|
|
|
Either::Left(field_list) => {
|
2021-05-11 01:54:48 -05:00
|
|
|
let field_list = field_list.clone_for_update();
|
|
|
|
|
2021-08-30 20:18:28 -05:00
|
|
|
if let Some(vis) = &enum_vis {
|
|
|
|
field_list
|
|
|
|
.fields()
|
|
|
|
.filter(|field| field.visibility().is_none())
|
|
|
|
.filter_map(|field| field.name())
|
|
|
|
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
|
|
|
|
}
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
field_list.into()
|
2020-11-03 13:54:44 -06:00
|
|
|
}
|
2021-05-11 01:54:48 -05:00
|
|
|
Either::Right(field_list) => {
|
|
|
|
let field_list = field_list.clone_for_update();
|
|
|
|
|
2021-08-30 20:18:28 -05:00
|
|
|
if let Some(vis) = &enum_vis {
|
|
|
|
field_list
|
|
|
|
.fields()
|
|
|
|
.filter(|field| field.visibility().is_none())
|
|
|
|
.filter_map(|field| field.ty())
|
|
|
|
.for_each(|it| insert_vis(it.syntax(), vis.syntax()));
|
|
|
|
}
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
field_list.into()
|
|
|
|
}
|
2020-11-03 13:54:44 -06:00
|
|
|
};
|
2020-05-22 15:28:30 -05:00
|
|
|
|
2021-08-30 18:09:29 -05:00
|
|
|
field_list.reindent_to(IndentLevel::single());
|
|
|
|
|
2022-02-12 16:21:41 -06:00
|
|
|
let strukt = make::struct_(enum_vis, variant_name, generics, field_list).clone_for_update();
|
2021-06-02 10:54:57 -05:00
|
|
|
|
2021-08-30 19:47:42 -05:00
|
|
|
// FIXME: Consider making this an actual function somewhere (like in `AttrsOwnerEdit`) after some deliberation
|
|
|
|
let attrs_and_docs = |node: &SyntaxNode| {
|
|
|
|
let mut select_next_ws = false;
|
|
|
|
node.children_with_tokens().filter(move |child| {
|
|
|
|
let accept = match child.kind() {
|
|
|
|
ATTR | COMMENT => {
|
|
|
|
select_next_ws = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
WHITESPACE if select_next_ws => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
select_next_ws = false;
|
|
|
|
|
|
|
|
accept
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// copy attributes & comments from variant
|
|
|
|
let variant_attrs = attrs_and_docs(variant.syntax())
|
|
|
|
.map(|tok| match tok.kind() {
|
|
|
|
WHITESPACE => make::tokens::single_newline().into(),
|
2021-09-13 11:50:19 -05:00
|
|
|
_ => tok,
|
2021-08-30 19:47:42 -05:00
|
|
|
})
|
|
|
|
.collect();
|
2022-02-22 12:38:34 -06:00
|
|
|
ted::insert_all(ted::Position::first_child_of(strukt.syntax()), variant_attrs);
|
2021-08-30 19:47:42 -05:00
|
|
|
|
|
|
|
// copy attributes from enum
|
2021-06-02 10:54:57 -05:00
|
|
|
ted::insert_all(
|
2022-02-22 12:38:34 -06:00
|
|
|
ted::Position::first_child_of(strukt.syntax()),
|
2021-06-02 10:54:57 -05:00
|
|
|
enum_.attrs().map(|it| it.syntax().clone_for_update().into()).collect(),
|
|
|
|
);
|
|
|
|
strukt
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
|
2022-02-21 16:00:16 -06:00
|
|
|
fn update_variant(variant: &ast::Variant, generics: Option<ast::GenericParamList>) -> Option<()> {
|
2020-11-03 13:54:44 -06:00
|
|
|
let name = variant.name()?;
|
2022-02-12 16:21:41 -06:00
|
|
|
let ty = generics
|
2022-02-21 16:00:16 -06:00
|
|
|
.filter(|generics| generics.generic_params().count() > 0)
|
2022-02-12 16:21:41 -06:00
|
|
|
.map(|generics| {
|
2022-02-22 12:38:34 -06:00
|
|
|
let mut generic_str = String::with_capacity(8);
|
|
|
|
|
|
|
|
for (p, more) in generics.generic_params().with_position().map(|p| match p {
|
|
|
|
Position::First(p) | Position::Middle(p) => (p, true),
|
|
|
|
Position::Last(p) | Position::Only(p) => (p, false),
|
|
|
|
}) {
|
|
|
|
match p {
|
|
|
|
ast::GenericParam::ConstParam(konst) => {
|
|
|
|
if let Some(name) = konst.name() {
|
|
|
|
generic_str.push_str(name.text().as_str());
|
2022-02-21 16:00:16 -06:00
|
|
|
}
|
2022-02-22 12:38:34 -06:00
|
|
|
}
|
|
|
|
ast::GenericParam::LifetimeParam(lt) => {
|
|
|
|
if let Some(lt) = lt.lifetime() {
|
|
|
|
generic_str.push_str(lt.text().as_str());
|
2022-02-21 16:00:16 -06:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 12:38:34 -06:00
|
|
|
ast::GenericParam::TypeParam(ty) => {
|
|
|
|
if let Some(name) = ty.name() {
|
|
|
|
generic_str.push_str(name.text().as_str());
|
|
|
|
}
|
2022-02-21 16:00:16 -06:00
|
|
|
}
|
2022-02-22 12:38:34 -06:00
|
|
|
}
|
|
|
|
if more {
|
|
|
|
generic_str.push_str(", ");
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 16:00:16 -06:00
|
|
|
|
|
|
|
make::ty(&format!("{}<{}>", &name.text(), &generic_str))
|
2022-02-12 16:21:41 -06:00
|
|
|
})
|
|
|
|
.unwrap_or_else(|| make::ty(&name.text()));
|
|
|
|
|
2021-06-02 10:44:00 -05:00
|
|
|
let tuple_field = make::tuple_field(None, ty);
|
2020-11-03 13:54:44 -06:00
|
|
|
let replacement = make::variant(
|
|
|
|
name,
|
|
|
|
Some(ast::FieldList::TupleFieldList(make::tuple_field_list(iter::once(tuple_field)))),
|
2021-04-19 19:05:22 -05:00
|
|
|
)
|
|
|
|
.clone_for_update();
|
|
|
|
ted::replace(variant.syntax(), replacement.syntax());
|
2020-05-22 15:28:30 -05:00
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:28:18 -05:00
|
|
|
fn apply_references(
|
|
|
|
insert_use_cfg: InsertUseConfig,
|
|
|
|
segment: ast::PathSegment,
|
|
|
|
node: SyntaxNode,
|
|
|
|
import: Option<(ImportScope, hir::ModPath)>,
|
|
|
|
) {
|
|
|
|
if let Some((scope, path)) = import {
|
2021-06-18 16:11:56 -05:00
|
|
|
insert_use(&scope, mod_path_to_ast(&path), &insert_use_cfg);
|
2021-04-20 12:28:18 -05:00
|
|
|
}
|
2021-06-02 10:44:00 -05:00
|
|
|
// deep clone to prevent cycle
|
|
|
|
let path = make::path_from_segments(iter::once(segment.clone_subtree()), false);
|
|
|
|
ted::insert_raw(ted::Position::before(segment.syntax()), path.clone_for_update().syntax());
|
2021-04-20 12:28:18 -05:00
|
|
|
ted::insert_raw(ted::Position::before(segment.syntax()), make::token(T!['(']));
|
|
|
|
ted::insert_raw(ted::Position::after(&node), make::token(T![')']));
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:36:36 -05:00
|
|
|
fn process_references(
|
2020-05-22 15:28:30 -05:00
|
|
|
ctx: &AssistContext,
|
2021-06-02 11:16:59 -05:00
|
|
|
builder: &mut AssistBuilder,
|
2021-04-20 10:36:36 -05:00
|
|
|
visited_modules: &mut FxHashSet<Module>,
|
2020-11-02 14:40:52 -06:00
|
|
|
enum_module_def: &ModuleDef,
|
|
|
|
variant_hir_name: &Name,
|
2021-04-20 10:36:36 -05:00
|
|
|
refs: Vec<FileReference>,
|
|
|
|
) -> Vec<(ast::PathSegment, SyntaxNode, Option<(ImportScope, hir::ModPath)>)> {
|
2021-04-20 12:28:18 -05:00
|
|
|
// we have to recollect here eagerly as we are about to edit the tree we need to calculate the changes
|
|
|
|
// and corresponding nodes up front
|
2021-04-20 10:36:36 -05:00
|
|
|
refs.into_iter()
|
|
|
|
.flat_map(|reference| {
|
2021-06-02 11:16:59 -05:00
|
|
|
let (segment, scope_node, module) = reference_to_node(&ctx.sema, reference)?;
|
|
|
|
let segment = builder.make_mut(segment);
|
|
|
|
let scope_node = builder.make_syntax_mut(scope_node);
|
2021-04-20 10:36:36 -05:00
|
|
|
if !visited_modules.contains(&module) {
|
|
|
|
let mod_path = module.find_use_path_prefixed(
|
|
|
|
ctx.sema.db,
|
|
|
|
*enum_module_def,
|
|
|
|
ctx.config.insert_use.prefix_kind,
|
|
|
|
);
|
|
|
|
if let Some(mut mod_path) = mod_path {
|
|
|
|
mod_path.pop_segment();
|
|
|
|
mod_path.push_segment(variant_hir_name.clone());
|
2021-11-03 15:12:36 -05:00
|
|
|
let scope = ImportScope::find_insert_use_container(&scope_node, &ctx.sema)?;
|
2021-04-20 10:36:36 -05:00
|
|
|
visited_modules.insert(module);
|
|
|
|
return Some((segment, scope_node, Some((scope, mod_path))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some((segment, scope_node, None))
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reference_to_node(
|
|
|
|
sema: &hir::Semantics<RootDatabase>,
|
|
|
|
reference: FileReference,
|
|
|
|
) -> Option<(ast::PathSegment, SyntaxNode, hir::Module)> {
|
2021-06-02 11:16:59 -05:00
|
|
|
let segment =
|
|
|
|
reference.name.as_name_ref()?.syntax().parent().and_then(ast::PathSegment::cast)?;
|
|
|
|
let parent = segment.parent_path().syntax().parent()?;
|
|
|
|
let expr_or_pat = match_ast! {
|
|
|
|
match parent {
|
|
|
|
ast::PathExpr(_it) => parent.parent()?,
|
|
|
|
ast::RecordExpr(_it) => parent,
|
|
|
|
ast::TupleStructPat(_it) => parent,
|
|
|
|
ast::RecordPat(_it) => parent,
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
2022-03-31 04:12:08 -05:00
|
|
|
let module = sema.scope(&expr_or_pat)?.module();
|
2021-06-02 11:16:59 -05:00
|
|
|
Some((segment, expr_or_pat, module))
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-11-24 15:25:13 -06:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-05-22 15:28:30 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-11-03 12:55:14 -06:00
|
|
|
fn test_extract_struct_several_fields_tuple() {
|
2020-05-22 15:28:30 -05:00
|
|
|
check_assist(
|
2020-06-03 13:43:57 -05:00
|
|
|
extract_struct_from_enum_variant,
|
2021-01-06 14:15:48 -06:00
|
|
|
"enum A { $0One(u32, u32) }",
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"struct One(u32, u32);
|
2020-05-22 15:28:30 -05:00
|
|
|
|
2020-11-03 12:55:14 -06:00
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_several_fields_named() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
2021-01-06 14:15:48 -06:00
|
|
|
"enum A { $0One { foo: u32, bar: u32 } }",
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"struct One{ foo: u32, bar: u32 }
|
2020-11-03 13:54:44 -06:00
|
|
|
|
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_one_field_named() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
2021-01-06 14:15:48 -06:00
|
|
|
"enum A { $0One { foo: u32 } }",
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"struct One{ foo: u32 }
|
2020-11-03 12:55:14 -06:00
|
|
|
|
2021-05-11 01:54:48 -05:00
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 10:44:00 -05:00
|
|
|
#[test]
|
2021-06-02 10:54:57 -05:00
|
|
|
fn test_extract_struct_carries_over_generics() {
|
2021-06-02 10:44:00 -05:00
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r"enum En<T> { Var { a: T$0 } }",
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"struct Var<T>{ a: T }
|
2021-06-02 10:44:00 -05:00
|
|
|
|
|
|
|
enum En<T> { Var(Var<T>) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:54:48 -05:00
|
|
|
#[test]
|
2021-06-02 10:54:57 -05:00
|
|
|
fn test_extract_struct_carries_over_attributes() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"#[derive(Debug)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum Enum { Variant{ field: u32$0 } }"#,
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"#[derive(Debug)]#[derive(Clone)] struct Variant{ field: u32 }
|
2021-06-02 10:54:57 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
enum Enum { Variant(Variant) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-30 18:09:29 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_indent_to_parent_enum() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum Enum {
|
|
|
|
Variant {
|
|
|
|
field: u32$0
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct Variant{
|
2021-08-30 20:18:28 -05:00
|
|
|
field: u32
|
2021-08-30 18:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Enum {
|
|
|
|
Variant(Variant)
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_indent_to_parent_enum_in_mod() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
mod indenting {
|
|
|
|
enum Enum {
|
|
|
|
Variant {
|
|
|
|
field: u32$0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
mod indenting {
|
|
|
|
struct Variant{
|
2021-08-30 20:18:28 -05:00
|
|
|
field: u32
|
2021-08-30 18:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Enum {
|
|
|
|
Variant(Variant)
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-02 10:54:57 -05:00
|
|
|
#[test]
|
2021-05-11 01:54:48 -05:00
|
|
|
fn test_extract_struct_keep_comments_and_attrs_one_field_named() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum A {
|
|
|
|
$0One {
|
|
|
|
// leading comment
|
|
|
|
/// doc comment
|
|
|
|
#[an_attr]
|
|
|
|
foo: u32
|
|
|
|
// trailing comment
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct One{
|
2021-08-30 18:09:29 -05:00
|
|
|
// leading comment
|
|
|
|
/// doc comment
|
|
|
|
#[an_attr]
|
2021-08-30 20:18:28 -05:00
|
|
|
foo: u32
|
2021-08-30 18:09:29 -05:00
|
|
|
// trailing comment
|
|
|
|
}
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
enum A {
|
|
|
|
One(One)
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_comments_and_attrs_several_fields_named() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum A {
|
|
|
|
$0One {
|
|
|
|
// comment
|
|
|
|
/// doc
|
|
|
|
#[attr]
|
|
|
|
foo: u32,
|
|
|
|
// comment
|
|
|
|
#[attr]
|
|
|
|
/// doc
|
|
|
|
bar: u32
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct One{
|
2021-08-30 18:09:29 -05:00
|
|
|
// comment
|
|
|
|
/// doc
|
|
|
|
#[attr]
|
2021-08-30 20:18:28 -05:00
|
|
|
foo: u32,
|
2021-08-30 18:09:29 -05:00
|
|
|
// comment
|
|
|
|
#[attr]
|
|
|
|
/// doc
|
2021-08-30 20:18:28 -05:00
|
|
|
bar: u32
|
2021-08-30 18:09:29 -05:00
|
|
|
}
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
enum A {
|
|
|
|
One(One)
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_comments_and_attrs_several_fields_tuple() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
"enum A { $0One(/* comment */ #[attr] u32, /* another */ u32 /* tail */) }",
|
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One(/* comment */ #[attr] u32, /* another */ u32 /* tail */);
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:47:42 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_comments_and_attrs_on_variant_struct() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum A {
|
|
|
|
/* comment */
|
|
|
|
// other
|
|
|
|
/// comment
|
|
|
|
#[attr]
|
|
|
|
$0One {
|
|
|
|
a: u32
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
/* comment */
|
|
|
|
// other
|
|
|
|
/// comment
|
|
|
|
#[attr]
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One{
|
|
|
|
a: u32
|
2021-08-30 19:47:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum A {
|
|
|
|
One(One)
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_comments_and_attrs_on_variant_tuple() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum A {
|
|
|
|
/* comment */
|
|
|
|
// other
|
|
|
|
/// comment
|
|
|
|
#[attr]
|
|
|
|
$0One(u32, u32)
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
/* comment */
|
|
|
|
// other
|
|
|
|
/// comment
|
|
|
|
#[attr]
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One(u32, u32);
|
2021-08-30 19:47:42 -05:00
|
|
|
|
|
|
|
enum A {
|
|
|
|
One(One)
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:54:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_existing_visibility_named() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
2021-08-30 20:18:28 -05:00
|
|
|
"enum A { $0One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 } }",
|
2021-05-11 01:54:48 -05:00
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One{ a: u32, pub(crate) b: u32, pub(super) c: u32, d: u32 }
|
2021-05-11 01:54:48 -05:00
|
|
|
|
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keep_existing_visibility_tuple() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
2021-08-30 20:18:28 -05:00
|
|
|
"enum A { $0One(u32, pub(crate) u32, pub(super) u32, u32) }",
|
2021-05-11 01:54:48 -05:00
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One(u32, pub(crate) u32, pub(super) u32, u32);
|
2021-05-11 01:54:48 -05:00
|
|
|
|
2020-11-03 12:55:14 -06:00
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_enum_variant_name_value_namespace() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"const One: () = ();
|
2021-01-06 14:15:48 -06:00
|
|
|
enum A { $0One(u32, u32) }"#,
|
2020-11-03 12:55:14 -06:00
|
|
|
r#"const One: () = ();
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One(u32, u32);
|
|
|
|
|
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_no_visibility() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
"enum A { $0One(u32, u32) }",
|
|
|
|
r#"
|
|
|
|
struct One(u32, u32);
|
2020-11-03 12:55:14 -06:00
|
|
|
|
2020-05-22 15:28:30 -05:00
|
|
|
enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_pub_visibility() {
|
|
|
|
check_assist(
|
2020-06-03 13:43:57 -05:00
|
|
|
extract_struct_from_enum_variant,
|
2021-01-06 14:15:48 -06:00
|
|
|
"pub enum A { $0One(u32, u32) }",
|
2021-08-30 20:18:28 -05:00
|
|
|
r#"
|
|
|
|
pub struct One(pub u32, pub u32);
|
2020-05-22 15:28:30 -05:00
|
|
|
|
|
|
|
pub enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-30 20:18:28 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_pub_in_mod_visibility() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
"pub(in something) enum A { $0One{ a: u32, b: u32 } }",
|
|
|
|
r#"
|
|
|
|
pub(in something) struct One{ pub(in something) a: u32, pub(in something) b: u32 }
|
|
|
|
|
|
|
|
pub(in something) enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_pub_crate_visibility() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
"pub(crate) enum A { $0One{ a: u32, b: u32, c: u32 } }",
|
|
|
|
r#"
|
|
|
|
pub(crate) struct One{ pub(crate) a: u32, pub(crate) b: u32, pub(crate) c: u32 }
|
|
|
|
|
|
|
|
pub(crate) enum A { One(One) }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:28:30 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_with_complex_imports() {
|
|
|
|
check_assist(
|
2020-06-03 13:43:57 -05:00
|
|
|
extract_struct_from_enum_variant,
|
2020-05-22 15:28:30 -05:00
|
|
|
r#"mod my_mod {
|
|
|
|
fn another_fn() {
|
|
|
|
let m = my_other_mod::MyEnum::MyField(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod my_other_mod {
|
|
|
|
fn another_fn() {
|
|
|
|
let m = MyEnum::MyField(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum MyEnum {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0MyField(u8, u8),
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn another_fn() {
|
|
|
|
let m = my_mod::my_other_mod::MyEnum::MyField(1, 1);
|
|
|
|
}"#,
|
|
|
|
r#"use my_mod::my_other_mod::MyField;
|
|
|
|
|
|
|
|
mod my_mod {
|
2020-11-12 10:32:45 -06:00
|
|
|
use self::my_other_mod::MyField;
|
2020-05-22 15:28:30 -05:00
|
|
|
|
|
|
|
fn another_fn() {
|
|
|
|
let m = my_other_mod::MyEnum::MyField(MyField(1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod my_other_mod {
|
|
|
|
fn another_fn() {
|
|
|
|
let m = MyEnum::MyField(MyField(1, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MyField(pub u8, pub u8);
|
|
|
|
|
2021-08-30 18:09:29 -05:00
|
|
|
pub enum MyEnum {
|
2020-05-22 15:28:30 -05:00
|
|
|
MyField(MyField),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn another_fn() {
|
|
|
|
let m = my_mod::my_other_mod::MyEnum::MyField(MyField(1, 1));
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-12 10:13:33 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_record_fix_references() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum E {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0V { i: i32, j: i32 }
|
2020-11-12 10:13:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
2021-06-02 11:16:59 -05:00
|
|
|
let E::V { i, j } = E::V { i: 9, j: 2 };
|
2020-11-12 10:13:33 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct V{ i: i32, j: i32 }
|
2020-11-12 10:13:33 -06:00
|
|
|
|
|
|
|
enum E {
|
|
|
|
V(V)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
2021-06-02 11:16:59 -05:00
|
|
|
let E::V(V { i, j }) = E::V(V { i: 9, j: 2 });
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_record_fix_references2() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum E {
|
|
|
|
$0V(i32, i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let E::V(i, j) = E::V(9, 2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct V(i32, i32);
|
2021-06-02 11:16:59 -05:00
|
|
|
|
|
|
|
enum E {
|
|
|
|
V(V)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
|
|
|
let E::V(V(i, j)) = E::V(V(9, 2));
|
2020-11-12 10:13:33 -06:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-09 07:22:45 -06:00
|
|
|
#[test]
|
|
|
|
fn test_several_files() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
enum E {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0V(i32, i32)
|
2020-11-09 07:22:45 -06:00
|
|
|
}
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
use crate::E;
|
|
|
|
fn f() {
|
|
|
|
let e = E::V(9, 2);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
2021-08-30 20:18:28 -05:00
|
|
|
struct V(i32, i32);
|
2020-11-09 07:22:45 -06:00
|
|
|
|
|
|
|
enum E {
|
|
|
|
V(V)
|
|
|
|
}
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
2020-11-12 10:32:45 -06:00
|
|
|
use crate::{E, V};
|
2020-11-09 07:22:45 -06:00
|
|
|
fn f() {
|
|
|
|
let e = E::V(V(9, 2));
|
|
|
|
}
|
2020-11-09 09:40:41 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_several_files_record() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
enum E {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0V { i: i32, j: i32 }
|
2020-11-09 09:40:41 -06:00
|
|
|
}
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
use crate::E;
|
|
|
|
fn f() {
|
|
|
|
let e = E::V { i: 9, j: 2 };
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-11-12 10:13:33 -06:00
|
|
|
//- /main.rs
|
2021-08-30 20:18:28 -05:00
|
|
|
struct V{ i: i32, j: i32 }
|
2020-11-09 09:40:41 -06:00
|
|
|
|
|
|
|
enum E {
|
|
|
|
V(V)
|
|
|
|
}
|
|
|
|
mod foo;
|
|
|
|
|
2020-11-12 10:13:33 -06:00
|
|
|
//- /foo.rs
|
2020-11-12 10:32:45 -06:00
|
|
|
use crate::{E, V};
|
2020-11-12 10:13:33 -06:00
|
|
|
fn f() {
|
|
|
|
let e = E::V(V { i: 9, j: 2 });
|
|
|
|
}
|
2020-11-09 07:22:45 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-12 10:47:58 -06:00
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_record_nested_call_exp() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
enum A { $0One { a: u32, b: u32 } }
|
2020-11-12 10:47:58 -06:00
|
|
|
|
|
|
|
struct B(A);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let _ = B(A::One { a: 1, b: 2 });
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2021-08-30 20:18:28 -05:00
|
|
|
struct One{ a: u32, b: u32 }
|
2020-11-12 10:47:58 -06:00
|
|
|
|
|
|
|
enum A { One(One) }
|
|
|
|
|
|
|
|
struct B(A);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let _ = B(A::One(One { a: 1, b: 2 }));
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:28:30 -05:00
|
|
|
#[test]
|
|
|
|
fn test_extract_enum_not_applicable_for_element_with_no_fields() {
|
2021-06-17 16:36:25 -05:00
|
|
|
check_assist_not_applicable(extract_struct_from_enum_variant, r#"enum A { $0One }"#);
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_enum_not_applicable_if_struct_exists() {
|
2021-06-17 16:36:25 -05:00
|
|
|
cov_mark::check!(test_extract_enum_not_applicable_if_struct_exists);
|
|
|
|
check_assist_not_applicable(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
struct One;
|
|
|
|
enum A { $0One(u8, u32) }
|
|
|
|
"#,
|
2020-05-22 15:28:30 -05:00
|
|
|
);
|
|
|
|
}
|
2020-10-26 16:38:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_not_applicable_one_field() {
|
2021-06-17 16:36:25 -05:00
|
|
|
check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0One(u32) }");
|
2020-10-26 16:38:23 -05:00
|
|
|
}
|
2020-11-03 13:54:44 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_not_applicable_no_field_tuple() {
|
2021-06-17 16:36:25 -05:00
|
|
|
check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None() }");
|
2020-11-03 13:54:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_not_applicable_no_field_named() {
|
2021-06-17 16:36:25 -05:00
|
|
|
check_assist_not_applicable(extract_struct_from_enum_variant, r"enum A { $0None {} }");
|
2020-11-03 13:54:44 -06:00
|
|
|
}
|
2022-02-12 16:21:41 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_only_copies_needed_generics() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum X<'a, 'b, 'x> {
|
|
|
|
$0A { a: &'a &'x mut () },
|
|
|
|
B { b: &'b () },
|
|
|
|
C { c: () },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct A<'a, 'x>{ a: &'a &'x mut () }
|
|
|
|
|
|
|
|
enum X<'a, 'b, 'x> {
|
|
|
|
A(A<'a, 'x>),
|
|
|
|
B { b: &'b () },
|
|
|
|
C { c: () },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_with_liftime_type_const() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum X<'b, T, V, const C: usize> {
|
|
|
|
$0A { a: T, b: X<'b>, c: [u8; C] },
|
|
|
|
D { d: V },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct A<'b, T, const C: usize>{ a: T, b: X<'b>, c: [u8; C] }
|
|
|
|
|
|
|
|
enum X<'b, T, V, const C: usize> {
|
|
|
|
A(A<'b, T, C>),
|
|
|
|
D { d: V },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_without_generics() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum X<'a, 'b> {
|
|
|
|
A { a: &'a () },
|
|
|
|
B { b: &'b () },
|
|
|
|
$0C { c: () },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct C{ c: () }
|
|
|
|
|
|
|
|
enum X<'a, 'b> {
|
|
|
|
A { a: &'a () },
|
|
|
|
B { b: &'b () },
|
|
|
|
C(C),
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_struct_keeps_trait_bounds() {
|
|
|
|
check_assist(
|
|
|
|
extract_struct_from_enum_variant,
|
|
|
|
r#"
|
|
|
|
enum En<T: TraitT, V: TraitV> {
|
|
|
|
$0A { a: T },
|
|
|
|
B { b: V },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct A<T: TraitT>{ a: T }
|
|
|
|
|
|
|
|
enum En<T: TraitT, V: TraitV> {
|
|
|
|
A(A<T>),
|
|
|
|
B { b: V },
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2020-05-22 15:28:30 -05:00
|
|
|
}
|