aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_completion')
-rw-r--r--crates/ide_completion/src/completions/attribute.rs15
-rw-r--r--crates/ide_completion/src/completions/attribute/cfg.rs112
2 files changed, 127 insertions, 0 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index 78fc30e16..cc4f4b2af 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -15,6 +15,7 @@ use crate::{
15 Completions, 15 Completions,
16}; 16};
17 17
18mod cfg;
18mod derive; 19mod derive;
19mod lint; 20mod lint;
20mod repr; 21mod repr;
@@ -30,6 +31,9 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
30 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS); 31 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
31 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS); 32 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
32 } 33 }
34 "cfg" => {
35 cfg::complete_cfg(acc, ctx);
36 }
33 _ => (), 37 _ => (),
34 }, 38 },
35 (None, Some(_)) => (), 39 (None, Some(_)) => (),
@@ -852,4 +856,15 @@ mod tests {
852 "#]], 856 "#]],
853 ); 857 );
854 } 858 }
859
860 #[test]
861 fn test_cfg() {
862 check(
863 r#"#[cfg(target_endian = $0"#,
864 expect![[r#"
865 at little
866 at big
867"#]],
868 );
869 }
855} 870}
diff --git a/crates/ide_completion/src/completions/attribute/cfg.rs b/crates/ide_completion/src/completions/attribute/cfg.rs
new file mode 100644
index 000000000..847e6529a
--- /dev/null
+++ b/crates/ide_completion/src/completions/attribute/cfg.rs
@@ -0,0 +1,112 @@
1//! Completion for cfg
2
3use std::iter;
4
5use syntax::SyntaxKind;
6
7use crate::{
8 completions::Completions, context::CompletionContext, item::CompletionKind, CompletionItem,
9 CompletionItemKind,
10};
11
12pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
13 let add_completion = |item: &&str| {
14 let mut completion =
15 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), *item);
16 completion.insert_text(format!(r#""{}""#, item));
17 completion.kind(CompletionItemKind::Attribute);
18 acc.add(completion.build());
19 };
20
21 let previous = iter::successors(ctx.original_token.prev_token(), |t| {
22 (matches!(t.kind(), SyntaxKind::EQ) || t.kind().is_trivia())
23 .then(|| t.prev_token())
24 .flatten()
25 })
26 .find(|t| matches!(t.kind(), SyntaxKind::IDENT));
27
28 match previous.as_ref().map(|p| p.text()) {
29 Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion),
30 Some("target_env") => KNOWN_ENV.iter().for_each(add_completion),
31 Some("target_os") => KNOWN_OS.iter().for_each(add_completion),
32 Some("target_vendor") => KNOWN_VENDOR.iter().for_each(add_completion),
33 Some("target_endian") => ["little", "big"].iter().for_each(add_completion),
34 Some(name) => {
35 ctx.krate.map(|krate| {
36 krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| {
37 let mut item = CompletionItem::new(
38 CompletionKind::Attribute,
39 ctx.source_range(),
40 s.as_str(),
41 );
42 item.insert_text(format!(r#""{}""#, s));
43
44 acc.add(item.build());
45 })
46 });
47 }
48 None => {
49 ctx.krate.map(|krate| {
50 krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| {
51 let item = CompletionItem::new(
52 CompletionKind::Attribute,
53 ctx.source_range(),
54 s.as_str(),
55 );
56 acc.add(item.build());
57 })
58 });
59 }
60 };
61}
62
63const KNOWN_ARCH: [&'static str; 19] = [
64 "aarch64",
65 "arm",
66 "avr",
67 "hexagon",
68 "mips",
69 "mips64",
70 "msp430",
71 "nvptx64",
72 "powerpc",
73 "powerpc64",
74 "riscv32",
75 "riscv64",
76 "s390x",
77 "sparc",
78 "sparc64",
79 "wasm32",
80 "wasm64",
81 "x86",
82 "x86_64",
83];
84
85const KNOWN_ENV: [&'static str; 7] =
86 ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];
87
88const KNOWN_OS: [&'static str; 20] = [
89 "cuda",
90 "dragonfly",
91 "emscripten",
92 "freebsd",
93 "fuchsia",
94 "haiku",
95 "hermit",
96 "illumos",
97 "l4re",
98 "linux",
99 "netbsd",
100 "none",
101 "openbsd",
102 "psp",
103 "redox",
104 "solaris",
105 "uefi",
106 "unknown",
107 "vxworks",
108 "windows",
109];
110
111const KNOWN_VENDOR: [&'static str; 8] =
112 ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"];