aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions/attribute/cfg.rs
blob: 847e6529a1bf272fcd386c10a6914aada06d8535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Completion for cfg

use std::iter;

use syntax::SyntaxKind;

use crate::{
    completions::Completions, context::CompletionContext, item::CompletionKind, CompletionItem,
    CompletionItemKind,
};

pub(crate) fn complete_cfg(acc: &mut Completions, ctx: &CompletionContext) {
    let add_completion = |item: &&str| {
        let mut completion =
            CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), *item);
        completion.insert_text(format!(r#""{}""#, item));
        completion.kind(CompletionItemKind::Attribute);
        acc.add(completion.build());
    };

    let previous = iter::successors(ctx.original_token.prev_token(), |t| {
        (matches!(t.kind(), SyntaxKind::EQ) || t.kind().is_trivia())
            .then(|| t.prev_token())
            .flatten()
    })
    .find(|t| matches!(t.kind(), SyntaxKind::IDENT));

    match previous.as_ref().map(|p| p.text()) {
        Some("target_arch") => KNOWN_ARCH.iter().for_each(add_completion),
        Some("target_env") => KNOWN_ENV.iter().for_each(add_completion),
        Some("target_os") => KNOWN_OS.iter().for_each(add_completion),
        Some("target_vendor") => KNOWN_VENDOR.iter().for_each(add_completion),
        Some("target_endian") => ["little", "big"].iter().for_each(add_completion),
        Some(name) => {
            ctx.krate.map(|krate| {
                krate.potential_cfg(ctx.db).get_cfg_values(&name).iter().for_each(|s| {
                    let mut item = CompletionItem::new(
                        CompletionKind::Attribute,
                        ctx.source_range(),
                        s.as_str(),
                    );
                    item.insert_text(format!(r#""{}""#, s));

                    acc.add(item.build());
                })
            });
        }
        None => {
            ctx.krate.map(|krate| {
                krate.potential_cfg(ctx.db).get_cfg_keys().iter().for_each(|s| {
                    let item = CompletionItem::new(
                        CompletionKind::Attribute,
                        ctx.source_range(),
                        s.as_str(),
                    );
                    acc.add(item.build());
                })
            });
        }
    };
}

const KNOWN_ARCH: [&'static str; 19] = [
    "aarch64",
    "arm",
    "avr",
    "hexagon",
    "mips",
    "mips64",
    "msp430",
    "nvptx64",
    "powerpc",
    "powerpc64",
    "riscv32",
    "riscv64",
    "s390x",
    "sparc",
    "sparc64",
    "wasm32",
    "wasm64",
    "x86",
    "x86_64",
];

const KNOWN_ENV: [&'static str; 7] =
    ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"];

const KNOWN_OS: [&'static str; 20] = [
    "cuda",
    "dragonfly",
    "emscripten",
    "freebsd",
    "fuchsia",
    "haiku",
    "hermit",
    "illumos",
    "l4re",
    "linux",
    "netbsd",
    "none",
    "openbsd",
    "psp",
    "redox",
    "solaris",
    "uefi",
    "unknown",
    "vxworks",
    "windows",
];

const KNOWN_VENDOR: [&'static str; 8] =
    ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"];