Skip to content

Commit

Permalink
Migrate extract_type_alias to mutable ast
Browse files Browse the repository at this point in the history
  • Loading branch information
DropDemBits committed Jun 5, 2023
1 parent e6e72bf commit dd5f055
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
49 changes: 28 additions & 21 deletions crates/ide-assists/src/handlers/extract_type_alias.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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(),
Expand All @@ -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);

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}"),
);
// 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());

// 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);
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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()));
}
}

Expand Down

0 comments on commit dd5f055

Please sign in to comment.