Auto merge of #12173 - Veykril:completion-rev, r=Veykril

internal: completion PathKind is not optional
This commit is contained in:
bors 2022-05-06 10:06:06 +00:00
commit 616796a2c0
12 changed files with 40 additions and 33 deletions

View File

@ -71,7 +71,7 @@ pub(crate) fn complete_known_attribute_input(
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Attr { kind, annotated_item_kind }),
kind: PathKind::Attr { kind, annotated_item_kind },
is_absolute_path,
ref qualifier,
..

View File

@ -13,10 +13,7 @@
pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let (qualifier, is_absolute_path) = match ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Derive),
ref qualifier,
is_absolute_path,
..
kind: PathKind::Derive, ref qualifier, is_absolute_path, ..
}) => (qualifier, is_absolute_path),
_ => return,
};

View File

@ -2,7 +2,11 @@
use ide_db::FxHashSet;
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
use crate::{
context::{CompletionContext, PathCompletionCtx, PathKind},
patterns::ImmediateLocation,
Completions,
};
/// Complete dot accesses, i.e. fields or methods.
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
@ -34,9 +38,16 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.config.enable_self_on_the_fly {
return;
}
if ctx.is_non_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
return;
match ctx.path_context {
Some(PathCompletionCtx {
is_absolute_path: false,
qualifier: None,
kind: PathKind::Expr,
..
}) if !ctx.is_path_disallowed() => {}
_ => return,
}
if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
if let Some(self_) = func.self_param(ctx.db) {
let ty = self_.ty(ctx.db);

View File

@ -15,9 +15,9 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
}
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Expr), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Expr, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

View File

@ -13,9 +13,9 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
}
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Item), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Item, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

View File

@ -17,9 +17,9 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
}
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Type), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Type, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

View File

@ -12,9 +12,9 @@
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Use), is_absolute_path, qualifier, ..
}) => (is_absolute_path, qualifier),
Some(PathCompletionCtx { kind: PathKind::Use, is_absolute_path, qualifier, .. }) => {
(is_absolute_path, qualifier)
}
_ => return,
};

View File

@ -10,7 +10,7 @@
pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
let (&is_absolute_path, qualifier, &has_in_token) = match &ctx.path_context {
Some(PathCompletionCtx {
kind: Some(PathKind::Vis { has_in_token }),
kind: PathKind::Vis { has_in_token },
is_absolute_path,
qualifier,
..

View File

@ -75,8 +75,7 @@ pub(crate) struct PathCompletionCtx {
// FIXME: use this
/// The parent of the path we are completing.
pub(super) parent: Option<ast::Path>,
// FIXME: This should be PathKind, the none case should never occur
pub(super) kind: Option<PathKind>,
pub(super) kind: PathKind,
/// Whether the path segment has type args or not.
pub(super) has_type_args: bool,
/// `true` if we are a statement or a last expr in the block.
@ -315,11 +314,11 @@ pub(crate) fn is_path_disallowed(&self) -> bool {
}
pub(crate) fn expects_expression(&self) -> bool {
matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Expr), .. }))
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Expr, .. }))
}
pub(crate) fn expects_type(&self) -> bool {
matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Type), .. }))
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Type, .. }))
}
pub(crate) fn path_is_call(&self) -> bool {
@ -341,7 +340,7 @@ pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
}
pub(crate) fn path_kind(&self) -> Option<PathKind> {
self.path_context.as_ref().and_then(|it| it.kind)
self.path_context.as_ref().map(|it| it.kind)
}
pub(crate) fn is_immediately_after_macro_bang(&self) -> bool {
@ -837,7 +836,7 @@ fn fill(
Self::classify_name_ref(&self.sema, &original_file, name_ref)
{
self.path_context =
Some(PathCompletionCtx { kind: Some(PathKind::Derive), ..path_ctx });
Some(PathCompletionCtx { kind: PathKind::Derive, ..path_ctx });
}
}
return;
@ -969,7 +968,7 @@ fn classify_name_ref(
is_absolute_path: false,
qualifier: None,
parent: path.parent_path(),
kind: None,
kind: PathKind::Item,
has_type_args: false,
can_be_stmt: false,
in_loop_body: false,
@ -1041,7 +1040,7 @@ fn classify_name_ref(
}
};
Some(kind)
}).flatten();
}).flatten()?;
path_ctx.has_type_args = segment.generic_arg_list().is_some();
if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {

View File

@ -273,7 +273,7 @@ fn render_resolution_simple_(
// Add `<>` for generic types
let type_path_no_ty_args = matches!(
ctx.completion.path_context,
Some(PathCompletionCtx { kind: Some(PathKind::Type), has_type_args: false, .. })
Some(PathCompletionCtx { kind: PathKind::Type, has_type_args: false, .. })
) && ctx.completion.config.add_call_parenthesis;
if type_path_no_ty_args {
if let Some(cap) = ctx.snippet_cap() {

View File

@ -197,10 +197,10 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
}
match ctx.path_context {
Some(PathCompletionCtx { kind: Some(PathKind::Expr), has_call_parens: true, .. }) => {
Some(PathCompletionCtx { kind: PathKind::Expr, has_call_parens: true, .. }) => {
return false
}
Some(PathCompletionCtx { kind: Some(PathKind::Use | PathKind::Type), .. }) => {
Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type, .. }) => {
cov_mark::hit!(no_parens_in_use_item);
return false;
}

View File

@ -35,7 +35,7 @@ fn render(
let needs_bang = match completion.path_context {
Some(PathCompletionCtx { kind, has_macro_bang, .. }) => {
is_fn_like && kind != Some(PathKind::Use) && !has_macro_bang
is_fn_like && kind != PathKind::Use && !has_macro_bang
}
_ => is_fn_like,
};