]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/attribute/cfg.rs
Merge #10650
[rust.git] / crates / ide_completion / src / completions / attribute / cfg.rs
1 //! Completion for cfg
2
3 use std::iter;
4
5 use syntax::SyntaxKind;
6
7 use crate::{
8     completions::Completions, context::CompletionContext, CompletionItem, CompletionItemKind,
9 };
10
11 pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
12     let add_completion = |item: &str| {
13         let mut completion =
14             CompletionItem::new(CompletionItemKind::Attribute, ctx.source_range(), item);
15         completion.insert_text(format!(r#""{}""#, item));
16         acc.add(completion.build());
17     };
18
19     let previous = iter::successors(ctx.original_token.prev_token(), |t| {
20         (matches!(t.kind(), SyntaxKind::EQ) || t.kind().is_trivia())
21             .then(|| t.prev_token())
22             .flatten()
23     })
24     .find(|t| matches!(t.kind(), SyntaxKind::IDENT));
25
26     match previous.as_ref().map(|p| p.text()) {
27         Some("target_arch") => KNOWN_ARCH.iter().copied().for_each(add_completion),
28         Some("target_env") => KNOWN_ENV.iter().copied().for_each(add_completion),
29         Some("target_os") => KNOWN_OS.iter().copied().for_each(add_completion),
30         Some("target_vendor") => KNOWN_VENDOR.iter().copied().for_each(add_completion),
31         Some("target_endian") => ["little", "big"].into_iter().for_each(add_completion),
32         Some(name) => {
33             if let Some(krate) = ctx.krate {
34                 krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| {
35                     let mut item = CompletionItem::new(
36                         CompletionItemKind::Attribute,
37                         ctx.source_range(),
38                         s.as_str(),
39                     );
40                     item.insert_text(format!(r#""{}""#, s));
41
42                     acc.add(item.build());
43                 })
44             };
45         }
46         None => {
47             if let Some(krate) = ctx.krate {
48                 krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| {
49                     let item = CompletionItem::new(
50                         CompletionItemKind::Attribute,
51                         ctx.source_range(),
52                         s.as_str(),
53                     );
54                     acc.add(item.build());
55                 })
56             }
57         }
58     };
59 }
60
61 const KNOWN_ARCH: [&str; 19] = [
62     "aarch64",
63     "arm",
64     "avr",
65     "hexagon",
66     "mips",
67     "mips64",
68     "msp430",
69     "nvptx64",
70     "powerpc",
71     "powerpc64",
72     "riscv32",
73     "riscv64",
74     "s390x",
75     "sparc",
76     "sparc64",
77     "wasm32",
78     "wasm64",
79     "x86",
80     "x86_64",
81 ];
82
83 const KNOWN_ENV: [&str; 7] = ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];
84
85 const KNOWN_OS: [&str; 20] = [
86     "cuda",
87     "dragonfly",
88     "emscripten",
89     "freebsd",
90     "fuchsia",
91     "haiku",
92     "hermit",
93     "illumos",
94     "l4re",
95     "linux",
96     "netbsd",
97     "none",
98     "openbsd",
99     "psp",
100     "redox",
101     "solaris",
102     "uefi",
103     "unknown",
104     "vxworks",
105     "windows",
106 ];
107
108 const KNOWN_VENDOR: [&str; 8] =
109     ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"];