2 ast::{self, VisibilityOwner},
8 assist_context::{AssistContext, Assists},
12 // Assist: unmerge_use
14 // Extracts single use item from use list.
17 // use std::fmt::{Debug, Display$0};
21 // use std::fmt::{Debug};
22 // use std::fmt::Display;
24 pub(crate) fn unmerge_use(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
25 let tree: ast::UseTree = ctx.find_node_at_offset::<ast::UseTree>()?.clone_for_update();
27 let tree_list = tree.syntax().parent().and_then(ast::UseTreeList::cast)?;
28 if tree_list.use_trees().count() < 2 {
29 cov_mark::hit!(skip_single_use_item);
33 let use_: ast::Use = tree_list.syntax().ancestors().find_map(ast::Use::cast)?;
34 let path = resolve_full_path(&tree)?;
36 let old_parent_range = use_.syntax().parent()?.text_range();
37 let new_parent = use_.syntax().parent()?;
39 let target = tree.syntax().text_range();
41 AssistId("unmerge_use", AssistKind::RefactorRewrite),
45 let new_use = ast::make::use_(
51 tree.star_token().is_some(),
57 ted::insert(Position::after(use_.syntax()), new_use.syntax());
59 builder.replace(old_parent_range, new_parent.to_string());
64 fn resolve_full_path(tree: &ast::UseTree) -> Option<ast::Path> {
68 .take_while(|n| n.kind() != SyntaxKind::USE_KW)
69 .filter_map(ast::UseTree::cast)
70 .filter_map(|t| t.path());
72 let mut final_path = paths.next()?;
74 final_path = ast::make::path_concat(path, final_path)
81 use crate::tests::{check_assist, check_assist_not_applicable};
86 fn skip_single_use_item() {
87 cov_mark::check!(skip_single_use_item);
88 check_assist_not_applicable(
91 use std::fmt::Debug$0;
94 check_assist_not_applicable(
97 use std::fmt::{Debug$0};
100 check_assist_not_applicable(
103 use std::fmt::Debug as Dbg$0;
109 fn skip_single_glob_import() {
110 check_assist_not_applicable(
119 fn unmerge_use_item() {
123 use std::fmt::{Debug, Display$0};
126 use std::fmt::{Debug};
127 use std::fmt::Display;
134 use std::fmt::{Debug, format$0, Display};
137 use std::fmt::{Debug, Display};
138 use std::fmt::format;
144 fn unmerge_glob_import() {
148 use std::fmt::{*$0, Display};
151 use std::fmt::{Display};
158 fn unmerge_renamed_use_item() {
162 use std::fmt::{Debug, Display as Disp$0};
165 use std::fmt::{Debug};
166 use std::fmt::Display as Disp;
172 fn unmerge_indented_use_item() {
177 use std::fmt::{Debug, Display$0 as Disp, format};
182 use std::fmt::{Debug, format};
183 use std::fmt::Display as Disp;
190 fn unmerge_nested_use_item() {
194 use foo::bar::{baz::{qux$0, foobar}, barbaz};
197 use foo::bar::{baz::{foobar}, barbaz};
198 use foo::bar::baz::qux;
204 use foo::bar::{baz$0::{qux, foobar}, barbaz};
207 use foo::bar::{barbaz};
208 use foo::bar::baz::{qux, foobar};
214 fn unmerge_use_item_with_visibility() {
218 pub use std::fmt::{Debug, Display$0};
221 pub use std::fmt::{Debug};
222 pub use std::fmt::Display;