mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
refactor: migrate merge_imports
to syntax editor
Co-authored-by: Tarek <tareknaser360@gmail.com> Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
c6ae7b3541
commit
d25c175b3e
@ -1,14 +1,14 @@
|
||||
use either::Either;
|
||||
use ide_db::imports::{
|
||||
insert_use::{ImportGranularity, InsertUseConfig},
|
||||
merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees, try_normalize_use_tree},
|
||||
merge_imports::{MergeBehavior, try_merge_imports, try_merge_trees},
|
||||
};
|
||||
use itertools::Itertools;
|
||||
use syntax::{
|
||||
AstNode, SyntaxElement, SyntaxNode,
|
||||
algo::neighbor,
|
||||
ast::{self, edit_in_place::Removable},
|
||||
match_ast, ted,
|
||||
ast::{self, syntax_factory::SyntaxFactory},
|
||||
match_ast,
|
||||
syntax_editor::Removable,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@ -69,49 +69,32 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
|
||||
(selection_range, edits?)
|
||||
};
|
||||
|
||||
acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| {
|
||||
let edits_mut: Vec<Edit> = edits
|
||||
.into_iter()
|
||||
.map(|it| match it {
|
||||
Remove(Either::Left(it)) => Remove(Either::Left(builder.make_mut(it))),
|
||||
Remove(Either::Right(it)) => Remove(Either::Right(builder.make_mut(it))),
|
||||
Replace(old, new) => Replace(builder.make_syntax_mut(old), new),
|
||||
})
|
||||
.collect();
|
||||
for edit in edits_mut {
|
||||
match edit {
|
||||
Remove(it) => it.as_ref().either(Removable::remove, Removable::remove),
|
||||
Replace(old, new) => {
|
||||
ted::replace(old, &new);
|
||||
let parent_node = match ctx.covering_element() {
|
||||
SyntaxElement::Node(n) => n,
|
||||
SyntaxElement::Token(t) => t.parent()?,
|
||||
};
|
||||
|
||||
// If there's a selection and we're replacing a use tree in a tree list,
|
||||
// normalize the parent use tree if it only contains the merged subtree.
|
||||
if !ctx.has_empty_selection() {
|
||||
let normalized_use_tree = ast::UseTree::cast(new)
|
||||
.as_ref()
|
||||
.and_then(ast::UseTree::parent_use_tree_list)
|
||||
.and_then(|use_tree_list| {
|
||||
if use_tree_list.use_trees().collect_tuple::<(_,)>().is_some() {
|
||||
Some(use_tree_list.parent_use_tree())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.and_then(|target_tree| {
|
||||
try_normalize_use_tree(
|
||||
&target_tree,
|
||||
ctx.config.insert_use.granularity.into(),
|
||||
)
|
||||
.map(|top_use_tree_flat| (target_tree, top_use_tree_flat))
|
||||
});
|
||||
if let Some((old_tree, new_tree)) = normalized_use_tree {
|
||||
cov_mark::hit!(replace_parent_with_normalized_use_tree);
|
||||
ted::replace(old_tree.syntax(), new_tree.syntax());
|
||||
}
|
||||
acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| {
|
||||
let make = SyntaxFactory::with_mappings();
|
||||
let mut editor = builder.make_editor(&parent_node);
|
||||
|
||||
for edit in edits {
|
||||
match edit {
|
||||
Remove(it) => {
|
||||
let node = it.as_ref();
|
||||
if let Some(left) = node.left() {
|
||||
left.remove(&mut editor);
|
||||
} else if let Some(right) = node.right() {
|
||||
right.remove(&mut editor);
|
||||
}
|
||||
}
|
||||
Replace(old, new) => {
|
||||
editor.replace(old, &new);
|
||||
}
|
||||
}
|
||||
}
|
||||
editor.add_mappings(make.finish_with_mappings());
|
||||
builder.add_file_edits(ctx.vfs_file_id(), editor);
|
||||
})
|
||||
}
|
||||
|
||||
@ -723,11 +706,10 @@ use std::{
|
||||
);
|
||||
|
||||
cov_mark::check!(merge_with_selected_use_tree_neighbors);
|
||||
cov_mark::check!(replace_parent_with_normalized_use_tree);
|
||||
check_assist(
|
||||
merge_imports,
|
||||
r"use std::$0{fmt::Display, fmt::Debug}$0;",
|
||||
r"use std::fmt::{Debug, Display};",
|
||||
r"use std::{fmt::{Debug, Display}};",
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user