Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/remove_unused_imports.rs')
-rw-r--r--crates/ide-assists/src/handlers/remove_unused_imports.rs27
1 files changed, 16 insertions, 11 deletions
diff --git a/crates/ide-assists/src/handlers/remove_unused_imports.rs b/crates/ide-assists/src/handlers/remove_unused_imports.rs
index c38bdfdccf..2958acc478 100644
--- a/crates/ide-assists/src/handlers/remove_unused_imports.rs
+++ b/crates/ide-assists/src/handlers/remove_unused_imports.rs
@@ -33,7 +33,7 @@ use crate::{AssistContext, AssistId, Assists};
// mod foo {
// }
// ```
-pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
+pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> Option<()> {
// First, grab the uses that intersect with the current selection.
let selected_el = match ctx.covering_element() {
syntax::NodeOrToken::Node(n) => n,
@@ -111,19 +111,24 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
is_path_per_ns_unused_in_scope(ctx, &u, scope, &res).then_some(u)
}
})
- .peekable();
+ .collect::<Vec<_>>();
- // Peek so we terminate early if an unused use is found. Only do the rest of the work if the user selects the assist.
- if unused.peek().is_some() {
+ // Terminate early unless an unused use is found. Only do the rest of the work if the user selects the assist.
+ if !unused.is_empty() {
acc.add(
AssistId::quick_fix("remove_unused_imports"),
"Remove all unused imports",
selected_el.text_range(),
|builder| {
- let unused: Vec<ast::UseTree> = unused.map(|x| builder.make_mut(x)).collect();
- for node in unused {
- node.remove_recursive();
+ let editor = builder.make_editor(&selected_el);
+ unused.sort_by_key(|use_tree| use_tree.syntax().text_range().start());
+ for node in &unused {
+ editor.delete(node.syntax());
}
+ for node in unused.iter().cloned() {
+ node.remove_recursive(&editor);
+ }
+ builder.add_file_edits(ctx.vfs_file_id(), editor);
},
)
} else {
@@ -132,7 +137,7 @@ pub(crate) fn remove_unused_imports(acc: &mut Assists, ctx: &AssistContext<'_>)
}
fn is_path_per_ns_unused_in_scope(
- ctx: &AssistContext<'_>,
+ ctx: &AssistContext<'_, '_>,
u: &ast::UseTree,
scope: &mut Vec<SearchScope>,
path: &PathResolutionPerNs,
@@ -151,7 +156,7 @@ fn is_path_per_ns_unused_in_scope(
}
fn is_path_unused_in_scope(
- ctx: &AssistContext<'_>,
+ ctx: &AssistContext<'_, '_>,
u: &ast::UseTree,
scope: &mut Vec<SearchScope>,
path: &[Option<PathResolution>],
@@ -167,7 +172,7 @@ fn is_path_unused_in_scope(
}
fn is_trait_unused_in_scope(
- ctx: &AssistContext<'_>,
+ ctx: &AssistContext<'_, '_>,
u: &ast::UseTree,
scope: &mut Vec<SearchScope>,
t: &hir::Trait,
@@ -178,7 +183,7 @@ fn is_trait_unused_in_scope(
}
fn used_once_in_scope(
- ctx: &AssistContext<'_>,
+ ctx: &AssistContext<'_, '_>,
def: Definition,
rename: Option<Rename>,
scopes: &Vec<SearchScope>,