aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/split_import.rs
blob: 4bf1852db5774b28143fc2498238d15cf399c299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use hir::db::HirDatabase;
use ra_syntax::{
    TextUnit, AstNode, SyntaxKind::COLONCOLON,
    ast,
    algo::generate,
};

use crate::{AssistCtx, Assist, AssistId};

pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
    let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?;
    let path = ast::Path::cast(colon_colon.parent())?;
    let top_path = generate(Some(path), |it| it.parent_path()).last()?;

    let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast);
    if use_tree.is_none() {
        return None;
    }

    let l_curly = colon_colon.range().end();
    let r_curly = match top_path.syntax().parent().and_then(ast::UseTree::cast) {
        Some(tree) => tree.syntax().range().end(),
        None => top_path.syntax().range().end(),
    };

    ctx.add_action(AssistId("split_import"), "split import", |edit| {
        edit.target(colon_colon.range());
        edit.insert(l_curly, "{");
        edit.insert(r_curly, "}");
        edit.set_cursor(l_curly + TextUnit::of_str("{"));
    });

    ctx.build()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::helpers::{check_assist, check_assist_target};

    #[test]
    fn test_split_import() {
        check_assist(
            split_import,
            "use crate::<|>db::RootDatabase;",
            "use crate::{<|>db::RootDatabase};",
        )
    }

    #[test]
    fn split_import_works_with_trees() {
        check_assist(
            split_import,
            "use algo:<|>:visitor::{Visitor, visit}",
            "use algo::{<|>visitor::{Visitor, visit}}",
        )
    }

    #[test]
    fn split_import_target() {
        check_assist_target(split_import, "use algo::<|>visitor::{Visitor, visit}", "::");
    }
}