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/context.rs | 51 ++++++++++++++---------------------- 1 file changed, 19 insertions(+), 32 deletions(-) (limited to 'crates/ide_completion/src/context.rs') diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 7c46c815d..eeb4333f8 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -80,9 +80,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) is_expr: bool, /// Something is typed at the "top" level, in module or impl/trait. pub(super) is_new_item: bool, - /// The receiver if this is a field or method access, i.e. writing something.$0 - pub(super) dot_receiver: Option, - pub(super) dot_receiver_is_ambiguous_float_literal: bool, /// If this is a call (method or function) in particular, i.e. the () are already there. pub(super) is_call: bool, /// Like `is_call`, but for tuple patterns. @@ -159,8 +156,6 @@ impl<'a> CompletionContext<'a> { can_be_stmt: false, is_expr: false, is_new_item: false, - dot_receiver: None, - dot_receiver_is_ambiguous_float_literal: false, is_call: false, is_pattern_call: false, is_macro_call: false, @@ -255,6 +250,22 @@ impl<'a> CompletionContext<'a> { ) } + pub(crate) fn has_dot_receiver(&self) -> bool { + matches!( + &self.completion_location, + Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver }) + if receiver.is_some() + ) + } + + pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> { + match &self.completion_location { + Some(ImmediateLocation::MethodCall { receiver }) + | Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(), + _ => None, + } + } + pub(crate) fn expects_use_tree(&self) -> bool { matches!(self.completion_location, Some(ImmediateLocation::Use)) } @@ -267,6 +278,7 @@ impl<'a> CompletionContext<'a> { matches!(self.completion_location, Some(ImmediateLocation::ItemList)) } + // fn expects_value(&self) -> bool { pub(crate) fn expects_expression(&self) -> bool { self.is_expr } @@ -623,33 +635,8 @@ impl<'a> CompletionContext<'a> { .unwrap_or(false); self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some(); } - - if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { - // The receiver comes before the point of insertion of the fake - // ident, so it should have the same range in the non-modified file - self.dot_receiver = field_expr - .expr() - .map(|e| e.syntax().text_range()) - .and_then(|r| find_node_with_range(original_file, r)); - self.dot_receiver_is_ambiguous_float_literal = - if let Some(ast::Expr::Literal(l)) = &self.dot_receiver { - match l.kind() { - ast::LiteralKind::FloatNumber { .. } => l.token().text().ends_with('.'), - _ => false, - } - } else { - false - }; - } - - if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { - // As above - self.dot_receiver = method_call_expr - .receiver() - .map(|e| e.syntax().text_range()) - .and_then(|r| find_node_with_range(original_file, r)); - self.is_call = true; - } + self.is_call |= + matches!(self.completion_location, Some(ImmediateLocation::MethodCall { .. })); } } -- cgit v1.2.3