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) { pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context { let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context {
Some(PathCompletionCtx { Some(PathCompletionCtx {
kind: Some(PathKind::Attr { kind, annotated_item_kind }), kind: PathKind::Attr { kind, annotated_item_kind },
is_absolute_path, is_absolute_path,
ref qualifier, ref qualifier,
.. ..

View File

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

View File

@ -2,7 +2,11 @@
use ide_db::FxHashSet; 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. /// 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) {
@ -34,9 +38,16 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.config.enable_self_on_the_fly { if !ctx.config.enable_self_on_the_fly {
return; return;
} }
if ctx.is_non_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() { match ctx.path_context {
return; 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(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
if let Some(self_) = func.self_param(ctx.db) { if let Some(self_) = func.self_param(ctx.db) {
let ty = self_.ty(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 { let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx { Some(PathCompletionCtx { kind: PathKind::Expr, is_absolute_path, qualifier, .. }) => {
kind: Some(PathKind::Expr), is_absolute_path, qualifier, .. (is_absolute_path, qualifier)
}) => (is_absolute_path, qualifier), }
_ => return, _ => 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 { let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx { Some(PathCompletionCtx { kind: PathKind::Item, is_absolute_path, qualifier, .. }) => {
kind: Some(PathKind::Item), is_absolute_path, qualifier, .. (is_absolute_path, qualifier)
}) => (is_absolute_path, qualifier), }
_ => return, _ => 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 { let (&is_absolute_path, qualifier) = match &ctx.path_context {
Some(PathCompletionCtx { Some(PathCompletionCtx { kind: PathKind::Type, is_absolute_path, qualifier, .. }) => {
kind: Some(PathKind::Type), is_absolute_path, qualifier, .. (is_absolute_path, qualifier)
}) => (is_absolute_path, qualifier), }
_ => return, _ => return,
}; };

View File

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

View File

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

View File

@ -75,8 +75,7 @@ pub(crate) struct PathCompletionCtx {
// FIXME: use this // FIXME: use this
/// The parent of the path we are completing. /// The parent of the path we are completing.
pub(super) parent: Option<ast::Path>, pub(super) parent: Option<ast::Path>,
// FIXME: This should be PathKind, the none case should never occur pub(super) kind: PathKind,
pub(super) kind: Option<PathKind>,
/// Whether the path segment has type args or not. /// Whether the path segment has type args or not.
pub(super) has_type_args: bool, pub(super) has_type_args: bool,
/// `true` if we are a statement or a last expr in the block. /// `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 { 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 { 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 { 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> { 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 { 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::classify_name_ref(&self.sema, &original_file, name_ref)
{ {
self.path_context = self.path_context =
Some(PathCompletionCtx { kind: Some(PathKind::Derive), ..path_ctx }); Some(PathCompletionCtx { kind: PathKind::Derive, ..path_ctx });
} }
} }
return; return;
@ -969,7 +968,7 @@ fn classify_name_ref(
is_absolute_path: false, is_absolute_path: false,
qualifier: None, qualifier: None,
parent: path.parent_path(), parent: path.parent_path(),
kind: None, kind: PathKind::Item,
has_type_args: false, has_type_args: false,
can_be_stmt: false, can_be_stmt: false,
in_loop_body: false, in_loop_body: false,
@ -1041,7 +1040,7 @@ fn classify_name_ref(
} }
}; };
Some(kind) Some(kind)
}).flatten(); }).flatten()?;
path_ctx.has_type_args = segment.generic_arg_list().is_some(); path_ctx.has_type_args = segment.generic_arg_list().is_some();
if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) { 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 // Add `<>` for generic types
let type_path_no_ty_args = matches!( let type_path_no_ty_args = matches!(
ctx.completion.path_context, 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; ) && ctx.completion.config.add_call_parenthesis;
if type_path_no_ty_args { if type_path_no_ty_args {
if let Some(cap) = ctx.snippet_cap() { if let Some(cap) = ctx.snippet_cap() {

View File

@ -197,10 +197,10 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
} }
match ctx.path_context { 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 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); cov_mark::hit!(no_parens_in_use_item);
return false; return false;
} }

View File

@ -35,7 +35,7 @@ fn render(
let needs_bang = match completion.path_context { let needs_bang = match completion.path_context {
Some(PathCompletionCtx { kind, has_macro_bang, .. }) => { 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, _ => is_fn_like,
}; };