1 use hir::db::HirDatabase;
3 TextUnit, AstNode, SyntaxKind::COLONCOLON,
8 use crate::{AssistCtx, Assist};
10 pub(crate) fn split_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let colon_colon = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
12 let path = colon_colon.parent().and_then(ast::Path::cast)?;
13 let top_path = generate(Some(path), |it| it.parent_path()).last()?;
15 let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast);
16 if use_tree.is_none() {
20 let l_curly = colon_colon.range().end();
21 let r_curly = match top_path.syntax().parent().and_then(ast::UseTree::cast) {
22 Some(tree) => tree.syntax().range().end(),
23 None => top_path.syntax().range().end(),
26 ctx.build("split import", |edit| {
27 edit.target(colon_colon.range());
28 edit.insert(l_curly, "{");
29 edit.insert(r_curly, "}");
30 edit.set_cursor(l_curly + TextUnit::of_str("{"));
37 use crate::helpers::{check_assist, check_assist_target};
40 fn test_split_import() {
43 "use crate::<|>db::RootDatabase;",
44 "use crate::{<|>db::RootDatabase};",
49 fn split_import_works_with_trees() {
52 "use algo:<|>:visitor::{Visitor, visit}",
53 "use algo::{<|>visitor::{Visitor, visit}}",
58 fn split_import_target() {
59 check_assist_target(split_import, "use algo::<|>visitor::{Visitor, visit}", "::");