diff options
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_item.rs | 41 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/presentation.rs | 29 |
2 files changed, 20 insertions, 50 deletions
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index cb880d92c..f515fcc14 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs | |||
@@ -1,14 +1,8 @@ | |||
1 | use std::fmt; | 1 | use std::fmt; |
2 | 2 | ||
3 | use hir::{Docs, Documentation}; | 3 | use hir::Documentation; |
4 | use ra_syntax::TextRange; | 4 | use ra_syntax::TextRange; |
5 | use ra_text_edit::{ TextEditBuilder, TextEdit}; | 5 | use ra_text_edit::{TextEditBuilder, TextEdit}; |
6 | |||
7 | use crate::completion::{ | ||
8 | completion_context::CompletionContext, | ||
9 | const_label, | ||
10 | type_label | ||
11 | }; | ||
12 | 6 | ||
13 | /// `CompletionItem` describes a single completion variant in the editor pop-up. | 7 | /// `CompletionItem` describes a single completion variant in the editor pop-up. |
14 | /// It is basically a POD with various properties. To construct a | 8 | /// It is basically a POD with various properties. To construct a |
@@ -253,27 +247,6 @@ impl Builder { | |||
253 | self.documentation = docs.map(Into::into); | 247 | self.documentation = docs.map(Into::into); |
254 | self | 248 | self |
255 | } | 249 | } |
256 | pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { | ||
257 | if let Some(docs) = ct.docs(ctx.db) { | ||
258 | self.documentation = Some(docs); | ||
259 | } | ||
260 | |||
261 | self.detail = Some(const_item_label(ctx, ct)); | ||
262 | self.kind = Some(CompletionItemKind::Const); | ||
263 | |||
264 | self | ||
265 | } | ||
266 | |||
267 | pub(super) fn from_type(mut self, ctx: &CompletionContext, ty: hir::Type) -> Builder { | ||
268 | if let Some(docs) = ty.docs(ctx.db) { | ||
269 | self.documentation = Some(docs); | ||
270 | } | ||
271 | |||
272 | self.detail = Some(type_item_label(ctx, ty)); | ||
273 | self.kind = Some(CompletionItemKind::TypeAlias); | ||
274 | |||
275 | self | ||
276 | } | ||
277 | } | 250 | } |
278 | 251 | ||
279 | impl<'a> Into<CompletionItem> for Builder { | 252 | impl<'a> Into<CompletionItem> for Builder { |
@@ -307,16 +280,6 @@ impl Into<Vec<CompletionItem>> for Completions { | |||
307 | } | 280 | } |
308 | } | 281 | } |
309 | 282 | ||
310 | fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { | ||
311 | let node = ct.source(ctx.db).1; | ||
312 | const_label(&node) | ||
313 | } | ||
314 | |||
315 | fn type_item_label(ctx: &CompletionContext, ty: hir::Type) -> String { | ||
316 | let node = ty.source(ctx.db).1; | ||
317 | type_label(&node) | ||
318 | } | ||
319 | |||
320 | #[cfg(test)] | 283 | #[cfg(test)] |
321 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { | 284 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { |
322 | use crate::mock_analysis::{single_file_with_position, analysis_and_position}; | 285 | use crate::mock_analysis::{single_file_with_position, analysis_and_position}; |
diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index d386288ed..0ead52032 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs | |||
@@ -6,7 +6,7 @@ use ra_syntax::ast::NameOwner; | |||
6 | 6 | ||
7 | use crate::completion::{ | 7 | use crate::completion::{ |
8 | Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, | 8 | Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, |
9 | function_label, | 9 | function_label, const_label, type_label, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | impl Completions { | 12 | impl Completions { |
@@ -91,6 +91,8 @@ impl Completions { | |||
91 | ) { | 91 | ) { |
92 | let sig = func.signature(ctx.db); | 92 | let sig = func.signature(ctx.db); |
93 | let name = name.unwrap_or_else(|| sig.name().to_string()); | 93 | let name = name.unwrap_or_else(|| sig.name().to_string()); |
94 | let (_, ast_node) = func.source(ctx.db); | ||
95 | let detail = function_label(&ast_node); | ||
94 | 96 | ||
95 | let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) | 97 | let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) |
96 | .kind(if sig.has_self_param() { | 98 | .kind(if sig.has_self_param() { |
@@ -99,7 +101,7 @@ impl Completions { | |||
99 | CompletionItemKind::Function | 101 | CompletionItemKind::Function |
100 | }) | 102 | }) |
101 | .set_documentation(func.docs(ctx.db)) | 103 | .set_documentation(func.docs(ctx.db)) |
102 | .set_detail(function_item_label(ctx, func)); | 104 | .set_detail(detail); |
103 | // If not an import, add parenthesis automatically. | 105 | // If not an import, add parenthesis automatically. |
104 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | 106 | if ctx.use_item_syntax.is_none() && !ctx.is_call { |
105 | tested_by!(inserts_parens_for_function_calls); | 107 | tested_by!(inserts_parens_for_function_calls); |
@@ -115,13 +117,18 @@ impl Completions { | |||
115 | } | 117 | } |
116 | 118 | ||
117 | pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { | 119 | pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { |
118 | let (_file_id, cosnt_def) = constant.source(ctx.db); | 120 | let (_file_id, ast_node) = constant.source(ctx.db); |
119 | let name = match cosnt_def.name() { | 121 | let name = match ast_node.name() { |
120 | Some(name) => name, | 122 | Some(name) => name, |
121 | _ => return, | 123 | _ => return, |
122 | }; | 124 | }; |
125 | let (_, ast_node) = constant.source(ctx.db); | ||
126 | let detail = const_label(&ast_node); | ||
127 | |||
123 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | 128 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) |
124 | .from_const(ctx, constant) | 129 | .kind(CompletionItemKind::Const) |
130 | .set_documentation(constant.docs(ctx.db)) | ||
131 | .detail(detail) | ||
125 | .add_to(self); | 132 | .add_to(self); |
126 | } | 133 | } |
127 | 134 | ||
@@ -131,8 +138,13 @@ impl Completions { | |||
131 | Some(name) => name, | 138 | Some(name) => name, |
132 | _ => return, | 139 | _ => return, |
133 | }; | 140 | }; |
141 | let (_, ast_node) = type_alias.source(ctx.db); | ||
142 | let detail = type_label(&ast_node); | ||
143 | |||
134 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | 144 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) |
135 | .from_type(ctx, type_alias) | 145 | .kind(CompletionItemKind::TypeAlias) |
146 | .set_documentation(type_alias.docs(ctx.db)) | ||
147 | .detail(detail) | ||
136 | .add_to(self); | 148 | .add_to(self); |
137 | } | 149 | } |
138 | 150 | ||
@@ -152,11 +164,6 @@ impl Completions { | |||
152 | } | 164 | } |
153 | } | 165 | } |
154 | 166 | ||
155 | fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> { | ||
156 | let node = function.source(ctx.db).1; | ||
157 | function_label(&node) | ||
158 | } | ||
159 | |||
160 | #[cfg(test)] | 167 | #[cfg(test)] |
161 | mod tests { | 168 | mod tests { |
162 | use test_utils::covers; | 169 | use test_utils::covers; |