1 use std::iter::successors;
3 use ra_syntax::{ast, AstNode, T};
5 use crate::{Assist, AssistCtx, AssistId};
7 // Assist: split_import
9 // Wraps the tail of import into braces.
12 // use std::<|>collections::HashMap;
16 // use std::{collections::HashMap};
18 pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> {
19 let colon_colon = ctx.find_token_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 {
29 let cursor = ctx.frange.range.start();
31 let target = colon_colon.text_range();
32 ctx.add_assist(AssistId("split_import"), "Split import", target, |edit| {
33 edit.replace_ast(use_tree, new_tree);
34 edit.set_cursor(cursor);
40 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
45 fn test_split_import() {
48 "use crate::<|>db::RootDatabase;",
49 "use crate::<|>{db::RootDatabase};",
54 fn split_import_works_with_trees() {
57 "use crate:<|>:db::{RootDatabase, FileSymbol}",
58 "use crate:<|>:{db::{RootDatabase, FileSymbol}}",
63 fn split_import_target() {
64 check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::");
69 check_assist_not_applicable(split_import, "use crate::<|>:::self;")