ide_completion/completions/
extern_crate.rs

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
64
65
66
67
68
69
70
71
72
73
//! Completion for extern crates

use hir::Name;
use ide_db::{documentation::HasDocs, SymbolKind};
use syntax::ToSmolStr;

use crate::{context::CompletionContext, CompletionItem, CompletionItemKind};

use super::Completions;

pub(crate) fn complete_extern_crate(acc: &mut Completions, ctx: &CompletionContext<'_>) {
    let imported_extern_crates: Vec<Name> = ctx.scope.extern_crate_decls().collect();

    for (name, module) in ctx.scope.extern_crates() {
        if imported_extern_crates.contains(&name) {
            continue;
        }

        let mut item = CompletionItem::new(
            CompletionItemKind::SymbolKind(SymbolKind::Module),
            ctx.source_range(),
            name.display_no_db(ctx.edition).to_smolstr(),
            ctx.edition,
        );
        item.set_documentation(module.docs(ctx.db));

        item.add_to(acc, ctx.db);
    }
}

#[cfg(test)]
mod test {
    use crate::tests::completion_list_no_kw;

    #[test]
    fn can_complete_extern_crate() {
        let case = r#"
//- /lib.rs crate:other_crate_a
// nothing here
//- /other_crate_b.rs crate:other_crate_b
pub mod good_mod{}
//- /lib.rs crate:crate_c
// nothing here
//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a
extern crate oth$0
mod other_mod {}
"#;

        let completion_list = completion_list_no_kw(case);

        assert_eq!("md other_crate_a\n".to_owned(), completion_list);
    }

    #[test]
    fn will_not_complete_existing_import() {
        let case = r#"
//- /lib.rs crate:other_crate_a
// nothing here
//- /lib.rs crate:crate_c
// nothing here
//- /lib.rs crate:other_crate_b
//
//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a,other_crate_b
extern crate other_crate_b;
extern crate oth$0
mod other_mod {}
"#;

        let completion_list = completion_list_no_kw(case);

        assert_eq!("md other_crate_a\n".to_owned(), completion_list);
    }
}