aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/assists/split_import.rs
blob: 75f9e8dfbb1bce7f0b07dee8ee2ab856db8b60d2 (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
use ra_syntax::{
    TextUnit, AstNode, SyntaxKind::COLONCOLON,
    ast,
    algo::generate,
};

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

pub fn split_import(ctx: AssistCtx) -> Option<Assist> {
    let colon_colon = ctx
        .leaf_at_offset()
        .find(|leaf| leaf.kind() == COLONCOLON)?;
    let path = colon_colon.parent().and_then(ast::Path::cast)?;
    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 = top_path.syntax().range().end();

    ctx.build("split import", |edit| {
        edit.insert(l_curly, "{");
        edit.insert(r_curly, "}");
        edit.set_cursor(l_curly + TextUnit::of_str("{"));
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::assists::check_assist;

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