|
| 1 | +use ide_db::{FxHashSet, SymbolKind}; |
| 2 | +use syntax::{ |
| 3 | + ast::{self, NameRef}, |
| 4 | + AstNode, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::{context::CompletionContext, CompletionItem, CompletionItemKind}; |
| 8 | + |
| 9 | +use super::Completions; |
| 10 | + |
| 11 | +pub(crate) fn complete_extern_crate( |
| 12 | + acc: &mut Completions, |
| 13 | + ctx: &CompletionContext<'_>, |
| 14 | + name_ref: Option<&NameRef>, |
| 15 | +) { |
| 16 | + let imported_extern_crates: FxHashSet<String> = ctx |
| 17 | + .token |
| 18 | + .parent_ancestors() |
| 19 | + .find_map(ast::SourceFile::cast) |
| 20 | + .map(|src_file| { |
| 21 | + src_file |
| 22 | + .syntax() |
| 23 | + .children() |
| 24 | + .into_iter() |
| 25 | + .filter_map(ast::ExternCrate::cast) |
| 26 | + .filter_map(|node| node.name_ref()) |
| 27 | + .map(|name| name.to_string()) |
| 28 | + .collect() |
| 29 | + }) |
| 30 | + .unwrap_or_default(); |
| 31 | + |
| 32 | + let current_txt = |
| 33 | + name_ref.as_ref().map(|name_ref| name_ref.to_string()).unwrap_or(String::new()); |
| 34 | + |
| 35 | + for name in ctx.scope.extern_crates() { |
| 36 | + if (!current_txt.is_empty() && !name.to_smol_str().starts_with(¤t_txt)) |
| 37 | + || imported_extern_crates.contains(name.to_smol_str().as_str()) |
| 38 | + { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + CompletionItem::new( |
| 43 | + CompletionItemKind::SymbolKind(SymbolKind::Module), |
| 44 | + ctx.source_range(), |
| 45 | + name.to_smol_str(), |
| 46 | + ) |
| 47 | + .add_to(acc, ctx.db); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod test { |
| 53 | + use crate::tests::completion_list_no_kw; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn can_complete_extern_crate() { |
| 57 | + let case = r#" |
| 58 | +//- /lib.rs crate:other_crate_a |
| 59 | +// nothing here |
| 60 | +//- /other_crate_b.rs crate:other_crate_b |
| 61 | +pub mod good_mod{} |
| 62 | +//- /lib.rs crate:crate_c |
| 63 | +// nothing here |
| 64 | +//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a,crate_c |
| 65 | +extern crate crate_c; |
| 66 | +extern crate oth$0 |
| 67 | +mod other_mod {} |
| 68 | +"#; |
| 69 | + |
| 70 | + let completion_list = completion_list_no_kw(case); |
| 71 | + |
| 72 | + assert_eq!("md other_crate_a\n".to_string(), completion_list); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn will_not_complete_existing_import() { |
| 77 | + let case = r#" |
| 78 | +//- /lib.rs crate:other_crate_a |
| 79 | +// nothing here |
| 80 | +//- /lib.rs crate:crate_c |
| 81 | +// nothing here |
| 82 | +//- /lib.rs crate:other_crate_b |
| 83 | +// |
| 84 | +//- /lib.rs crate:lib deps:other_crate_a,other_crate_b,crate_c extern-prelude:other_crate_a,other_crate_b,crate_c |
| 85 | +extern crate other_crate_b; |
| 86 | +extern crate oth$0 |
| 87 | +mod other_mod {} |
| 88 | +"#; |
| 89 | + |
| 90 | + let completion_list = completion_list_no_kw(case); |
| 91 | + |
| 92 | + assert_eq!("md other_crate_a\n".to_string(), completion_list); |
| 93 | + } |
| 94 | +} |
0 commit comments