diff options
Diffstat (limited to 'crates/ra_ide/src/completion/presentation.rs')
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 2189cef65..f8dac1d54 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs | |||
@@ -6,12 +6,13 @@ use stdx::SepBy; | |||
6 | use test_utils::tested_by; | 6 | use test_utils::tested_by; |
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | call_info::call_info, | ||
9 | completion::{ | 10 | completion::{ |
10 | completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, | 11 | completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, |
11 | CompletionKind, Completions, | 12 | CompletionKind, Completions, |
12 | }, | 13 | }, |
13 | display::{const_label, macro_label, type_label, FunctionSignature}, | 14 | display::{const_label, macro_label, type_label, FunctionSignature}, |
14 | RootDatabase, | 15 | CompletionScore, RootDatabase, |
15 | }; | 16 | }; |
16 | 17 | ||
17 | impl Completions { | 18 | impl Completions { |
@@ -22,7 +23,7 @@ impl Completions { | |||
22 | ty: &Type, | 23 | ty: &Type, |
23 | ) { | 24 | ) { |
24 | let is_deprecated = is_deprecated(field, ctx.db); | 25 | let is_deprecated = is_deprecated(field, ctx.db); |
25 | CompletionItem::new( | 26 | let mut completion_item = CompletionItem::new( |
26 | CompletionKind::Reference, | 27 | CompletionKind::Reference, |
27 | ctx.source_range(), | 28 | ctx.source_range(), |
28 | field.name(ctx.db).to_string(), | 29 | field.name(ctx.db).to_string(), |
@@ -31,7 +32,11 @@ impl Completions { | |||
31 | .detail(ty.display(ctx.db).to_string()) | 32 | .detail(ty.display(ctx.db).to_string()) |
32 | .set_documentation(field.docs(ctx.db)) | 33 | .set_documentation(field.docs(ctx.db)) |
33 | .set_deprecated(is_deprecated) | 34 | .set_deprecated(is_deprecated) |
34 | .add_to(self); | 35 | .build(); |
36 | |||
37 | compute_score(&mut completion_item, ctx); | ||
38 | |||
39 | self.add(completion_item); | ||
35 | } | 40 | } |
36 | 41 | ||
37 | pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { | 42 | pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { |
@@ -300,6 +305,42 @@ impl Completions { | |||
300 | } | 305 | } |
301 | } | 306 | } |
302 | 307 | ||
308 | pub(crate) fn compute_score(completion_item: &mut CompletionItem, ctx: &CompletionContext) { | ||
309 | let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax { | ||
310 | if let Some((struct_field, _)) = ctx.sema.resolve_record_field(record_field) { | ||
311 | ( | ||
312 | struct_field.name(ctx.db).to_string(), | ||
313 | struct_field.signature_ty(ctx.db).display(ctx.db).to_string(), | ||
314 | ) | ||
315 | } else { | ||
316 | return; | ||
317 | } | ||
318 | } else if let Some(call_info) = call_info(ctx.db, ctx.file_position) { | ||
319 | if call_info.active_parameter_type().is_some() | ||
320 | && call_info.active_parameter_name().is_some() | ||
321 | { | ||
322 | (call_info.active_parameter_name().unwrap(), call_info.active_parameter_type().unwrap()) | ||
323 | } else { | ||
324 | return; | ||
325 | } | ||
326 | } else { | ||
327 | return; | ||
328 | }; | ||
329 | |||
330 | // Compute score | ||
331 | // For the same type | ||
332 | if let Some(a_parameter_type) = completion_item.detail() { | ||
333 | if &active_type == a_parameter_type { | ||
334 | // If same type + same name then go top position | ||
335 | if active_name == completion_item.label() { | ||
336 | completion_item.set_score(CompletionScore::TypeAndNameMatch); | ||
337 | } else { | ||
338 | completion_item.set_score(CompletionScore::TypeMatch); | ||
339 | } | ||
340 | } | ||
341 | } | ||
342 | } | ||
343 | |||
303 | enum Params { | 344 | enum Params { |
304 | Named(Vec<String>), | 345 | Named(Vec<String>), |
305 | Anonymous(usize), | 346 | Anonymous(usize), |