Skip to content

Commit

Permalink
Auto merge of rust-lang#15621 - kpreid:import, r=Veykril
Browse files Browse the repository at this point in the history
Give `unmerge_use` a label explaining what it will affect.

When I'm trying to clean up `use`s, I often feel uncertain about what exactly the effects of choosing an assist will be. This PR makes a small improvement to that by giving “Unmerge use” a label which names the root of the tree that it's going to move, when one exists.

There is no test because I didn't see, among the test helpers, a way to assert on the assist label (as opposed to filtering on it). However, I did test the change manually.

I looked into making a similar change to “Merge imports”, but that is considerably trickier.
  • Loading branch information
bors committed Sep 20, 2023
2 parents 4778255 + cac796a commit d6fef2c
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions crates/ide-assists/src/handlers/unmerge_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,25 @@ pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let old_parent_range = use_.syntax().parent()?.text_range();
let new_parent = use_.syntax().parent()?;

// If possible, explain what is going to be done.
let label = match tree.path().and_then(|path| path.first_segment()) {
Some(name) => format!("Unmerge use of `{name}`"),
None => "Unmerge use".into(),
};

let target = tree.syntax().text_range();
acc.add(
AssistId("unmerge_use", AssistKind::RefactorRewrite),
"Unmerge use",
target,
|builder| {
let new_use = make::use_(
use_.visibility(),
make::use_tree(
path,
tree.use_tree_list(),
tree.rename(),
tree.star_token().is_some(),
),
)
.clone_for_update();

tree.remove();
ted::insert(Position::after(use_.syntax()), new_use.syntax());

builder.replace(old_parent_range, new_parent.to_string());
},
)
acc.add(AssistId("unmerge_use", AssistKind::RefactorRewrite), label, target, |builder| {
let new_use = make::use_(
use_.visibility(),
make::use_tree(path, tree.use_tree_list(), tree.rename(), tree.star_token().is_some()),
)
.clone_for_update();

tree.remove();
ted::insert(Position::after(use_.syntax()), new_use.syntax());

builder.replace(old_parent_range, new_parent.to_string());
})
}

fn resolve_full_path(tree: &ast::UseTree) -> Option<ast::Path> {
Expand Down

0 comments on commit d6fef2c

Please sign in to comment.