5 use ide_db::SymbolKind;
6 use syntax::SyntaxKind;
8 use crate::{completions::Completions, context::CompletionContext, CompletionItem};
10 pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
11 let add_completion = |item: &str| {
12 let mut completion = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), item);
13 completion.insert_text(format!(r#""{}""#, item));
14 acc.add(completion.build());
17 let previous = iter::successors(ctx.original_token.prev_token(), |t| {
18 (matches!(t.kind(), SyntaxKind::EQ) || t.kind().is_trivia())
19 .then(|| t.prev_token())
22 .find(|t| matches!(t.kind(), SyntaxKind::IDENT));
24 match previous.as_ref().map(|p| p.text()) {
25 Some("target_arch") => KNOWN_ARCH.iter().copied().for_each(add_completion),
26 Some("target_env") => KNOWN_ENV.iter().copied().for_each(add_completion),
27 Some("target_os") => KNOWN_OS.iter().copied().for_each(add_completion),
28 Some("target_vendor") => KNOWN_VENDOR.iter().copied().for_each(add_completion),
29 Some("target_endian") => ["little", "big"].into_iter().for_each(add_completion),
31 if let Some(krate) = ctx.krate {
32 krate.potential_cfg(ctx.db).get_cfg_values(&name).cloned().for_each(|s| {
33 let insert_text = format!(r#""{}""#, s);
35 CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), s);
36 item.insert_text(insert_text);
38 acc.add(item.build());
43 if let Some(krate) = ctx.krate {
44 krate.potential_cfg(ctx.db).get_cfg_keys().cloned().for_each(|s| {
45 let item = CompletionItem::new(SymbolKind::Attribute, ctx.source_range(), s);
46 acc.add(item.build());
53 const KNOWN_ARCH: [&str; 19] = [
75 const KNOWN_ENV: [&str; 7] = ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];
77 const KNOWN_OS: [&str; 20] = [
100 const KNOWN_VENDOR: [&str; 8] =
101 ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"];