Skip to content

Commit d34cbe0

Browse files
committed
Use SyntaxEditor in extract_type_alias
1 parent 5bd2f42 commit d34cbe0

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_type_alias.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use either::Either;
22
use ide_db::syntax_helpers::node_ext::walk_ty;
33
use syntax::{
44
ast::{self, edit::IndentLevel, make, AstNode, HasGenericArgs, HasGenericParams, HasName},
5-
ted,
5+
syntax_editor,
66
};
77

88
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -43,9 +43,8 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4343
AssistId("extract_type_alias", AssistKind::RefactorExtract),
4444
"Extract type as type alias",
4545
target,
46-
|edit| {
47-
let node = edit.make_syntax_mut(node.clone());
48-
let target_ty = edit.make_mut(ty.clone());
46+
|builder| {
47+
let mut edit = builder.make_editor(node);
4948

5049
let mut known_generics = match item.generic_param_list() {
5150
Some(it) => it.generic_params().collect(),
@@ -67,25 +66,28 @@ pub(crate) fn extract_type_alias(acc: &mut Assists, ctx: &AssistContext<'_>) ->
6766
.map_or(String::new(), |it| it.to_generic_args().to_string());
6867
// FIXME: replace with a `ast::make` constructor
6968
let new_ty = make::ty(&format!("Type{ty_args}")).clone_for_update();
70-
ted::replace(target_ty.syntax(), new_ty.syntax());
69+
edit.replace(ty.syntax(), new_ty.syntax());
7170

7271
// Insert new alias
73-
let indent = IndentLevel::from_node(&node);
7472
let ty_alias = make::ty_alias("Type", generic_params, None, None, Some((ty, None)))
7573
.clone_for_update();
76-
ted::insert_all(
77-
ted::Position::before(node),
74+
75+
if let Some(cap) = ctx.config.snippet_cap {
76+
if let Some(name) = ty_alias.name() {
77+
edit.add_annotation(name.syntax(), builder.make_tabstop_before(cap));
78+
}
79+
}
80+
81+
let indent = IndentLevel::from_node(node);
82+
edit.insert_all(
83+
syntax_editor::Position::before(node),
7884
vec![
7985
ty_alias.syntax().clone().into(),
8086
make::tokens::whitespace(&format!("\n\n{indent}")).into(),
8187
],
8288
);
8389

84-
if let Some(cap) = ctx.config.snippet_cap {
85-
if let Some(name) = ty_alias.name() {
86-
edit.add_tabstop_before(cap, name);
87-
}
88-
}
90+
builder.add_file_edits(ctx.file_id(), edit);
8991
},
9092
)
9193
}

0 commit comments

Comments
 (0)