Migrate extract_type_alias to mutable ast

This commit is contained in:
DropDemBits 2023-06-04 23:30:48 -04:00
parent e6e72bf9d5
commit dd5f05590e
No known key found for this signature in database
GPG key ID: 7FE02A6C1EDFA075
2 changed files with 30 additions and 23 deletions

View file

@ -1,6 +1,9 @@
use either::Either; use either::Either;
use ide_db::syntax_helpers::node_ext::walk_ty; 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}; use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -34,14 +37,16 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|| item.syntax(), || item.syntax(),
|impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax), |impl_| impl_.as_ref().either(AstNode::syntax, AstNode::syntax),
); );
let insert_pos = node.text_range().start();
let target = ty.syntax().text_range(); let target = ty.syntax().text_range();
acc.add( acc.add(
AssistId("extract_type_alias", AssistKind::RefactorExtract), AssistId("extract_type_alias", AssistKind::RefactorExtract),
"Extract type as type alias", "Extract type as type alias",
target, 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() { let mut known_generics = match item.generic_param_list() {
Some(it) => it.generic_params().collect(), Some(it) => it.generic_params().collect(),
None => Vec::new(), None => Vec::new(),
@ -56,27 +61,29 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let generic_params = 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 let ty_args = generic_params
.as_ref() .as_ref()
.map_or(String::new(), |it| it.to_generic_args().to_string()); .map_or(String::new(), |it| it.to_generic_args().to_string());
let replacement = format!("Type{ty_args}"); // FIXME: replace with a `ast::make` constructor
builder.replace(target, replacement); 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); // Insert new alias
let generic_params = generic_params.map_or(String::new(), |it| it.to_string()); let indent = IndentLevel::from_node(&node);
match ctx.config.snippet_cap { let ty_alias = make::ty_alias("Type", generic_params, None, None, Some((ty, None)))
Some(cap) => { .clone_for_update();
builder.insert_snippet( ted::insert_all(
cap, ted::Position::before(node),
insert_pos, vec![
format!("type $0Type{generic_params} = {ty};\n\n{indent}"), ty_alias.syntax().clone().into(),
); make::tokens::whitespace(&format!("\n\n{indent}")).into(),
} ],
None => { );
builder.insert(
insert_pos, if let Some(cap) = ctx.config.snippet_cap {
format!("type Type{generic_params} = {ty};\n\n{indent}"), if let Some(name) = ty_alias.name() {
); edit.add_tabstop_before(cap, name);
} }
} }
}, },

View file

@ -166,7 +166,7 @@ pub fn ty_alias(
assignment: Option<(ast::Type, Option<ast::WhereClause>)>, assignment: Option<(ast::Type, Option<ast::WhereClause>)>,
) -> ast::TypeAlias { ) -> ast::TypeAlias {
let mut s = String::new(); let mut s = String::new();
s.push_str(&format!("type {} ", ident)); s.push_str(&format!("type {}", ident));
if let Some(list) = generic_param_list { if let Some(list) = generic_param_list {
s.push_str(&list.to_string()); s.push_str(&list.to_string());
@ -182,9 +182,9 @@ pub fn ty_alias(
if let Some(exp) = assignment { if let Some(exp) = assignment {
if let Some(cl) = exp.1 { 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 { } else {
s.push_str(&format!("= {}", &exp.0.to_string())); s.push_str(&format!(" = {}", &exp.0.to_string()));
} }
} }