From 9271941a950026836511bd1c85e15e26a480b824 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 2 Jun 2021 15:21:18 +0200 Subject: Add MethodCall and FieldAccess variants to ImmediateLocation --- crates/ide_completion/src/completions/dot.rs | 7 +++++-- crates/ide_completion/src/completions/flyimport.rs | 6 +++--- crates/ide_completion/src/completions/keyword.rs | 2 +- crates/ide_completion/src/completions/postfix.rs | 17 ++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) (limited to 'crates/ide_completion/src/completions') diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs index 302c9ccbd..e0a7021fd 100644 --- a/crates/ide_completion/src/completions/dot.rs +++ b/crates/ide_completion/src/completions/dot.rs @@ -8,7 +8,7 @@ use crate::{context::CompletionContext, Completions}; /// Complete dot accesses, i.e. fields or methods. pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { - let dot_receiver = match &ctx.dot_receiver { + let dot_receiver = match ctx.dot_receiver() { Some(expr) => expr, _ => return complete_undotted_self(acc, ctx), }; @@ -30,7 +30,10 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { } fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) { - if !ctx.is_trivial_path || !ctx.config.enable_self_on_the_fly { + if !ctx.config.enable_self_on_the_fly { + return; + } + if !ctx.is_trivial_path || ctx.is_path_disallowed() { return; } ctx.scope.process_all_names(&mut |name, def| { diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index df27e7a84..d72bf13d3 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -162,19 +162,19 @@ pub(crate) fn position_for_import<'a>( Some(match import_candidate { Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(), Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(), - Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver.as_ref()?.syntax(), + Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(), None => ctx .name_ref_syntax .as_ref() .map(|name_ref| name_ref.syntax()) .or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax())) - .or_else(|| ctx.dot_receiver.as_ref().map(|expr| expr.syntax()))?, + .or_else(|| ctx.dot_receiver().map(|expr| expr.syntax()))?, }) } fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option { let current_module = ctx.scope.module()?; - if let Some(dot_receiver) = &ctx.dot_receiver { + if let Some(dot_receiver) = ctx.dot_receiver() { ImportAssets::for_fuzzy_method_call( current_module, ctx.sema.type_of_expr(dot_receiver)?, diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs index 0d035c611..1a7a484a4 100644 --- a/crates/ide_completion/src/completions/keyword.rs +++ b/crates/ide_completion/src/completions/keyword.rs @@ -31,7 +31,7 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC } // Suggest .await syntax for types that implement Future trait - if let Some(receiver) = &ctx.dot_receiver { + if let Some(receiver) = ctx.dot_receiver() { if let Some(ty) = ctx.sema.type_of_expr(receiver) { if ty.impls_future(ctx.db) { let mut item = kw_completion("await"); diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs index 962aaf0df..86bbb58e2 100644 --- a/crates/ide_completion/src/completions/postfix.rs +++ b/crates/ide_completion/src/completions/postfix.rs @@ -14,6 +14,7 @@ use crate::{ completions::postfix::format_like::add_format_like_completions, context::CompletionContext, item::{Builder, CompletionKind}, + patterns::ImmediateLocation, CompletionItem, CompletionItemKind, Completions, }; @@ -22,13 +23,16 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { return; } - let dot_receiver = match &ctx.dot_receiver { - Some(it) => it, - None => return, + let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location { + Some(ImmediateLocation::MethodCall { receiver: Some(it) }) => (it, false), + Some(ImmediateLocation::FieldAccess { + receiver: Some(it), + receiver_is_ambiguous_float_literal, + }) => (it, *receiver_is_ambiguous_float_literal), + _ => return, }; - let receiver_text = - get_receiver_text(dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); + let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal); let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { Some(it) => it, @@ -123,8 +127,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { // The rest of the postfix completions create an expression that moves an argument, // so it's better to consider references now to avoid breaking the compilation let dot_receiver = include_references(dot_receiver); - let receiver_text = - get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); + let receiver_text = get_receiver_text(&dot_receiver, receiver_is_ambiguous_float_literal); match try_enum { Some(try_enum) => match try_enum { -- cgit v1.2.3