Auto merge of #14723 - obsgolem:master, r=Veykril

Added remove unused imports assist

This resolves the most important part of #5131. I needed to make a couple of cosmetic changes to the search infrastructure to do this.

A few open questions:
* Should imports that don't resolve to anything be considered unused? I figured probably not, but it would be a trivial change to make if we want it.
* Is there a cleaner way to make the edits to the use list?
* Is there a cleaner way to get the list of uses that intersect the current selection?
* Is the performance acceptable? When testing this on itself, it takes a good couple seconds to perform the assist.
* Is there a way to hide the rustc diagnostics that overlap with this functionality?
This commit is contained in:
bors 2023-08-01 09:50:16 +00:00
commit 62dcf39ef0
16 changed files with 822 additions and 28 deletions

View file

@ -380,6 +380,26 @@ impl Removable for ast::UseTree {
}
impl ast::UseTree {
/// Deletes the usetree node represented by the input. Recursively removes parents, including use nodes that become empty.
pub fn remove_recursive(self) {
let parent = self.syntax().parent();
self.remove();
if let Some(u) = parent.clone().and_then(ast::Use::cast) {
if u.use_tree().is_none() {
u.remove();
}
} else if let Some(u) = parent.and_then(ast::UseTreeList::cast) {
if u.use_trees().next().is_none() {
let parent = u.syntax().parent().and_then(ast::UseTree::cast);
if let Some(u) = parent {
u.remove_recursive();
}
}
}
}
pub fn get_or_create_use_tree_list(&self) -> ast::UseTreeList {
match self.use_tree_list() {
Some(it) => it,
@ -487,6 +507,22 @@ impl Removable for ast::Use {
}
}
}
let prev_ws = self
.syntax()
.prev_sibling_or_token()
.and_then(|it| it.into_token())
.and_then(ast::Whitespace::cast);
if let Some(prev_ws) = prev_ws {
let ws_text = prev_ws.syntax().text();
let prev_newline = ws_text.rfind('\n').map(|x| x + 1).unwrap_or(0);
let rest = &ws_text[0..prev_newline];
if rest.is_empty() {
ted::remove(prev_ws.syntax());
} else {
ted::replace(prev_ws.syntax(), make::tokens::whitespace(rest));
}
}
ted::remove(self.syntax());
}
}