aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_struct_literal.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-24 14:01:56 +0000
committerAleksey Kladov <[email protected]>2019-02-24 14:01:56 +0000
commit65a2be49539375502ca95c8da455f50f580df2e3 (patch)
treef5e07db7d5ddbc920a5d768f516bf3b6097ad817 /crates/ra_ide_api/src/completion/complete_struct_literal.rs
parent666303faf3c8b4215fde884451688084e298d6a8 (diff)
complete struct literals
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_struct_literal.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_struct_literal.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs
new file mode 100644
index 000000000..893056c2b
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs
@@ -0,0 +1,64 @@
1use hir::{Ty, AdtDef, Docs};
2
3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
4use crate::completion::completion_item::CompletionKind;
5
6/// Complete dot accesses, i.e. fields or methods (currently only fields).
7pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) {
8 let (function, struct_lit) = match (&ctx.function, ctx.struct_lit_syntax) {
9 (Some(function), Some(struct_lit)) => (function, struct_lit),
10 _ => return,
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(struct_lit.into()) {
15 Some(expr) => expr,
16 None => return,
17 };
18 let ty = infer_result[expr].clone();
19 let (adt, substs) = match ty {
20 Ty::Adt { def_id, ref substs, .. } => (def_id, substs),
21 _ => return,
22 };
23 match adt {
24 AdtDef::Struct(s) => {
25 for field in s.fields(ctx.db) {
26 CompletionItem::new(
27 CompletionKind::Reference,
28 ctx.source_range(),
29 field.name(ctx.db).to_string(),
30 )
31 .kind(CompletionItemKind::Field)
32 .detail(field.ty(ctx.db).subst(substs).to_string())
33 .set_documentation(field.docs(ctx.db))
34 .add_to(acc);
35 }
36 }
37
38 // TODO unions
39 AdtDef::Enum(_) => (),
40 };
41}
42
43#[cfg(test)]
44mod tests {
45 use crate::completion::*;
46 use crate::completion::completion_item::check_completion;
47
48 fn check_ref_completion(name: &str, code: &str) {
49 check_completion(name, code, CompletionKind::Reference);
50 }
51
52 #[test]
53 fn test_struct_literal_field() {
54 check_ref_completion(
55 "test_struct_literal_field",
56 r"
57 struct A { the_field: u32 }
58 fn foo() {
59 A { the<|> }
60 }
61 ",
62 );
63 }
64}