diff options
Diffstat (limited to 'crates/ra_ide/src/completion/completion_item.rs')
-rw-r--r-- | crates/ra_ide/src/completion/completion_item.rs | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index bc0f1aff5..f8e6e53f1 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs | |||
@@ -1,7 +1,8 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use std::fmt; | 3 | use std::{cmp::Ordering, fmt}; |
4 | 4 | ||
5 | use crate::CallInfo; | ||
5 | use hir::Documentation; | 6 | use hir::Documentation; |
6 | use ra_syntax::TextRange; | 7 | use ra_syntax::TextRange; |
7 | use ra_text_edit::TextEdit; | 8 | use ra_text_edit::TextEdit; |
@@ -297,10 +298,17 @@ impl<'a> Into<CompletionItem> for Builder { | |||
297 | } | 298 | } |
298 | } | 299 | } |
299 | 300 | ||
301 | #[derive(Debug)] | ||
302 | pub(crate) enum SortOption { | ||
303 | CallFn(CallInfo), | ||
304 | // LitStruct, | ||
305 | } | ||
306 | |||
300 | /// Represents an in-progress set of completions being built. | 307 | /// Represents an in-progress set of completions being built. |
301 | #[derive(Debug, Default)] | 308 | #[derive(Debug, Default)] |
302 | pub(crate) struct Completions { | 309 | pub(crate) struct Completions { |
303 | buf: Vec<CompletionItem>, | 310 | buf: Vec<CompletionItem>, |
311 | sort_option: Option<SortOption>, | ||
304 | } | 312 | } |
305 | 313 | ||
306 | impl Completions { | 314 | impl Completions { |
@@ -314,6 +322,46 @@ impl Completions { | |||
314 | { | 322 | { |
315 | items.into_iter().for_each(|item| self.add(item.into())) | 323 | items.into_iter().for_each(|item| self.add(item.into())) |
316 | } | 324 | } |
325 | |||
326 | pub(crate) fn with_sort_option(&mut self, sort_option: SortOption) { | ||
327 | self.sort_option = Some(sort_option); | ||
328 | } | ||
329 | |||
330 | pub(crate) fn sort(&mut self) { | ||
331 | if self.sort_option.is_none() { | ||
332 | return; | ||
333 | } | ||
334 | let sort_option = self.sort_option.as_ref().unwrap(); | ||
335 | |||
336 | match sort_option { | ||
337 | SortOption::CallFn(call_info) => { | ||
338 | if let Some(active_parameter_type) = call_info.active_parameter_type() { | ||
339 | let active_parameter_name = call_info.active_parameter_name().unwrap(); | ||
340 | |||
341 | self.buf.sort_by(|a, b| { | ||
342 | // For the same type | ||
343 | if let Some(a_parameter_type) = &a.detail { | ||
344 | if &active_parameter_type == a_parameter_type { | ||
345 | // If same type + same name then go top position | ||
346 | if active_parameter_name != a.label { | ||
347 | if let Some(b_parameter_type) = &b.detail { | ||
348 | if &active_parameter_type == b_parameter_type { | ||
349 | return Ordering::Equal; | ||
350 | } | ||
351 | } | ||
352 | } | ||
353 | Ordering::Less | ||
354 | } else { | ||
355 | Ordering::Greater | ||
356 | } | ||
357 | } else { | ||
358 | Ordering::Greater | ||
359 | } | ||
360 | }); | ||
361 | } | ||
362 | } // _ => unimplemented!("sort options not already implemented"), | ||
363 | } | ||
364 | } | ||
317 | } | 365 | } |
318 | 366 | ||
319 | impl Into<Vec<CompletionItem>> for Completions { | 367 | impl Into<Vec<CompletionItem>> for Completions { |