ide_completion/completions/
extern_crate.rs
1use hir::Name;
4use ide_db::{SymbolKind, documentation::HasDocs};
5use syntax::ToSmolStr;
6
7use crate::{CompletionItem, CompletionItemKind, context::CompletionContext};
8
9use super::Completions;
10
11pub(crate) fn complete_extern_crate(acc: &mut Completions, ctx: &CompletionContext<'_>) {
12 let imported_extern_crates: Vec<Name> = ctx.scope.extern_crate_decls().collect();
13
14 for (name, module) in ctx.scope.extern_crates() {
15 if imported_extern_crates.contains(&name) {
16 continue;
17 }
18
19 let mut item = CompletionItem::new(
20 CompletionItemKind::SymbolKind(SymbolKind::Module),
21 ctx.source_range(),
22 name.display_no_db(ctx.edition).to_smolstr(),
23 ctx.edition,
24 );
25 item.set_documentation(module.docs(ctx.db));
26
27 item.add_to(acc, ctx.db);
28 }
29}
30
31#[cfg(test)]
32mod test {
33 use crate::tests::completion_list_no_kw;
34
35 #[test]
36 fn can_complete_extern_crate() {
37 let case = r#"
38//- /lib.rs crate:other_crate_a
39// nothing here
40//- /other_crate_b.rs crate:other_crate_b
41pub mod good_mod{}
42//- /lib.rs crate:crate_c
43// nothing here
44//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a
45extern crate oth$0
46mod other_mod {}
47"#;
48
49 let completion_list = completion_list_no_kw(case);
50
51 assert_eq!("md other_crate_a\n".to_owned(), completion_list);
52 }
53
54 #[test]
55 fn will_not_complete_existing_import() {
56 let case = r#"
57//- /lib.rs crate:other_crate_a
58// nothing here
59//- /lib.rs crate:crate_c
60// nothing here
61//- /lib.rs crate:other_crate_b
62//
63//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a,other_crate_b
64extern crate other_crate_b;
65extern crate oth$0
66mod other_mod {}
67"#;
68
69 let completion_list = completion_list_no_kw(case);
70
71 assert_eq!("md other_crate_a\n".to_owned(), completion_list);
72 }
73}