From 064095742980d4c825391f643e437520599f51d8 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sun, 12 Apr 2020 17:45:07 +0200 Subject: Improve autocompletion by looking on the type and name, change implementation, include sort in Completions struct Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_ide/src/completion/complete_dot.rs | 69 ++++++++++++++++++---------- 1 file changed, 44 insertions(+), 25 deletions(-) (limited to 'crates/ra_ide/src/completion/complete_dot.rs') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index b5448af5c..cb899d8ff 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -1,14 +1,23 @@ //! FIXME: write short doc here -use hir::{HasVisibility, HirDisplay, Type}; +use hir::{ + HasVisibility, + // HirDisplay, + Type, +}; use crate::completion::completion_item::CompletionKind; use crate::{ - completion::{completion_context::CompletionContext, completion_item::Completions}, + call_info::call_info, + completion::{ + completion_context::CompletionContext, + completion_item::{Completions, SortOption}, + }, + // CallInfo, CompletionItem, }; use rustc_hash::FxHashSet; -use std::cmp::Ordering; +// use std::cmp::Ordering; /// Complete dot accesses, i.e. fields or methods (and .await syntax). pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { @@ -38,30 +47,40 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { for receiver in receiver.autoderef(ctx.db) { - let mut fields = receiver.fields(ctx.db); - if let Some(call_info) = &ctx.call_info { - if let Some(active_parameter_type) = call_info.active_parameter_type() { - let active_parameter_name = call_info.active_parameter_name().unwrap(); - fields.sort_by(|a, b| { - // For the same type - if active_parameter_type == a.1.display(ctx.db).to_string() { - // If same type + same name then go top position - if active_parameter_name == a.0.name(ctx.db).to_string() { - Ordering::Less - } else { - if active_parameter_type == b.1.display(ctx.db).to_string() { - Ordering::Equal - } else { - Ordering::Less - } - } - } else { - Ordering::Greater - } - }); - } + let fields = receiver.fields(ctx.db); + + // If we use this implementation we can delete call_info in the CompletionContext + if let Some(call_info) = call_info(ctx.db, ctx.file_position) { + acc.with_sort_option(SortOption::CallFn(call_info)); } + // // For Call Fn + // if let Some(call_info) = &ctx.call_info { + // if let Some(active_parameter_type) = call_info.active_parameter_type() { + // let active_parameter_name = call_info.active_parameter_name().unwrap(); + // fields.sort_by(|a, b| { + // // For the same type + // if active_parameter_type == a.1.display(ctx.db).to_string() { + // // If same type + same name then go top position + // if active_parameter_name == a.0.name(ctx.db).to_string() { + // Ordering::Less + // } else { + // if active_parameter_type == b.1.display(ctx.db).to_string() { + // Ordering::Equal + // } else { + // Ordering::Less + // } + // } + // } else { + // Ordering::Greater + // } + // }); + // } + // } + + // For Lit struct fields + // --- + for (field, ty) in fields { if ctx.scope().module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) { // Skip private field. FIXME: If the definition location of the -- cgit v1.2.3