Skip to content

Commit 236168f

Browse files
committed
feat: migrate merge_import to use SyntaxFactory
Signed-off-by: Tarek <[email protected]>
1 parent b51f9bc commit 236168f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

crates/ide-assists/src/handlers/merge_imports.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use ide_db::imports::{
66
use itertools::Itertools;
77
use syntax::{
88
algo::neighbor,
9-
ast::{self, edit_in_place::Removable},
10-
match_ast, ted, AstNode, SyntaxElement, SyntaxNode,
9+
ast::{self, edit_in_place::Removable, syntax_factory::SyntaxFactory},
10+
match_ast, AstNode, SyntaxElement, SyntaxNode,
1111
};
1212

1313
use crate::{
@@ -68,11 +68,19 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
6868
(selection_range, edits?)
6969
};
7070

71+
// FIXME: Is this the best to get a `SyntaxNode` object? We need one for `SourceChangeBuilder::make_editor`.
72+
let parent_node = match ctx.covering_element() {
73+
SyntaxElement::Node(n) => n,
74+
SyntaxElement::Token(t) => t.parent()?,
75+
};
76+
let make = SyntaxFactory::new();
77+
7178
acc.add(
7279
AssistId("merge_imports", AssistKind::RefactorRewrite),
7380
"Merge imports",
7481
target,
7582
|builder| {
83+
let mut editor = builder.make_editor(&parent_node);
7684
let edits_mut: Vec<Edit> = edits
7785
.into_iter()
7886
.map(|it| match it {
@@ -85,7 +93,7 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
8593
match edit {
8694
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
8795
Replace(old, new) => {
88-
ted::replace(old, &new);
96+
editor.replace(old, &new);
8997

9098
// If there's a selection and we're replacing a use tree in a tree list,
9199
// normalize the parent use tree if it only contains the merged subtree.
@@ -109,12 +117,14 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
109117
});
110118
if let Some((old_tree, new_tree)) = normalized_use_tree {
111119
cov_mark::hit!(replace_parent_with_normalized_use_tree);
112-
ted::replace(old_tree.syntax(), new_tree.syntax());
120+
editor.replace(old_tree.syntax(), new_tree.syntax());
113121
}
114122
}
115123
}
116124
}
117125
}
126+
editor.add_mappings(make.finish_with_mappings());
127+
builder.add_file_edits(ctx.file_id(), editor);
118128
},
119129
)
120130
}

0 commit comments

Comments
 (0)