diff options
Diffstat (limited to 'crates/ra_analysis/src/completion/complete_dot.rs')
-rw-r--r-- | crates/ra_analysis/src/completion/complete_dot.rs | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/crates/ra_analysis/src/completion/complete_dot.rs b/crates/ra_analysis/src/completion/complete_dot.rs deleted file mode 100644 index 5d4e60dc5..000000000 --- a/crates/ra_analysis/src/completion/complete_dot.rs +++ /dev/null | |||
@@ -1,121 +0,0 @@ | |||
1 | use hir::{Ty, Def}; | ||
2 | |||
3 | use crate::Cancelable; | ||
4 | use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind}; | ||
5 | |||
6 | /// Complete dot accesses, i.e. fields or methods (currently only fields). | ||
7 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { | ||
8 | let (function, receiver) = match (&ctx.function, ctx.dot_receiver) { | ||
9 | (Some(function), Some(receiver)) => (function, receiver), | ||
10 | _ => return Ok(()), | ||
11 | }; | ||
12 | let infer_result = function.infer(ctx.db)?; | ||
13 | let syntax_mapping = function.body_syntax_mapping(ctx.db)?; | ||
14 | let expr = match syntax_mapping.node_expr(receiver) { | ||
15 | Some(expr) => expr, | ||
16 | None => return Ok(()), | ||
17 | }; | ||
18 | let receiver_ty = infer_result[expr].clone(); | ||
19 | if !ctx.is_method_call { | ||
20 | complete_fields(acc, ctx, receiver_ty)?; | ||
21 | } | ||
22 | Ok(()) | ||
23 | } | ||
24 | |||
25 | fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> { | ||
26 | for receiver in receiver.autoderef(ctx.db) { | ||
27 | match receiver { | ||
28 | Ty::Adt { def_id, .. } => { | ||
29 | match def_id.resolve(ctx.db)? { | ||
30 | Def::Struct(s) => { | ||
31 | let variant_data = s.variant_data(ctx.db)?; | ||
32 | for field in variant_data.fields() { | ||
33 | CompletionItem::new( | ||
34 | CompletionKind::Reference, | ||
35 | field.name().to_string(), | ||
36 | ) | ||
37 | .kind(CompletionItemKind::Field) | ||
38 | .add_to(acc); | ||
39 | } | ||
40 | } | ||
41 | // TODO unions | ||
42 | _ => {} | ||
43 | } | ||
44 | } | ||
45 | Ty::Tuple(fields) => { | ||
46 | for (i, _ty) in fields.iter().enumerate() { | ||
47 | CompletionItem::new(CompletionKind::Reference, i.to_string()) | ||
48 | .kind(CompletionItemKind::Field) | ||
49 | .add_to(acc); | ||
50 | } | ||
51 | } | ||
52 | _ => {} | ||
53 | }; | ||
54 | } | ||
55 | Ok(()) | ||
56 | } | ||
57 | |||
58 | #[cfg(test)] | ||
59 | mod tests { | ||
60 | use crate::completion::*; | ||
61 | |||
62 | fn check_ref_completion(code: &str, expected_completions: &str) { | ||
63 | check_completion(code, expected_completions, CompletionKind::Reference); | ||
64 | } | ||
65 | |||
66 | #[test] | ||
67 | fn test_struct_field_completion() { | ||
68 | check_ref_completion( | ||
69 | r" | ||
70 | struct A { the_field: u32 } | ||
71 | fn foo(a: A) { | ||
72 | a.<|> | ||
73 | } | ||
74 | ", | ||
75 | r#"the_field"#, | ||
76 | ); | ||
77 | } | ||
78 | |||
79 | #[test] | ||
80 | fn test_struct_field_completion_self() { | ||
81 | check_ref_completion( | ||
82 | r" | ||
83 | struct A { the_field: u32 } | ||
84 | impl A { | ||
85 | fn foo(self) { | ||
86 | self.<|> | ||
87 | } | ||
88 | } | ||
89 | ", | ||
90 | r#"the_field"#, | ||
91 | ); | ||
92 | } | ||
93 | |||
94 | #[test] | ||
95 | fn test_struct_field_completion_autoderef() { | ||
96 | check_ref_completion( | ||
97 | r" | ||
98 | struct A { the_field: u32 } | ||
99 | impl A { | ||
100 | fn foo(&self) { | ||
101 | self.<|> | ||
102 | } | ||
103 | } | ||
104 | ", | ||
105 | r#"the_field"#, | ||
106 | ); | ||
107 | } | ||
108 | |||
109 | #[test] | ||
110 | fn test_no_struct_field_completion_for_method_call() { | ||
111 | check_ref_completion( | ||
112 | r" | ||
113 | struct A { the_field: u32 } | ||
114 | fn foo(a: A) { | ||
115 | a.<|>() | ||
116 | } | ||
117 | ", | ||
118 | r#""#, | ||
119 | ); | ||
120 | } | ||
121 | } | ||