diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-24 20:52:04 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-24 20:52:04 +0000 |
commit | 7ffff9c74caae108db53366e3b90857b7c405c6c (patch) | |
tree | b075e5b36542d9cdbdefb7207c01858a68201940 /crates/ra_ide_api/src/completion/complete_pattern.rs | |
parent | dfca3cbeb0680ad1e3694d442b6c761fe3007521 (diff) | |
parent | 330ce2e26baf6f9ebf808e16140f6ad9c9b05d5e (diff) |
Merge #895
895: complete patterns r=matklad a=matklad
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_pattern.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_pattern.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_pattern.rs b/crates/ra_ide_api/src/completion/complete_pattern.rs new file mode 100644 index 000000000..3cf79c080 --- /dev/null +++ b/crates/ra_ide_api/src/completion/complete_pattern.rs | |||
@@ -0,0 +1,87 @@ | |||
1 | use crate::completion::{CompletionContext, Completions}; | ||
2 | |||
3 | /// Completes constats and paths in patterns. | ||
4 | pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { | ||
5 | if !ctx.is_pat_binding { | ||
6 | return; | ||
7 | } | ||
8 | // TODO: ideally, we should look at the type we are matching against and | ||
9 | // suggest variants + auto-imports | ||
10 | let names = ctx.resolver.all_names(ctx.db); | ||
11 | for (name, res) in names.into_iter() { | ||
12 | let r = res.as_ref(); | ||
13 | let def = match r.take_types().or(r.take_values()) { | ||
14 | Some(hir::Resolution::Def(def)) => def, | ||
15 | _ => continue, | ||
16 | }; | ||
17 | match def { | ||
18 | hir::ModuleDef::Enum(..) | ||
19 | | hir::ModuleDef::EnumVariant(..) | ||
20 | | hir::ModuleDef::Const(..) | ||
21 | | hir::ModuleDef::Module(..) => (), | ||
22 | _ => continue, | ||
23 | } | ||
24 | acc.add_resolution(ctx, name.to_string(), &res) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | #[cfg(test)] | ||
29 | mod tests { | ||
30 | use insta::assert_debug_snapshot_matches; | ||
31 | use crate::completion::{CompletionItem, CompletionKind, do_completion}; | ||
32 | |||
33 | fn complete(code: &str) -> Vec<CompletionItem> { | ||
34 | do_completion(code, CompletionKind::Reference) | ||
35 | } | ||
36 | |||
37 | #[test] | ||
38 | fn completes_enum_variants_and_modules() { | ||
39 | let completions = complete( | ||
40 | r" | ||
41 | enum E { X } | ||
42 | use self::E::X; | ||
43 | const Z: E = E::X; | ||
44 | mod m {} | ||
45 | |||
46 | static FOO: E = E::X; | ||
47 | struct Bar { f: u32 } | ||
48 | |||
49 | fn foo() { | ||
50 | match E::X { | ||
51 | <|> | ||
52 | } | ||
53 | } | ||
54 | ", | ||
55 | ); | ||
56 | assert_debug_snapshot_matches!(completions, @r###"[ | ||
57 | CompletionItem { | ||
58 | label: "E", | ||
59 | source_range: [246; 246), | ||
60 | delete: [246; 246), | ||
61 | insert: "E", | ||
62 | kind: Enum | ||
63 | }, | ||
64 | CompletionItem { | ||
65 | label: "X", | ||
66 | source_range: [246; 246), | ||
67 | delete: [246; 246), | ||
68 | insert: "X", | ||
69 | kind: EnumVariant | ||
70 | }, | ||
71 | CompletionItem { | ||
72 | label: "Z", | ||
73 | source_range: [246; 246), | ||
74 | delete: [246; 246), | ||
75 | insert: "Z", | ||
76 | kind: Const | ||
77 | }, | ||
78 | CompletionItem { | ||
79 | label: "m", | ||
80 | source_range: [246; 246), | ||
81 | delete: [246; 246), | ||
82 | insert: "m", | ||
83 | kind: Module | ||
84 | } | ||
85 | ]"###); | ||
86 | } | ||
87 | } | ||