Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-completion/src/completions/extern_crate.rs')
-rw-r--r--crates/ide-completion/src/completions/extern_crate.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/crates/ide-completion/src/completions/extern_crate.rs b/crates/ide-completion/src/completions/extern_crate.rs
new file mode 100644
index 0000000000..0d0e143f5f
--- /dev/null
+++ b/crates/ide-completion/src/completions/extern_crate.rs
@@ -0,0 +1,71 @@
+//! Completion for extern crates
+
+use hir::{HasAttrs, Name};
+use ide_db::SymbolKind;
+
+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.to_smol_str(),
+ );
+ 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_string(), 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_string(), completion_list);
+ }
+}