aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/presentation.rs
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-04-21 13:31:57 +0100
committerBenjamin Coenen <[email protected]>2020-04-21 13:31:57 +0100
commit1c3a1385a587f0713908c0ae888ffad31f13de11 (patch)
treec1f1ea8d9575670d36c09bc7a9c0c0e34fee89d5 /crates/ra_ide/src/completion/presentation.rs
parentaf3c19e85f55db9277ce9ad5b784df2ccfe3c9e4 (diff)
Improve autocompletion by looking on the type and name
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/completion/presentation.rs')
-rw-r--r--crates/ra_ide/src/completion/presentation.rs48
1 files changed, 44 insertions, 4 deletions
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs
index 5c3360ce4..bb12a1bdc 100644
--- a/crates/ra_ide/src/completion/presentation.rs
+++ b/crates/ra_ide/src/completion/presentation.rs
@@ -6,12 +6,13 @@ use stdx::SepBy;
6use test_utils::tested_by; 6use test_utils::tested_by;
7 7
8use crate::{ 8use 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
17impl Completions { 18impl 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,8 +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 .compute_score(ctx) 35 .build();
35 .add_to(self); 36
37 compute_score(&mut completion_item, ctx);
38
39 self.add(completion_item);
36 } 40 }
37 41
38 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) {
@@ -295,6 +299,42 @@ impl Completions {
295 } 299 }
296} 300}
297 301
302pub(crate) fn compute_score(completion_item: &mut CompletionItem, ctx: &CompletionContext) {
303 let (active_name, active_type) = if let Some(record_field) = &ctx.record_field_syntax {
304 if let Some((struct_field, _)) = ctx.sema.resolve_record_field(record_field) {
305 (
306 struct_field.name(ctx.db).to_string(),
307 struct_field.signature_ty(ctx.db).display(ctx.db).to_string(),
308 )
309 } else {
310 return;
311 }
312 } else if let Some(call_info) = call_info(ctx.db, ctx.file_position) {
313 if call_info.active_parameter_type().is_some()
314 && call_info.active_parameter_name().is_some()
315 {
316 (call_info.active_parameter_name().unwrap(), call_info.active_parameter_type().unwrap())
317 } else {
318 return;
319 }
320 } else {
321 return;
322 };
323
324 // Compute score
325 // For the same type
326 if let Some(a_parameter_type) = completion_item.detail() {
327 if &active_type == a_parameter_type {
328 // If same type + same name then go top position
329 if active_name == completion_item.label() {
330 completion_item.set_score(CompletionScore::TypeAndNameMatch);
331 } else {
332 completion_item.set_score(CompletionScore::TypeMatch);
333 }
334 }
335 }
336}
337
298enum Params { 338enum Params {
299 Named(Vec<String>), 339 Named(Vec<String>),
300 Anonymous(usize), 340 Anonymous(usize),