mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2026-03-12 20:00:31 +00:00
remove make from extract_type_alias
This commit is contained in:
parent
0dc6097727
commit
2aeb269257
@ -2,7 +2,7 @@ use either::Either;
|
||||
use hir::HirDisplay;
|
||||
use ide_db::syntax_helpers::node_ext::walk_ty;
|
||||
use syntax::{
|
||||
ast::{self, AstNode, HasGenericArgs, HasGenericParams, HasName, edit::IndentLevel, make},
|
||||
ast::{self, AstNode, HasGenericArgs, HasGenericParams, HasName, edit::IndentLevel, syntax_factory::SyntaxFactory},
|
||||
syntax_editor,
|
||||
};
|
||||
|
||||
@ -44,9 +44,9 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||
let resolved_ty = if !resolved_ty.contains_unknown() {
|
||||
let module = ctx.sema.scope(ty.syntax())?.module();
|
||||
let resolved_ty = resolved_ty.display_source_code(ctx.db(), module.into(), false).ok()?;
|
||||
make::ty(&resolved_ty)
|
||||
resolved_ty
|
||||
} else {
|
||||
ty.clone()
|
||||
ty.to_string()
|
||||
};
|
||||
|
||||
acc.add(
|
||||
@ -54,7 +54,11 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||
"Extract type as type alias",
|
||||
target,
|
||||
|builder| {
|
||||
|
||||
let mut edit = builder.make_editor(node);
|
||||
let make = SyntaxFactory::without_mappings();
|
||||
|
||||
let resolved_ty = make.ty(&resolved_ty);
|
||||
|
||||
let mut known_generics = match item.generic_param_list() {
|
||||
Some(it) => it.generic_params().collect(),
|
||||
@ -68,22 +72,20 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||
}
|
||||
let generics = collect_used_generics(&ty, &known_generics);
|
||||
let generic_params =
|
||||
generics.map(|it| make::generic_param_list(it.into_iter().cloned()));
|
||||
generics.map(|it| make.generic_param_list(it.into_iter().cloned()));
|
||||
|
||||
// Replace original type with the alias
|
||||
let ty_args = generic_params.as_ref().map(|it| it.to_generic_args().generic_args());
|
||||
let new_ty = if let Some(ty_args) = ty_args {
|
||||
make::generic_ty_path_segment(make::name_ref("Type"), ty_args)
|
||||
make.generic_ty_path_segment(make.name_ref("Type"), ty_args)
|
||||
} else {
|
||||
make::path_segment(make::name_ref("Type"))
|
||||
}
|
||||
.clone_for_update();
|
||||
make.path_segment(make.name_ref("Type"))
|
||||
};
|
||||
edit.replace(ty.syntax(), new_ty.syntax());
|
||||
|
||||
// Insert new alias
|
||||
let ty_alias =
|
||||
make::ty_alias(None, "Type", generic_params, None, None, Some((resolved_ty, None)))
|
||||
.clone_for_update();
|
||||
make.ty_alias(None, "Type", generic_params, None, None, Some((resolved_ty, None)));
|
||||
|
||||
if let Some(cap) = ctx.config.snippet_cap
|
||||
&& let Some(name) = ty_alias.name()
|
||||
@ -96,7 +98,7 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||
syntax_editor::Position::before(node),
|
||||
vec![
|
||||
ty_alias.syntax().clone().into(),
|
||||
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
|
||||
make.whitespace(&format!("\n\n{indent}")).into(),
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@ -287,6 +287,14 @@ impl SyntaxFactory {
|
||||
ast
|
||||
}
|
||||
|
||||
pub fn generic_ty_path_segment(
|
||||
&self,
|
||||
name_ref: ast::NameRef,
|
||||
generic_args: impl IntoIterator<Item = ast::GenericArg>,
|
||||
) -> ast::PathSegment {
|
||||
make::generic_ty_path_segment(name_ref, generic_args).clone_for_update()
|
||||
}
|
||||
|
||||
pub fn path_segment_generics(
|
||||
&self,
|
||||
name_ref: ast::NameRef,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user