aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
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
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')
-rw-r--r--crates/ra_ide/src/completion/complete_dot.rs2
-rw-r--r--crates/ra_ide/src/completion/completion_item.rs42
-rw-r--r--crates/ra_ide/src/completion/presentation.rs48
-rw-r--r--crates/ra_ide/src/display/function_signature.rs3
4 files changed, 47 insertions, 48 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs
index c16357a7e..44288f92e 100644
--- a/crates/ra_ide/src/completion/complete_dot.rs
+++ b/crates/ra_ide/src/completion/complete_dot.rs
@@ -7,11 +7,9 @@ use crate::{
7 completion_context::CompletionContext, 7 completion_context::CompletionContext,
8 completion_item::{CompletionKind, Completions}, 8 completion_item::{CompletionKind, Completions},
9 }, 9 },
10 // CallInfo,
11 CompletionItem, 10 CompletionItem,
12}; 11};
13use rustc_hash::FxHashSet; 12use rustc_hash::FxHashSet;
14// use std::cmp::Ordering;
15 13
16/// Complete dot accesses, i.e. fields or methods (and .await syntax). 14/// Complete dot accesses, i.e. fields or methods (and .await syntax).
17pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { 15pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs
index a3ae9c86b..8b96b81db 100644
--- a/crates/ra_ide/src/completion/completion_item.rs
+++ b/crates/ra_ide/src/completion/completion_item.rs
@@ -305,46 +305,6 @@ impl Builder {
305 self 305 self
306 } 306 }
307 #[allow(unused)] 307 #[allow(unused)]
308 pub(crate) fn compute_score(mut self, ctx: &CompletionContext) -> Builder {
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 self;
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 (
323 call_info.active_parameter_name().unwrap(),
324 call_info.active_parameter_type().unwrap(),
325 )
326 } else {
327 return self;
328 }
329 } else {
330 return self;
331 };
332
333 // Compute score
334 // For the same type
335 if let Some(a_parameter_type) = &self.detail {
336 if &active_type == a_parameter_type {
337 // If same type + same name then go top position
338 if active_name == self.label {
339 return self.set_score(CompletionScore::TypeAndNameMatch);
340 } else {
341 return self.set_score(CompletionScore::TypeMatch);
342 }
343 }
344 }
345
346 self
347 }
348 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder { 308 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder {
349 self.score = Some(score); 309 self.score = Some(score);
350 self 310 self
@@ -363,7 +323,9 @@ impl<'a> Into<CompletionItem> for Builder {
363 323
364#[derive(Debug, Clone)] 324#[derive(Debug, Clone)]
365pub enum CompletionScore { 325pub enum CompletionScore {
326 /// If only type match
366 TypeMatch, 327 TypeMatch,
328 /// If type and name match
367 TypeAndNameMatch, 329 TypeAndNameMatch,
368} 330}
369 331
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),
diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs
index 2d175882b..b5e2785fe 100644
--- a/crates/ra_ide/src/display/function_signature.rs
+++ b/crates/ra_ide/src/display/function_signature.rs
@@ -73,7 +73,7 @@ impl FunctionSignature {
73 if let Some(param_type) = raw_param.split(':').nth(1) { 73 if let Some(param_type) = raw_param.split(':').nth(1) {
74 parameter_types.push(param_type[1..].to_string()); 74 parameter_types.push(param_type[1..].to_string());
75 } else { 75 } else {
76 // The unwrap_or_else is useful when you have tuple struct 76 // useful when you have tuple struct
77 parameter_types.push(raw_param.clone()); 77 parameter_types.push(raw_param.clone());
78 } 78 }
79 params.push(raw_param); 79 params.push(raw_param);
@@ -177,7 +177,6 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
177 has_self_param = true; 177 has_self_param = true;
178 let raw_param = self_param.syntax().text().to_string(); 178 let raw_param = self_param.syntax().text().to_string();
179 179
180 // FIXME: better solution ?
181 res_types.push( 180 res_types.push(
182 raw_param.split(':').nth(1).unwrap_or_else(|| " Self")[1..].to_string(), 181 raw_param.split(':').nth(1).unwrap_or_else(|| " Self")[1..].to_string(),
183 ); 182 );