diff options
author | Aleksey Kladov <[email protected]> | 2019-02-24 15:51:38 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-24 15:51:38 +0000 |
commit | d0a261468e67be9213a3fae20f1511e303cac064 (patch) | |
tree | 2169423b7b9472e51b870f2ade7f5fb1df9fc6d9 /crates/ra_ide_api | |
parent | 67528c4b3943a2027839a25770d079132a9ea130 (diff) |
introduce completion presentation
This module should remove completion rendering boilerplate from the
"brains" of completion engine.
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/completion.rs | 1 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_dot.rs | 24 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_struct_literal.rs | 15 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 33 |
4 files changed, 41 insertions, 32 deletions
diff --git a/crates/ra_ide_api/src/completion.rs b/crates/ra_ide_api/src/completion.rs index d92e01bfb..e13baa7a9 100644 --- a/crates/ra_ide_api/src/completion.rs +++ b/crates/ra_ide_api/src/completion.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | mod completion_item; | 1 | mod completion_item; |
2 | mod completion_context; | 2 | mod completion_context; |
3 | mod presentation; | ||
3 | 4 | ||
4 | mod complete_dot; | 5 | mod complete_dot; |
5 | mod complete_struct_literal; | 6 | mod complete_struct_literal; |
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 20fa323ce..c9099bcd7 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use hir::{Ty, AdtDef, Docs}; | 1 | use hir::{Ty, AdtDef}; |
2 | 2 | ||
3 | use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; | 3 | use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind, CompletionKind}; |
4 | use crate::completion::completion_item::CompletionKind; | ||
5 | 4 | ||
6 | /// Complete dot accesses, i.e. fields or methods (currently only fields). | 5 | /// Complete dot accesses, i.e. fields or methods (currently only fields). |
7 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 6 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { |
@@ -29,15 +28,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) | |||
29 | match def_id { | 28 | match def_id { |
30 | AdtDef::Struct(s) => { | 29 | AdtDef::Struct(s) => { |
31 | for field in s.fields(ctx.db) { | 30 | for field in s.fields(ctx.db) { |
32 | CompletionItem::new( | 31 | acc.add_field(CompletionKind::Reference, ctx, field, substs); |
33 | CompletionKind::Reference, | ||
34 | ctx.source_range(), | ||
35 | field.name(ctx.db).to_string(), | ||
36 | ) | ||
37 | .kind(CompletionItemKind::Field) | ||
38 | .detail(field.ty(ctx.db).subst(substs).to_string()) | ||
39 | .set_documentation(field.docs(ctx.db)) | ||
40 | .add_to(acc); | ||
41 | } | 32 | } |
42 | } | 33 | } |
43 | 34 | ||
@@ -47,14 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) | |||
47 | } | 38 | } |
48 | Ty::Tuple(fields) => { | 39 | Ty::Tuple(fields) => { |
49 | for (i, ty) in fields.iter().enumerate() { | 40 | for (i, ty) in fields.iter().enumerate() { |
50 | CompletionItem::new( | 41 | acc.add_pos_field(CompletionKind::Reference, ctx, i, ty); |
51 | CompletionKind::Reference, | ||
52 | ctx.source_range(), | ||
53 | i.to_string(), | ||
54 | ) | ||
55 | .kind(CompletionItemKind::Field) | ||
56 | .detail(ty.to_string()) | ||
57 | .add_to(acc); | ||
58 | } | 42 | } |
59 | } | 43 | } |
60 | _ => {} | 44 | _ => {} |
diff --git a/crates/ra_ide_api/src/completion/complete_struct_literal.rs b/crates/ra_ide_api/src/completion/complete_struct_literal.rs index f8dd2baad..c617bff5f 100644 --- a/crates/ra_ide_api/src/completion/complete_struct_literal.rs +++ b/crates/ra_ide_api/src/completion/complete_struct_literal.rs | |||
@@ -1,7 +1,6 @@ | |||
1 | use hir::{Ty, AdtDef, Docs}; | 1 | use hir::{Ty, AdtDef}; |
2 | 2 | ||
3 | use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; | 3 | use crate::completion::{CompletionContext, Completions, CompletionKind}; |
4 | use crate::completion::completion_item::CompletionKind; | ||
5 | 4 | ||
6 | /// Complete fields in fields literals. | 5 | /// Complete fields in fields literals. |
7 | pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { | 6 | pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionContext) { |
@@ -23,15 +22,7 @@ pub(super) fn complete_struct_literal(acc: &mut Completions, ctx: &CompletionCon | |||
23 | match adt { | 22 | match adt { |
24 | AdtDef::Struct(s) => { | 23 | AdtDef::Struct(s) => { |
25 | for field in s.fields(ctx.db) { | 24 | for field in s.fields(ctx.db) { |
26 | CompletionItem::new( | 25 | acc.add_field(CompletionKind::Reference, ctx, field, substs); |
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 | } | 26 | } |
36 | } | 27 | } |
37 | 28 | ||
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs new file mode 100644 index 000000000..6b2de56bc --- /dev/null +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -0,0 +1,33 @@ | |||
1 | //! This modules takes care of rendering various defenitions as completion items. | ||
2 | use hir::Docs; | ||
3 | |||
4 | use crate::completion::{Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem}; | ||
5 | |||
6 | impl Completions { | ||
7 | pub(crate) fn add_field( | ||
8 | &mut self, | ||
9 | kind: CompletionKind, | ||
10 | ctx: &CompletionContext, | ||
11 | field: hir::StructField, | ||
12 | substs: &hir::Substs, | ||
13 | ) { | ||
14 | CompletionItem::new(kind, ctx.source_range(), field.name(ctx.db).to_string()) | ||
15 | .kind(CompletionItemKind::Field) | ||
16 | .detail(field.ty(ctx.db).subst(substs).to_string()) | ||
17 | .set_documentation(field.docs(ctx.db)) | ||
18 | .add_to(self); | ||
19 | } | ||
20 | |||
21 | pub(crate) fn add_pos_field( | ||
22 | &mut self, | ||
23 | kind: CompletionKind, | ||
24 | ctx: &CompletionContext, | ||
25 | field: usize, | ||
26 | ty: &hir::Ty, | ||
27 | ) { | ||
28 | CompletionItem::new(kind, ctx.source_range(), field.to_string()) | ||
29 | .kind(CompletionItemKind::Field) | ||
30 | .detail(ty.to_string()) | ||
31 | .add_to(self); | ||
32 | } | ||
33 | } | ||