mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
internal: Simplify DotAccess representation in completions
This commit is contained in:
parent
d5965aa871
commit
697ade6f8d
4 changed files with 57 additions and 53 deletions
|
@ -3,30 +3,31 @@
|
||||||
use ide_db::FxHashSet;
|
use ide_db::FxHashSet;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::{CompletionContext, DotAccess, NameRefContext, PathCompletionCtx, PathKind},
|
context::{
|
||||||
Completions,
|
CompletionContext, DotAccess, DotAccessKind, NameRefContext, PathCompletionCtx, PathKind,
|
||||||
|
},
|
||||||
|
CompletionItem, CompletionItemKind, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Complete dot accesses, i.e. fields or methods.
|
/// Complete dot accesses, i.e. fields or methods.
|
||||||
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
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 {
|
Some(NameRefContext {
|
||||||
dot_access:
|
dot_access: Some(access @ DotAccess { receiver_ty: Some(receiver_ty), .. }),
|
||||||
Some(
|
|
||||||
access @ (DotAccess::Method { receiver: Some(receiver), .. }
|
|
||||||
| DotAccess::Field { receiver: Some(receiver), .. }),
|
|
||||||
),
|
|
||||||
..
|
..
|
||||||
}) => (access, receiver),
|
}) => (access, &receiver_ty.original),
|
||||||
_ => return complete_undotted_self(acc, ctx),
|
_ => return complete_undotted_self(acc, ctx),
|
||||||
};
|
};
|
||||||
|
|
||||||
let receiver_ty = match ctx.sema.type_of_expr(dot_receiver) {
|
// Suggest .await syntax for types that implement Future trait
|
||||||
Some(ty) => ty.original,
|
if receiver_ty.impls_future(ctx.db) {
|
||||||
_ => return,
|
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);
|
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
||||||
} else {
|
} else {
|
||||||
complete_fields(
|
complete_fields(
|
||||||
|
|
|
@ -13,7 +13,7 @@ use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completions::postfix::format_like::add_format_like_completions,
|
completions::postfix::format_like::add_format_like_completions,
|
||||||
context::{CompletionContext, DotAccess, NameRefContext},
|
context::{CompletionContext, DotAccess, DotAccessKind, NameRefContext},
|
||||||
item::{Builder, CompletionRelevancePostfixMatch},
|
item::{Builder, CompletionRelevancePostfixMatch},
|
||||||
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
|
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
|
||||||
};
|
};
|
||||||
|
@ -23,34 +23,25 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
return;
|
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 {
|
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 {
|
it,
|
||||||
dot_access:
|
&ty.original,
|
||||||
Some(DotAccess::Field { receiver: Some(it), receiver_is_ambiguous_float_literal }),
|
match *kind {
|
||||||
..
|
DotAccessKind::Field { receiver_is_ambiguous_float_literal } => {
|
||||||
}) => (it, *receiver_is_ambiguous_float_literal),
|
receiver_is_ambiguous_float_literal
|
||||||
|
}
|
||||||
|
DotAccessKind::Method { .. } => false,
|
||||||
|
},
|
||||||
|
),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let receiver_text = get_receiver_text(dot_receiver, 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.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 {
|
let cap = match ctx.config.snippet_cap {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
|
|
|
@ -186,15 +186,20 @@ pub(super) enum IdentContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[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 {
|
Field {
|
||||||
receiver: Option<ast::Expr>,
|
|
||||||
/// True if the receiver is an integer and there is no ident in the original file after it yet
|
/// True if the receiver is an integer and there is no ident in the original file after it yet
|
||||||
/// like `0.$0`
|
/// like `0.$0`
|
||||||
receiver_is_ambiguous_float_literal: bool,
|
receiver_is_ambiguous_float_literal: bool,
|
||||||
},
|
},
|
||||||
Method {
|
Method {
|
||||||
receiver: Option<ast::Expr>,
|
|
||||||
has_parens: bool,
|
has_parens: bool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -298,11 +303,9 @@ impl<'a> CompletionContext<'a> {
|
||||||
|
|
||||||
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
|
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
|
||||||
match self.nameref_ctx() {
|
match self.nameref_ctx() {
|
||||||
Some(NameRefContext {
|
Some(NameRefContext { dot_access: Some(DotAccess { receiver, .. }), .. }) => {
|
||||||
dot_access:
|
receiver.as_ref()
|
||||||
Some(DotAccess::Method { receiver, .. } | DotAccess::Field { receiver, .. }),
|
}
|
||||||
..
|
|
||||||
}) => receiver.as_ref(),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1073,16 +1076,20 @@ impl<'a> CompletionContext<'a> {
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => 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);
|
return (nameref_ctx, None);
|
||||||
},
|
},
|
||||||
ast::MethodCallExpr(method) => {
|
ast::MethodCallExpr(method) => {
|
||||||
nameref_ctx.dot_access = Some(
|
let receiver = find_in_original_file(method.receiver(), original_file);
|
||||||
DotAccess::Method {
|
nameref_ctx.dot_access = Some(DotAccess {
|
||||||
receiver: find_in_original_file(method.receiver(), original_file),
|
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
|
||||||
has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some())
|
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);
|
||||||
},
|
},
|
||||||
_ => return (nameref_ctx, None),
|
_ => return (nameref_ctx, None),
|
||||||
|
|
|
@ -7,7 +7,9 @@ use stdx::{format_to, to_lower_snake_case};
|
||||||
use syntax::SmolStr;
|
use syntax::SmolStr;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::{CompletionContext, DotAccess, NameRefContext, PathCompletionCtx, PathKind},
|
context::{
|
||||||
|
CompletionContext, DotAccess, DotAccessKind, NameRefContext, PathCompletionCtx, PathKind,
|
||||||
|
},
|
||||||
item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance},
|
item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance},
|
||||||
render::{compute_exact_name_match, compute_ref_match, compute_type_match, RenderContext},
|
render::{compute_exact_name_match, compute_ref_match, compute_type_match, RenderContext},
|
||||||
CallableSnippets,
|
CallableSnippets,
|
||||||
|
@ -209,7 +211,10 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
|
||||||
|
|
||||||
if matches!(
|
if matches!(
|
||||||
ctx.nameref_ctx(),
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue