internal: Simplify DotAccess representation in completions

This commit is contained in:
Lukas Wirth 2022-05-23 17:40:41 +02:00
parent d5965aa871
commit 697ade6f8d
4 changed files with 57 additions and 53 deletions

View File

@ -3,30 +3,31 @@
use ide_db::FxHashSet;
use crate::{
context::{CompletionContext, DotAccess, NameRefContext, PathCompletionCtx, PathKind},
Completions,
context::{
CompletionContext, DotAccess, DotAccessKind, NameRefContext, PathCompletionCtx, PathKind,
},
CompletionItem, CompletionItemKind, Completions,
};
/// Complete dot accesses, i.e. fields or methods.
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
let (dot_access, dot_receiver) = match ctx.nameref_ctx() {
let (dot_access, receiver_ty) = match ctx.nameref_ctx() {
Some(NameRefContext {
dot_access:
Some(
access @ (DotAccess::Method { receiver: Some(receiver), .. }
| DotAccess::Field { receiver: Some(receiver), .. }),
),
dot_access: Some(access @ DotAccess { receiver_ty: Some(receiver_ty), .. }),
..
}) => (access, receiver),
}) => (access, &receiver_ty.original),
_ => return complete_undotted_self(acc, ctx),
};
let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
Some(ty) => ty.original,
_ => return,
};
// Suggest .await syntax for types that implement Future trait
if receiver_ty.impls_future(ctx.db) {
let mut item =
CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
item.detail("expr.await");
item.add_to(acc);
}
if let DotAccess::Method { .. } = dot_access {
if let DotAccessKind::Method { .. } = dot_access.kind {
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
} else {
complete_fields(

View File

@ -13,7 +13,7 @@ use text_edit::TextEdit;
use crate::{
completions::postfix::format_like::add_format_like_completions,
context::{CompletionContext, DotAccess, NameRefContext},
context::{CompletionContext, DotAccess, DotAccessKind, NameRefContext},
item::{Builder, CompletionRelevancePostfixMatch},
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
};
@ -23,34 +23,25 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
return;
}
let (dot_receiver, receiver_is_ambiguous_float_literal) = match ctx.nameref_ctx() {
let (dot_receiver, receiver_ty, receiver_is_ambiguous_float_literal) = match ctx.nameref_ctx() {
Some(NameRefContext {
dot_access: Some(DotAccess::Method { receiver: Some(it), .. }),
dot_access: Some(DotAccess { receiver_ty: Some(ty), receiver: Some(it), kind, .. }),
..
}) => (it, false),
Some(NameRefContext {
dot_access:
Some(DotAccess::Field { receiver: Some(it), receiver_is_ambiguous_float_literal }),
..
}) => (it, *receiver_is_ambiguous_float_literal),
}) => (
it,
&ty.original,
match *kind {
DotAccessKind::Field { receiver_is_ambiguous_float_literal } => {
receiver_is_ambiguous_float_literal
}
DotAccessKind::Method { .. } => false,
},
),
_ => return,
};
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.original,
None => return,
};
// Suggest .await syntax for types that implement Future trait
if receiver_ty.impls_future(ctx.db) {
let mut item =
CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
item.detail("expr.await");
item.add_to(acc);
}
let cap = match ctx.config.snippet_cap {
Some(it) => it,
None => return,

View File

@ -186,15 +186,20 @@ pub(super) enum IdentContext {
}
#[derive(Debug)]
pub(super) enum DotAccess {
pub(super) struct DotAccess {
pub(super) receiver: Option<ast::Expr>,
pub(super) receiver_ty: Option<TypeInfo>,
pub(super) kind: DotAccessKind,
}
#[derive(Debug)]
pub(super) enum DotAccessKind {
Field {
receiver: Option<ast::Expr>,
/// True if the receiver is an integer and there is no ident in the original file after it yet
/// like `0.$0`
receiver_is_ambiguous_float_literal: bool,
},
Method {
receiver: Option<ast::Expr>,
has_parens: bool,
},
}
@ -298,11 +303,9 @@ impl<'a> CompletionContext<'a> {
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
match self.nameref_ctx() {
Some(NameRefContext {
dot_access:
Some(DotAccess::Method { receiver, .. } | DotAccess::Field { receiver, .. }),
..
}) => receiver.as_ref(),
Some(NameRefContext { dot_access: Some(DotAccess { receiver, .. }), .. }) => {
receiver.as_ref()
}
_ => None,
}
}
@ -1073,16 +1076,20 @@ impl<'a> CompletionContext<'a> {
},
_ => false,
};
nameref_ctx.dot_access = Some(DotAccess::Field { receiver, receiver_is_ambiguous_float_literal });
nameref_ctx.dot_access = Some(DotAccess {
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },
receiver
});
return (nameref_ctx, None);
},
ast::MethodCallExpr(method) => {
nameref_ctx.dot_access = Some(
DotAccess::Method {
receiver: find_in_original_file(method.receiver(), original_file),
has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some())
}
);
let receiver = find_in_original_file(method.receiver(), original_file);
nameref_ctx.dot_access = Some(DotAccess {
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
kind: DotAccessKind::Method { has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some()) },
receiver
});
return (nameref_ctx, None);
},
_ => return (nameref_ctx, None),

View File

@ -7,7 +7,9 @@ use stdx::{format_to, to_lower_snake_case};
use syntax::SmolStr;
use crate::{
context::{CompletionContext, DotAccess, NameRefContext, PathCompletionCtx, PathKind},
context::{
CompletionContext, DotAccess, DotAccessKind, NameRefContext, PathCompletionCtx, PathKind,
},
item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance},
render::{compute_exact_name_match, compute_ref_match, compute_type_match, RenderContext},
CallableSnippets,
@ -209,7 +211,10 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
if matches!(
ctx.nameref_ctx(),
Some(NameRefContext { dot_access: Some(DotAccess::Method { has_parens: true, .. }), .. })
Some(NameRefContext {
dot_access: Some(DotAccess { kind: DotAccessKind::Method { has_parens: true }, .. }),
..
})
) {
return false;
}