Skip to content

Commit c8846c4

Browse files
Merge pull request #22070 from A4-Tacks/extract-ty-alias-name
fix: adjust name of extract_type_alias
2 parents 05808ac + 53083be commit c8846c4

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use either::Either;
22
use hir::HirDisplay;
3-
use ide_db::syntax_helpers::node_ext::walk_ty;
3+
use ide_db::syntax_helpers::{node_ext::walk_ty, suggest_name::NameGenerator};
44
use syntax::{
55
ast::{self, AstNode, HasGenericArgs, HasGenericParams, HasName, edit::IndentLevel},
66
syntax_editor,
@@ -40,9 +40,10 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4040
);
4141
let target = ty.syntax().text_range();
4242

43+
let scope = ctx.sema.scope(ty.syntax())?;
4344
let resolved_ty = ctx.sema.resolve_type(&ty)?;
4445
let resolved_ty = if !resolved_ty.contains_unknown() {
45-
let module = ctx.sema.scope(ty.syntax())?.module();
46+
let module = scope.module();
4647
resolved_ty.display_source_code(ctx.db(), module.into(), false).ok()?
4748
} else {
4849
ty.to_string()
@@ -57,6 +58,7 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5758
let make = editor.make();
5859

5960
let resolved_ty = make.ty(&resolved_ty);
61+
let name = &NameGenerator::new_from_scope_non_locals(Some(scope)).suggest_name("Type");
6062

6163
let mut known_generics = match item.generic_param_list() {
6264
Some(it) => it.generic_params().collect(),
@@ -75,15 +77,15 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
7577
// Replace original type with the alias
7678
let ty_args = generic_params.as_ref().map(|it| it.to_generic_args().generic_args());
7779
let new_ty = if let Some(ty_args) = ty_args {
78-
make.generic_ty_path_segment(make.name_ref("Type"), ty_args)
80+
make.generic_ty_path_segment(make.name_ref(name), ty_args)
7981
} else {
80-
make.path_segment(make.name_ref("Type"))
82+
make.path_segment(make.name_ref(name))
8183
};
8284
editor.replace(ty.syntax(), new_ty.syntax());
8385

8486
// Insert new alias
8587
let ty_alias =
86-
make.ty_alias(None, "Type", generic_params, None, None, Some((resolved_ty, None)));
88+
make.ty_alias(None, name, generic_params, None, None, Some((resolved_ty, None)));
8789

8890
if let Some(cap) = ctx.config.snippet_cap
8991
&& let Some(name) = ty_alias.name()
@@ -447,4 +449,41 @@ fn main() {
447449
"#,
448450
)
449451
}
452+
453+
#[test]
454+
fn duplicate_names() {
455+
check_assist(
456+
extract_type_alias,
457+
r"
458+
struct Type;
459+
struct S {
460+
field: $0u8$0,
461+
}
462+
",
463+
r#"
464+
struct Type;
465+
type $0Type1 = u8;
466+
467+
struct S {
468+
field: Type1,
469+
}
470+
"#,
471+
);
472+
473+
check_assist(
474+
extract_type_alias,
475+
r"
476+
struct S<Type> {
477+
field: $0u8$0,
478+
}
479+
",
480+
r#"
481+
type $0Type1 = u8;
482+
483+
struct S<Type> {
484+
field: Type1,
485+
}
486+
"#,
487+
);
488+
}
450489
}

crates/ide-db/src/syntax_helpers/suggest_name.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ impl NameGenerator {
123123
generator
124124
}
125125

126+
pub fn new_from_scope_non_locals(scope: Option<SemanticsScope<'_>>) -> Self {
127+
let mut generator = Self::default();
128+
if let Some(scope) = scope {
129+
scope.process_all_names(&mut |name, scope| {
130+
if let hir::ScopeDef::Local(_) = scope {
131+
return;
132+
}
133+
generator.insert(name.as_str());
134+
});
135+
}
136+
137+
generator
138+
}
139+
126140
/// Suggest a name without conflicts. If the name conflicts with existing names,
127141
/// it will try to resolve the conflict by adding a numeric suffix.
128142
pub fn suggest_name(&mut self, name: &str) -> SmolStr {

0 commit comments

Comments
 (0)