mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +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 either::Either;
|
||||||
use ide_db::imports::{
|
use ide_db::imports::{
|
||||||
insert_use::{ImportGranularity, InsertUseConfig},
|
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::{
|
use syntax::{
|
||||||
AstNode, SyntaxElement, SyntaxNode,
|
AstNode, SyntaxElement, SyntaxNode,
|
||||||
algo::neighbor,
|
algo::neighbor,
|
||||||
ast::{self, edit_in_place::Removable},
|
ast::{self, syntax_factory::SyntaxFactory},
|
||||||
match_ast, ted,
|
match_ast,
|
||||||
|
syntax_editor::Removable,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -69,49 +69,32 @@ pub(crate) fn merge_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
|
|||||||
(selection_range, edits?)
|
(selection_range, edits?)
|
||||||
};
|
};
|
||||||
|
|
||||||
acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| {
|
let parent_node = match ctx.covering_element() {
|
||||||
let edits_mut: Vec<Edit> = edits
|
SyntaxElement::Node(n) => n,
|
||||||
.into_iter()
|
SyntaxElement::Token(t) => t.parent()?,
|
||||||
.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);
|
|
||||||
|
|
||||||
// If there's a selection and we're replacing a use tree in a tree list,
|
acc.add(AssistId::refactor_rewrite("merge_imports"), "Merge imports", target, |builder| {
|
||||||
// normalize the parent use tree if it only contains the merged subtree.
|
let make = SyntaxFactory::with_mappings();
|
||||||
if !ctx.has_empty_selection() {
|
let mut editor = builder.make_editor(&parent_node);
|
||||||
let normalized_use_tree = ast::UseTree::cast(new)
|
|
||||||
.as_ref()
|
for edit in edits {
|
||||||
.and_then(ast::UseTree::parent_use_tree_list)
|
match edit {
|
||||||
.and_then(|use_tree_list| {
|
Remove(it) => {
|
||||||
if use_tree_list.use_trees().collect_tuple::<(_,)>().is_some() {
|
let node = it.as_ref();
|
||||||
Some(use_tree_list.parent_use_tree())
|
if let Some(left) = node.left() {
|
||||||
} else {
|
left.remove(&mut editor);
|
||||||
None
|
} else if let Some(right) = node.right() {
|
||||||
}
|
right.remove(&mut editor);
|
||||||
})
|
|
||||||
.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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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!(merge_with_selected_use_tree_neighbors);
|
||||||
cov_mark::check!(replace_parent_with_normalized_use_tree);
|
|
||||||
check_assist(
|
check_assist(
|
||||||
merge_imports,
|
merge_imports,
|
||||||
r"use std::$0{fmt::Display, fmt::Debug}$0;",
|
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