aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs14
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs19
2 files changed, 26 insertions, 7 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 5d4e60dc5..65bba6dc7 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -28,13 +28,13 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
28 Ty::Adt { def_id, .. } => { 28 Ty::Adt { def_id, .. } => {
29 match def_id.resolve(ctx.db)? { 29 match def_id.resolve(ctx.db)? {
30 Def::Struct(s) => { 30 Def::Struct(s) => {
31 let variant_data = s.variant_data(ctx.db)?; 31 for field in s.fields(ctx.db)? {
32 for field in variant_data.fields() {
33 CompletionItem::new( 32 CompletionItem::new(
34 CompletionKind::Reference, 33 CompletionKind::Reference,
35 field.name().to_string(), 34 field.name().to_string(),
36 ) 35 )
37 .kind(CompletionItemKind::Field) 36 .kind(CompletionItemKind::Field)
37 .set_detail(field.ty(ctx.db)?.map(|ty| ty.to_string()))
38 .add_to(acc); 38 .add_to(acc);
39 } 39 }
40 } 40 }
@@ -72,7 +72,7 @@ mod tests {
72 a.<|> 72 a.<|>
73 } 73 }
74 ", 74 ",
75 r#"the_field"#, 75 r#"the_field "u32""#,
76 ); 76 );
77 } 77 }
78 78
@@ -80,14 +80,14 @@ mod tests {
80 fn test_struct_field_completion_self() { 80 fn test_struct_field_completion_self() {
81 check_ref_completion( 81 check_ref_completion(
82 r" 82 r"
83 struct A { the_field: u32 } 83 struct A { the_field: (u32,) }
84 impl A { 84 impl A {
85 fn foo(self) { 85 fn foo(self) {
86 self.<|> 86 self.<|>
87 } 87 }
88 } 88 }
89 ", 89 ",
90 r#"the_field"#, 90 r#"the_field "(u32,)""#,
91 ); 91 );
92 } 92 }
93 93
@@ -95,14 +95,14 @@ mod tests {
95 fn test_struct_field_completion_autoderef() { 95 fn test_struct_field_completion_autoderef() {
96 check_ref_completion( 96 check_ref_completion(
97 r" 97 r"
98 struct A { the_field: u32 } 98 struct A { the_field: (u32, i32) }
99 impl A { 99 impl A {
100 fn foo(&self) { 100 fn foo(&self) {
101 self.<|> 101 self.<|>
102 } 102 }
103 } 103 }
104 ", 104 ",
105 r#"the_field"#, 105 r#"the_field "(u32, i32)""#,
106 ); 106 );
107 } 107 }
108 108
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index a25b87bee..334449fae 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -11,6 +11,7 @@ pub struct CompletionItem {
11 /// completion. 11 /// completion.
12 completion_kind: CompletionKind, 12 completion_kind: CompletionKind,
13 label: String, 13 label: String,
14 detail: Option<String>,
14 lookup: Option<String>, 15 lookup: Option<String>,
15 snippet: Option<String>, 16 snippet: Option<String>,
16 kind: Option<CompletionItemKind>, 17 kind: Option<CompletionItemKind>,
@@ -51,6 +52,7 @@ impl CompletionItem {
51 Builder { 52 Builder {
52 completion_kind, 53 completion_kind,
53 label, 54 label,
55 detail: None,
54 lookup: None, 56 lookup: None,
55 snippet: None, 57 snippet: None,
56 kind: None, 58 kind: None,
@@ -60,6 +62,10 @@ impl CompletionItem {
60 pub fn label(&self) -> &str { 62 pub fn label(&self) -> &str {
61 &self.label 63 &self.label
62 } 64 }
65 /// Short one-line additional information, like a type
66 pub fn detail(&self) -> Option<&str> {
67 self.detail.as_ref().map(|it| it.as_str())
68 }
63 /// What string is used for filtering. 69 /// What string is used for filtering.
64 pub fn lookup(&self) -> &str { 70 pub fn lookup(&self) -> &str {
65 self.lookup 71 self.lookup
@@ -87,6 +93,7 @@ impl CompletionItem {
87pub(crate) struct Builder { 93pub(crate) struct Builder {
88 completion_kind: CompletionKind, 94 completion_kind: CompletionKind,
89 label: String, 95 label: String,
96 detail: Option<String>,
90 lookup: Option<String>, 97 lookup: Option<String>,
91 snippet: Option<String>, 98 snippet: Option<String>,
92 kind: Option<CompletionItemKind>, 99 kind: Option<CompletionItemKind>,
@@ -100,6 +107,7 @@ impl Builder {
100 pub(crate) fn build(self) -> CompletionItem { 107 pub(crate) fn build(self) -> CompletionItem {
101 CompletionItem { 108 CompletionItem {
102 label: self.label, 109 label: self.label,
110 detail: self.detail,
103 lookup: self.lookup, 111 lookup: self.lookup,
104 snippet: self.snippet, 112 snippet: self.snippet,
105 kind: self.kind, 113 kind: self.kind,
@@ -118,6 +126,14 @@ impl Builder {
118 self.kind = Some(kind); 126 self.kind = Some(kind);
119 self 127 self
120 } 128 }
129 #[allow(unused)]
130 pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
131 self.set_detail(Some(detail))
132 }
133 pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
134 self.detail = detail.map(Into::into);
135 self
136 }
121 pub(super) fn from_resolution( 137 pub(super) fn from_resolution(
122 mut self, 138 mut self,
123 ctx: &CompletionContext, 139 ctx: &CompletionContext,
@@ -227,6 +243,9 @@ impl Completions {
227 } else { 243 } else {
228 res.push_str(&c.label); 244 res.push_str(&c.label);
229 } 245 }
246 if let Some(detail) = &c.detail {
247 res.push_str(&format!(" {:?}", detail));
248 }
230 if let Some(snippet) = &c.snippet { 249 if let Some(snippet) = &c.snippet {
231 res.push_str(&format!(" {:?}", snippet)); 250 res.push_str(&format!(" {:?}", snippet));
232 } 251 }