aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions/pattern.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-10-26 19:06:34 +0000
committerGitHub <[email protected]>2020-10-26 19:06:34 +0000
commitd10e2a04c8a5ff157acd95bc7458d36d9f396390 (patch)
treefb68b034d7e56053e5d1b4302e2996f64b024e11 /crates/completion/src/completions/pattern.rs
parentd01e412eb1572676a33ad145f3370a7157dbc9df (diff)
parent357bf0cedc658b7c95952324fda4bbe7f41a3e6a (diff)
Merge #6351
6351: Organized completions r=popzxc a=popzxc This PR continues the work on refactoring of the `completions` crate. In this episode: - Actual completions methods are encapsulated into `completions` module, so they aren't mixed with the rest of the code. - Name duplication was removed (`complete_attribute` => `completions::attribute`, `completion_context` => `context`). - `Completions` structure was moved from `item` module to the `completions`. - `presentation` module was removed, as it was basically a module with `impl` for `Completions`. - Code approaches were a bit unified here and there. Co-authored-by: Igor Aleksanov <[email protected]>
Diffstat (limited to 'crates/completion/src/completions/pattern.rs')
-rw-r--r--crates/completion/src/completions/pattern.rs88
1 files changed, 88 insertions, 0 deletions
diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs
new file mode 100644
index 000000000..7ab7f09fe
--- /dev/null
+++ b/crates/completion/src/completions/pattern.rs
@@ -0,0 +1,88 @@
1//! Completes constats and paths in patterns.
2
3use crate::{CompletionContext, Completions};
4
5/// Completes constats and paths in patterns.
6pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7 if !ctx.is_pat_binding_or_const {
8 return;
9 }
10 if ctx.record_pat_syntax.is_some() {
11 return;
12 }
13
14 // FIXME: ideally, we should look at the type we are matching against and
15 // suggest variants + auto-imports
16 ctx.scope.process_all_names(&mut |name, res| {
17 match &res {
18 hir::ScopeDef::ModuleDef(def) => match def {
19 hir::ModuleDef::Adt(hir::Adt::Enum(..))
20 | hir::ModuleDef::Adt(hir::Adt::Struct(..))
21 | hir::ModuleDef::EnumVariant(..)
22 | hir::ModuleDef::Const(..)
23 | hir::ModuleDef::Module(..) => (),
24 _ => return,
25 },
26 hir::ScopeDef::MacroDef(_) => (),
27 _ => return,
28 };
29
30 acc.add_resolution(ctx, name.to_string(), &res)
31 });
32}
33
34#[cfg(test)]
35mod tests {
36 use expect_test::{expect, Expect};
37
38 use crate::{test_utils::completion_list, CompletionKind};
39
40 fn check(ra_fixture: &str, expect: Expect) {
41 let actual = completion_list(ra_fixture, CompletionKind::Reference);
42 expect.assert_eq(&actual)
43 }
44
45 #[test]
46 fn completes_enum_variants_and_modules() {
47 check(
48 r#"
49enum E { X }
50use self::E::X;
51const Z: E = E::X;
52mod m {}
53
54static FOO: E = E::X;
55struct Bar { f: u32 }
56
57fn foo() {
58 match E::X { <|> }
59}
60"#,
61 expect![[r#"
62 st Bar
63 en E
64 ev X ()
65 ct Z
66 md m
67 "#]],
68 );
69 }
70
71 #[test]
72 fn completes_in_simple_macro_call() {
73 check(
74 r#"
75macro_rules! m { ($e:expr) => { $e } }
76enum E { X }
77
78fn foo() {
79 m!(match E::X { <|> })
80}
81"#,
82 expect![[r#"
83 en E
84 ma m!(…) macro_rules! m
85 "#]],
86 );
87 }
88}