diff options
author | Benjamin Coenen <[email protected]> | 2020-04-21 13:31:57 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-04-21 13:31:57 +0100 |
commit | 1c3a1385a587f0713908c0ae888ffad31f13de11 (patch) | |
tree | c1f1ea8d9575670d36c09bc7a9c0c0e34fee89d5 /crates/ra_ide/src/completion | |
parent | af3c19e85f55db9277ce9ad5b784df2ccfe3c9e4 (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')
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/completion_item.rs | 42 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 48 |
3 files changed, 46 insertions, 46 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 | }; |
13 | use rustc_hash::FxHashSet; | 12 | use 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). |
17 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 15 | pub(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)] |
365 | pub enum CompletionScore { | 325 | pub 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; | |||
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,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 | ||
302 | pub(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 | |||
298 | enum Params { | 338 | enum Params { |
299 | Named(Vec<String>), | 339 | Named(Vec<String>), |
300 | Anonymous(usize), | 340 | Anonymous(usize), |