diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-21 18:05:21 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-21 18:05:21 +0100 |
commit | f06ddbea6a038eb546d44cb71f2d4d9c055a8828 (patch) | |
tree | 9c39a29bdee4f45dcbd497c25350cbfbb0c5e9c3 /crates/ide_completion/src/completions | |
parent | 1b05dbba39d5a4d46f321dc962df99038cddbf21 (diff) | |
parent | ae823aa23f1c4fa55e71dd972d0b10c69148b0b4 (diff) |
Merge #9080
9080: Improve completion of cfg attributes r=JamieCunliffe a=JamieCunliffe
This will close #5398 and it also adds some completion for cfg options.
Co-authored-by: Jamie Cunliffe <[email protected]>
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r-- | crates/ide_completion/src/completions/attribute.rs | 15 | ||||
-rw-r--r-- | crates/ide_completion/src/completions/attribute/cfg.rs | 112 |
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 | ||
18 | mod cfg; | ||
18 | mod derive; | 19 | mod derive; |
19 | mod lint; | 20 | mod lint; |
20 | mod repr; | 21 | mod 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 | |||
3 | use std::iter; | ||
4 | |||
5 | use syntax::SyntaxKind; | ||
6 | |||
7 | use crate::{ | ||
8 | completions::Completions, context::CompletionContext, item::CompletionKind, CompletionItem, | ||
9 | CompletionItemKind, | ||
10 | }; | ||
11 | |||
12 | pub(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 | |||
63 | const 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 | |||
85 | const KNOWN_ENV: [&'static str; 7] = | ||
86 | ["eabihf", "gnu", "gnueabihf", "msvc", "relibc", "sgx", "uclibc"]; | ||
87 | |||
88 | const 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 | |||
111 | const KNOWN_VENDOR: [&'static str; 8] = | ||
112 | ["apple", "fortanix", "nvidia", "pc", "sony", "unknown", "wrs", "uwp"]; | ||