mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Migrate extract_type_alias
to mutable ast
This commit is contained in:
parent
e6e72bf9d5
commit
dd5f05590e
2 changed files with 30 additions and 23 deletions
|
@ -1,6 +1,9 @@
|
|||
use either::Either;
|
||||
use ide_db::syntax_helpers::node_ext::walk_ty;
|
||||
use syntax::ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName};
|
||||
use syntax::{
|
||||
ast::{self, edit::IndentLevel, make, AstNode, HasGenericParams, HasName},
|
||||
ted,
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
|
||||
|
@ -34,14 +37,16 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
|||
|| item.syntax(),
|
||||
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),
|
||||
);
|
||||
let insert_pos = node.text_range().start();
|
||||
let target = ty.syntax().text_range();
|
||||
|
||||
acc.add(
|
||||
AssistId("extract_type_alias", AssistKind::RefactorExtract),
|
||||
"Extract type as type alias",
|
||||
target,
|
||||
|builder| {
|
||||
|edit| {
|
||||
let node = edit.make_syntax_mut(node.clone());
|
||||
let target_ty = edit.make_mut(ty.clone());
|
||||
|
||||
let mut known_generics = match item.generic_param_list() {
|
||||
Some(it) => it.generic_params().collect(),
|
||||
None => Vec::new(),
|
||||
|
@ -56,27 +61,29 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
|||
let generic_params =
|
||||
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_or(String::new(), |it| it.to_generic_args().to_string());
|
||||
let replacement = format!("Type{ty_args}");
|
||||
builder.replace(target, replacement);
|
||||
// FIXME: replace with a `ast::make` constructor
|
||||
let new_ty = make::ty(&format!("Type{ty_args}")).clone_for_update();
|
||||
ted::replace(target_ty.syntax(), new_ty.syntax());
|
||||
|
||||
let indent = IndentLevel::from_node(node);
|
||||
let generic_params = generic_params.map_or(String::new(), |it| it.to_string());
|
||||
match ctx.config.snippet_cap {
|
||||
Some(cap) => {
|
||||
builder.insert_snippet(
|
||||
cap,
|
||||
insert_pos,
|
||||
format!("type $0Type{generic_params} = {ty};\n\n{indent}"),
|
||||
);
|
||||
}
|
||||
None => {
|
||||
builder.insert(
|
||||
insert_pos,
|
||||
format!("type Type{generic_params} = {ty};\n\n{indent}"),
|
||||
);
|
||||
// Insert new alias
|
||||
let indent = IndentLevel::from_node(&node);
|
||||
let ty_alias = make::ty_alias("Type", generic_params, None, None, Some((ty, None)))
|
||||
.clone_for_update();
|
||||
ted::insert_all(
|
||||
ted::Position::before(node),
|
||||
vec![
|
||||
ty_alias.syntax().clone().into(),
|
||||
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
|
||||
],
|
||||
);
|
||||
|
||||
if let Some(cap) = ctx.config.snippet_cap {
|
||||
if let Some(name) = ty_alias.name() {
|
||||
edit.add_tabstop_before(cap, name);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -166,7 +166,7 @@ pub fn ty_alias(
|
|||
assignment: Option<(ast::Type, Option<ast::WhereClause>)>,
|
||||
) -> ast::TypeAlias {
|
||||
let mut s = String::new();
|
||||
s.push_str(&format!("type {} ", ident));
|
||||
s.push_str(&format!("type {}", ident));
|
||||
|
||||
if let Some(list) = generic_param_list {
|
||||
s.push_str(&list.to_string());
|
||||
|
@ -182,9 +182,9 @@ pub fn ty_alias(
|
|||
|
||||
if let Some(exp) = assignment {
|
||||
if let Some(cl) = exp.1 {
|
||||
s.push_str(&format!("= {} {}", &exp.0.to_string(), &cl.to_string()));
|
||||
s.push_str(&format!(" = {} {}", &exp.0.to_string(), &cl.to_string()));
|
||||
} else {
|
||||
s.push_str(&format!("= {}", &exp.0.to_string()));
|
||||
s.push_str(&format!(" = {}", &exp.0.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue