diff options
Diffstat (limited to 'crates/ra_ide/src/completion/completion_item.rs')
-rw-r--r-- | crates/ra_ide/src/completion/completion_item.rs | 74 |
1 files changed, 45 insertions, 29 deletions
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index f8e6e53f1..c9c3fdc0e 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs | |||
@@ -2,9 +2,10 @@ | |||
2 | 2 | ||
3 | use std::{cmp::Ordering, fmt}; | 3 | use std::{cmp::Ordering, fmt}; |
4 | 4 | ||
5 | use super::completion_context::CompletionContext; | ||
5 | use crate::CallInfo; | 6 | use crate::CallInfo; |
6 | use hir::Documentation; | 7 | use hir::{Documentation, HirDisplay}; |
7 | use ra_syntax::TextRange; | 8 | use ra_syntax::{ast::RecordField, TextRange}; |
8 | use ra_text_edit::TextEdit; | 9 | use ra_text_edit::TextEdit; |
9 | 10 | ||
10 | /// `CompletionItem` describes a single completion variant in the editor pop-up. | 11 | /// `CompletionItem` describes a single completion variant in the editor pop-up. |
@@ -301,7 +302,7 @@ impl<'a> Into<CompletionItem> for Builder { | |||
301 | #[derive(Debug)] | 302 | #[derive(Debug)] |
302 | pub(crate) enum SortOption { | 303 | pub(crate) enum SortOption { |
303 | CallFn(CallInfo), | 304 | CallFn(CallInfo), |
304 | // LitStruct, | 305 | RecordField(RecordField), |
305 | } | 306 | } |
306 | 307 | ||
307 | /// Represents an in-progress set of completions being built. | 308 | /// Represents an in-progress set of completions being built. |
@@ -327,40 +328,55 @@ impl Completions { | |||
327 | self.sort_option = Some(sort_option); | 328 | self.sort_option = Some(sort_option); |
328 | } | 329 | } |
329 | 330 | ||
330 | pub(crate) fn sort(&mut self) { | 331 | pub(crate) fn sort(&mut self, ctx: &CompletionContext) { |
331 | if self.sort_option.is_none() { | 332 | if self.sort_option.is_none() { |
332 | return; | 333 | return; |
333 | } | 334 | } |
334 | let sort_option = self.sort_option.as_ref().unwrap(); | ||
335 | 335 | ||
336 | match sort_option { | 336 | let (active_name, active_type) = match self.sort_option.as_ref().unwrap() { |
337 | SortOption::CallFn(call_info) => { | 337 | SortOption::CallFn(call_info) => { |
338 | if let Some(active_parameter_type) = call_info.active_parameter_type() { | 338 | if call_info.active_parameter_type().is_none() |
339 | let active_parameter_name = call_info.active_parameter_name().unwrap(); | 339 | || call_info.active_parameter_name().is_none() |
340 | 340 | { | |
341 | self.buf.sort_by(|a, b| { | 341 | return; |
342 | // For the same type | 342 | } |
343 | if let Some(a_parameter_type) = &a.detail { | 343 | ( |
344 | if &active_parameter_type == a_parameter_type { | 344 | call_info.active_parameter_name().unwrap(), |
345 | // If same type + same name then go top position | 345 | call_info.active_parameter_type().unwrap(), |
346 | if active_parameter_name != a.label { | 346 | ) |
347 | if let Some(b_parameter_type) = &b.detail { | 347 | } |
348 | if &active_parameter_type == b_parameter_type { | 348 | SortOption::RecordField(record_field) => { |
349 | return Ordering::Equal; | 349 | if let Some((struct_field, _)) = ctx.sema.resolve_record_field(record_field) { |
350 | } | 350 | ( |
351 | } | 351 | struct_field.name(ctx.db).to_string(), |
352 | } | 352 | struct_field.signature_ty(ctx.db).display(ctx.db).to_string(), |
353 | Ordering::Less | 353 | ) |
354 | } else { | 354 | } else { |
355 | Ordering::Greater | 355 | return; |
356 | } | ||
357 | } | ||
358 | }; | ||
359 | |||
360 | self.buf.sort_by(|a, b| { | ||
361 | // For the same type | ||
362 | if let Some(a_parameter_type) = &a.detail { | ||
363 | if &active_type == a_parameter_type { | ||
364 | // If same type + same name then go top position | ||
365 | if active_name != a.label { | ||
366 | if let Some(b_parameter_type) = &b.detail { | ||
367 | if &active_type == b_parameter_type { | ||
368 | return Ordering::Equal; | ||
356 | } | 369 | } |
357 | } else { | ||
358 | Ordering::Greater | ||
359 | } | 370 | } |
360 | }); | 371 | } |
372 | Ordering::Less | ||
373 | } else { | ||
374 | Ordering::Greater | ||
361 | } | 375 | } |
362 | } // _ => unimplemented!("sort options not already implemented"), | 376 | } else { |
363 | } | 377 | Ordering::Greater |
378 | } | ||
379 | }); | ||
364 | } | 380 | } |
365 | } | 381 | } |
366 | 382 | ||