aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-02-24 20:52:04 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-02-24 20:52:04 +0000
commit7ffff9c74caae108db53366e3b90857b7c405c6c (patch)
treeb075e5b36542d9cdbdefb7207c01858a68201940 /crates
parentdfca3cbeb0680ad1e3694d442b6c761fe3007521 (diff)
parent330ce2e26baf6f9ebf808e16140f6ad9c9b05d5e (diff)
Merge #895
895: complete patterns r=matklad a=matklad bors r+ Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_api/src/completion.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_pattern.rs87
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs14
3 files changed, 103 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs
index fbfd7e3e7..c8022f94f 100644
--- a/crates/ra_ide_api/src/completion.rs
+++ b/crates/ra_ide_api/src/completion.rs
@@ -4,6 +4,7 @@ mod presentation;
4 4
5mod complete_dot; 5mod complete_dot;
6mod complete_struct_literal; 6mod complete_struct_literal;
7mod complete_pattern;
7mod complete_fn_param; 8mod complete_fn_param;
8mod complete_keyword; 9mod complete_keyword;
9mod complete_snippet; 10mod complete_snippet;
@@ -65,6 +66,7 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
65 complete_scope::complete_scope(&mut acc, &ctx); 66 complete_scope::complete_scope(&mut acc, &ctx);
66 complete_dot::complete_dot(&mut acc, &ctx); 67 complete_dot::complete_dot(&mut acc, &ctx);
67 complete_struct_literal::complete_struct_literal(&mut acc, &ctx); 68 complete_struct_literal::complete_struct_literal(&mut acc, &ctx);
69 complete_pattern::complete_pattern(&mut acc, &ctx);
68 complete_postfix::complete_postfix(&mut acc, &ctx); 70 complete_postfix::complete_postfix(&mut acc, &ctx);
69 Some(acc) 71 Some(acc)
70} 72}
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 @@
1use crate::completion::{CompletionContext, Completions};
2
3/// Completes constats and paths in patterns.
4pub(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)]
29mod 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}
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index d351be054..724d0dfbf 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -23,6 +23,9 @@ pub(crate) struct CompletionContext<'a> {
23 pub(super) use_item_syntax: Option<&'a ast::UseItem>, 23 pub(super) use_item_syntax: Option<&'a ast::UseItem>,
24 pub(super) struct_lit_syntax: Option<&'a ast::StructLit>, 24 pub(super) struct_lit_syntax: Option<&'a ast::StructLit>,
25 pub(super) is_param: bool, 25 pub(super) is_param: bool,
26 /// If a name-binding or reference to a const in a pattern.
27 /// Irrefutable patterns (like let) are excluded.
28 pub(super) is_pat_binding: bool,
26 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. 29 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
27 pub(super) is_trivial_path: bool, 30 pub(super) is_trivial_path: bool,
28 /// If not a trivial, path, the prefix (qualifier). 31 /// If not a trivial, path, the prefix (qualifier).
@@ -58,6 +61,7 @@ impl<'a> CompletionContext<'a> {
58 use_item_syntax: None, 61 use_item_syntax: None,
59 struct_lit_syntax: None, 62 struct_lit_syntax: None,
60 is_param: false, 63 is_param: false,
64 is_pat_binding: false,
61 is_trivial_path: false, 65 is_trivial_path: false,
62 path_prefix: None, 66 path_prefix: None,
63 after_if: false, 67 after_if: false,
@@ -102,12 +106,22 @@ impl<'a> CompletionContext<'a> {
102 // Otherwise, see if this is a declaration. We can use heuristics to 106 // Otherwise, see if this is a declaration. We can use heuristics to
103 // suggest declaration names, see `CompletionKind::Magic`. 107 // suggest declaration names, see `CompletionKind::Magic`.
104 if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) { 108 if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) {
109 if is_node::<ast::BindPat>(name.syntax()) {
110 let bind_pat = name.syntax().ancestors().find_map(ast::BindPat::cast).unwrap();
111 let parent = bind_pat.syntax().parent();
112 if parent.and_then(ast::MatchArm::cast).is_some()
113 || parent.and_then(ast::Condition::cast).is_some()
114 {
115 self.is_pat_binding = true;
116 }
117 }
105 if is_node::<ast::Param>(name.syntax()) { 118 if is_node::<ast::Param>(name.syntax()) {
106 self.is_param = true; 119 self.is_param = true;
107 return; 120 return;
108 } 121 }
109 } 122 }
110 } 123 }
124
111 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) { 125 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) {
112 let name_range = name_ref.syntax().range(); 126 let name_range = name_ref.syntax().range();
113 if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() { 127 if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() {