Skip to content

Commit d25c175

Browse files
snprajwaltareknaser
andcommitted
refactor: migrate merge_imports to syntax editor
Co-authored-by: Tarek <[email protected]> Signed-off-by: Prajwal S N <[email protected]>
1 parent c6ae7b3 commit d25c175

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

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

+25-43
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use either::Either;
22
use ide_db::imports::{
33
insert_use::{ImportGranularity, InsertUseConfig},
4-
merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees, try_normalize_use_tree},
4+
merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees},
55
};
6-
use itertools::Itertools;
76
use syntax::{
87
AstNode, SyntaxElement, SyntaxNode,
98
algo::neighbor,
10-
ast::{self, edit_in_place::Removable},
11-
match_ast, ted,
9+
ast::{self, syntax_factory::SyntaxFactory},
10+
match_ast,
11+
syntax_editor::Removable,
1212
};
1313

1414
use crate::{
@@ -69,49 +69,32 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
6969
(selection_range, edits?)
7070
};
7171

72+
let parent_node = match ctx.covering_element() {
73+
SyntaxElement::Node(n) => n,
74+
SyntaxElement::Token(t) => t.parent()?,
75+
};
76+
7277
acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| {
73-
let edits_mut: Vec<Edit> = edits
74-
.into_iter()
75-
.map(|it| match it {
76-
Remove(Either::Left(it)) => Remove(Either::Left(builder.make_mut(it))),
77-
Remove(Either::Right(it)) => Remove(Either::Right(builder.make_mut(it))),
78-
Replace(old, new) => Replace(builder.make_syntax_mut(old), new),
79-
})
80-
.collect();
81-
for edit in edits_mut {
78+
let make = SyntaxFactory::with_mappings();
79+
let mut editor = builder.make_editor(&parent_node);
80+
81+
for edit in edits {
8282
match edit {
83-
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
84-
Replace(old, new) => {
85-
ted::replace(old, &new);
86-
87-
// If there's a selection and we're replacing a use tree in a tree list,
88-
// normalize the parent use tree if it only contains the merged subtree.
89-
if !ctx.has_empty_selection() {
90-
let normalized_use_tree = ast::UseTree::cast(new)
91-
.as_ref()
92-
.and_then(ast::UseTree::parent_use_tree_list)
93-
.and_then(|use_tree_list| {
94-
if use_tree_list.use_trees().collect_tuple::<(_,)>().is_some() {
95-
Some(use_tree_list.parent_use_tree())
96-
} else {
97-
None
98-
}
99-
})
100-
.and_then(|target_tree| {
101-
try_normalize_use_tree(
102-
&target_tree,
103-
ctx.config.insert_use.granularity.into(),
104-
)
105-
.map(|top_use_tree_flat| (target_tree, top_use_tree_flat))
106-
});
107-
if let Some((old_tree, new_tree)) = normalized_use_tree {
108-
cov_mark::hit!(replace_parent_with_normalized_use_tree);
109-
ted::replace(old_tree.syntax(), new_tree.syntax());
110-
}
83+
Remove(it) => {
84+
let node = it.as_ref();
85+
if let Some(left) = node.left() {
86+
left.remove(&mut editor);
87+
} else if let Some(right) = node.right() {
88+
right.remove(&mut editor);
11189
}
11290
}
91+
Replace(old, new) => {
92+
editor.replace(old, &new);
93+
}
11394
}
11495
}
96+
editor.add_mappings(make.finish_with_mappings());
97+
builder.add_file_edits(ctx.vfs_file_id(), editor);
11598
})
11699
}
117100

@@ -723,11 +706,10 @@ use std::{
723706
);
724707

725708
cov_mark::check!(merge_with_selected_use_tree_neighbors);
726-
cov_mark::check!(replace_parent_with_normalized_use_tree);
727709
check_assist(
728710
merge_imports,
729711
r"use std::$0{fmt::Display, fmt::Debug}$0;",
730-
r"use std::fmt::{Debug, Display};",
712+
r"use std::{fmt::{Debug, Display}};",
731713
);
732714
}
733715

0 commit comments

Comments
 (0)