aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api
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
parent666303faf3c8b4215fde884451688084e298d6a8 (diff)
complete struct literals
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r--crates/ra_ide_api/src/completion.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_struct_literal.rs64
-rw-r--r--crates/ra_ide_api/src/completion/completion_context.rs6
3 files changed, 72 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs
index 4a38d62bb..d92e01bfb 100644
--- a/crates/ra_ide_api/src/completion.rs
+++ b/crates/ra_ide_api/src/completion.rs
@@ -2,6 +2,7 @@ mod completion_item;
2mod completion_context; 2mod completion_context;
3 3
4mod complete_dot; 4mod complete_dot;
5mod complete_struct_literal;
5mod complete_fn_param; 6mod complete_fn_param;
6mod complete_keyword; 7mod complete_keyword;
7mod complete_snippet; 8mod complete_snippet;
@@ -59,6 +60,7 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
59 complete_path::complete_path(&mut acc, &ctx); 60 complete_path::complete_path(&mut acc, &ctx);
60 complete_scope::complete_scope(&mut acc, &ctx); 61 complete_scope::complete_scope(&mut acc, &ctx);
61 complete_dot::complete_dot(&mut acc, &ctx); 62 complete_dot::complete_dot(&mut acc, &ctx);
63 complete_struct_literal::complete_struct_literal(&mut acc, &ctx);
62 complete_postfix::complete_postfix(&mut acc, &ctx); 64 complete_postfix::complete_postfix(&mut acc, &ctx);
63 Some(acc) 65 Some(acc)
64} 66}
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}
diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs
index b9f0abe19..d351be054 100644
--- a/crates/ra_ide_api/src/completion/completion_context.rs
+++ b/crates/ra_ide_api/src/completion/completion_context.rs
@@ -21,6 +21,7 @@ pub(crate) struct CompletionContext<'a> {
21 pub(super) function: Option<hir::Function>, 21 pub(super) function: Option<hir::Function>,
22 pub(super) function_syntax: Option<&'a ast::FnDef>, 22 pub(super) function_syntax: Option<&'a ast::FnDef>,
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) is_param: bool, 25 pub(super) is_param: bool,
25 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path. 26 /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
26 pub(super) is_trivial_path: bool, 27 pub(super) is_trivial_path: bool,
@@ -55,6 +56,7 @@ impl<'a> CompletionContext<'a> {
55 function: None, 56 function: None,
56 function_syntax: None, 57 function_syntax: None,
57 use_item_syntax: None, 58 use_item_syntax: None,
59 struct_lit_syntax: None,
58 is_param: false, 60 is_param: false,
59 is_trivial_path: false, 61 is_trivial_path: false,
60 path_prefix: None, 62 path_prefix: None,
@@ -108,6 +110,10 @@ impl<'a> CompletionContext<'a> {
108 } 110 }
109 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) { 111 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) {
110 let name_range = name_ref.syntax().range(); 112 let name_range = name_ref.syntax().range();
113 if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() {
114 self.struct_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);
115 }
116
111 let top_node = 117 let top_node =
112 name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap(); 118 name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap();
113 119