1 use std::iter::successors;
3 use syntax::{ast, AstNode, T};
5 use crate::{AssistContext, AssistId, AssistKind, Assists};
7 // Assist: split_import
9 // Wraps the tail of import into braces.
12 // use std::$0collections::HashMap;
16 // use std::{collections::HashMap};
18 pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
19 let colon_colon = ctx.find_token_syntax_at_offset(T![::])?;
20 let path = ast::Path::cast(colon_colon.parent())?.qualifier()?;
21 let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;
23 let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?;
25 let new_tree = use_tree.split_prefix(&path);
26 if new_tree == use_tree {
30 let target = colon_colon.text_range();
31 acc.add(AssistId("split_import", AssistKind::RefactorRewrite), "Split import", target, |edit| {
32 edit.replace_ast(use_tree, new_tree);
38 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
43 fn test_split_import() {
46 "use crate::$0db::RootDatabase;",
47 "use crate::{db::RootDatabase};",
52 fn split_import_works_with_trees() {
55 "use crate:$0:db::{RootDatabase, FileSymbol}",
56 "use crate::{db::{RootDatabase, FileSymbol}}",
61 fn split_import_target() {
62 check_assist_target(split_import, "use crate::$0db::{RootDatabase, FileSymbol}", "::");
67 check_assist_not_applicable(split_import, "use crate::$0:::self;")
72 check_assist_not_applicable(