diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs index 09ba672a14d3b..fef177d2dd0c9 100644 --- a/crates/ide-completion/src/completions/attribute.rs +++ b/crates/ide-completion/src/completions/attribute.rs @@ -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, .. diff --git a/crates/ide-completion/src/completions/attribute/derive.rs b/crates/ide-completion/src/completions/attribute/derive.rs index 4f524dd73bb31..d827d781a32d1 100644 --- a/crates/ide-completion/src/completions/attribute/derive.rs +++ b/crates/ide-completion/src/completions/attribute/derive.rs @@ -13,10 +13,7 @@ use crate::{ 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, }; diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 03d9d3fa875b2..3e9bd6078de79 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -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); diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index fb9955c5e8cd9..1278564e195dd 100644 --- a/crates/ide-completion/src/completions/expr.rs +++ b/crates/ide-completion/src/completions/expr.rs @@ -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, }; diff --git a/crates/ide-completion/src/completions/item_list.rs b/crates/ide-completion/src/completions/item_list.rs index ff1abd715dc7a..80825a633fd96 100644 --- a/crates/ide-completion/src/completions/item_list.rs +++ b/crates/ide-completion/src/completions/item_list.rs @@ -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, }; diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index 64c3bd3fdf181..2dbe81f92a0bb 100644 --- a/crates/ide-completion/src/completions/type.rs +++ b/crates/ide-completion/src/completions/type.rs @@ -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, }; diff --git a/crates/ide-completion/src/completions/use_.rs b/crates/ide-completion/src/completions/use_.rs index fd6d7709a0ee0..a8d301da17f26 100644 --- a/crates/ide-completion/src/completions/use_.rs +++ b/crates/ide-completion/src/completions/use_.rs @@ -12,9 +12,9 @@ use crate::{ 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, }; diff --git a/crates/ide-completion/src/completions/vis.rs b/crates/ide-completion/src/completions/vis.rs index 7315a488b8d8e..50560c99923f9 100644 --- a/crates/ide-completion/src/completions/vis.rs +++ b/crates/ide-completion/src/completions/vis.rs @@ -10,7 +10,7 @@ use crate::{ 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, .. diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index c4de8dc52e16b..86d7edccc9571 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -75,8 +75,7 @@ pub(crate) struct PathCompletionCtx { // FIXME: use this /// The parent of the path we are completing. pub(super) parent: Option, - // FIXME: This should be PathKind, the none case should never occur - pub(super) kind: Option, + 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 @@ impl<'a> CompletionContext<'a> { } 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 @@ impl<'a> CompletionContext<'a> { } pub(crate) fn path_kind(&self) -> Option { - 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 @@ impl<'a> CompletionContext<'a> { 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 @@ impl<'a> CompletionContext<'a> { 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 @@ impl<'a> CompletionContext<'a> { } }; 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) { diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 836b14bc2cdba..a59c8e8ee2f93 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -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() { diff --git a/crates/ide-completion/src/render/function.rs b/crates/ide-completion/src/render/function.rs index d8183660e892c..6430b1e46a684 100644 --- a/crates/ide-completion/src/render/function.rs +++ b/crates/ide-completion/src/render/function.rs @@ -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; } diff --git a/crates/ide-completion/src/render/macro_.rs b/crates/ide-completion/src/render/macro_.rs index cbf51af7cc0da..22df7132f0ea0 100644 --- a/crates/ide-completion/src/render/macro_.rs +++ b/crates/ide-completion/src/render/macro_.rs @@ -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, };