about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/ide_completion/src/completions/attribute.rs22
-rw-r--r--crates/ide_completion/src/completions/dot.rs2
-rw-r--r--crates/ide_completion/src/completions/keyword.rs8
-rw-r--r--crates/ide_completion/src/completions/qualified_path.rs14
-rw-r--r--crates/ide_completion/src/completions/snippet.rs6
-rw-r--r--crates/ide_completion/src/completions/unqualified_path.rs6
-rw-r--r--crates/ide_completion/src/completions/use_.rs27
-rw-r--r--crates/ide_completion/src/completions/vis.rs22
-rw-r--r--crates/ide_completion/src/context.rs69
-rw-r--r--crates/ide_completion/src/render.rs4
10 files changed, 89 insertions, 91 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index 1a1fa533c16..3dd5a7ba223 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -2,8 +2,6 @@
 //!
 //! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
 
-use std::iter;
-
 use hir::ScopeDef;
 use ide_db::{
     helpers::{
@@ -24,7 +22,7 @@ use syntax::{
 
 use crate::{
     completions::module_or_attr,
-    context::{CompletionContext, PathCompletionContext, PathKind},
+    context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
     item::CompletionItem,
     Completions,
 };
@@ -76,25 +74,23 @@ pub(crate) fn complete_known_attribute_input(
 }
 
 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) {
-    let (is_trivial_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context {
-        Some(PathCompletionContext {
+    let (is_absolute_path, qualifier, is_inner, annotated_item_kind) = match ctx.path_context {
+        Some(PathCompletionCtx {
             kind: Some(PathKind::Attr { kind, annotated_item_kind }),
-            is_trivial_path,
+            is_absolute_path,
             ref qualifier,
             ..
-        }) => (is_trivial_path, qualifier, kind == AttrKind::Inner, annotated_item_kind),
+        }) => (is_absolute_path, qualifier, kind == AttrKind::Inner, annotated_item_kind),
         _ => return,
     };
 
     match qualifier {
-        Some((path, qualifier)) => {
-            let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
-                .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
-            if is_super_chain {
+        Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
+            if *is_super_chain {
                 acc.add_keyword(ctx, "super::");
             }
 
-            let module = match qualifier {
+            let module = match resolution {
                 Some(hir::PathResolution::Def(hir::ModuleDef::Module(it))) => it,
                 _ => return,
             };
@@ -107,7 +103,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
             return;
         }
         // fresh use tree with leading colon2, only show crate roots
-        None if !is_trivial_path => {
+        None if is_absolute_path => {
             ctx.process_all_names(&mut |name, res| match res {
                 ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
                     acc.add_resolution(ctx, name, res);
diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs
index b8bab941706..4a34b0f7e56 100644
--- a/crates/ide_completion/src/completions/dot.rs
+++ b/crates/ide_completion/src/completions/dot.rs
@@ -32,7 +32,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
     if !ctx.config.enable_self_on_the_fly {
         return;
     }
-    if !ctx.is_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
+    if ctx.is_non_trivial_path() || ctx.is_path_disallowed() || !ctx.expects_expression() {
         return;
     }
     if let Some(func) = ctx.function_def.as_ref().and_then(|fn_| ctx.sema.to_def(fn_)) {
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 7403e024586..0c0c9719d31 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -5,7 +5,7 @@
 use syntax::{SyntaxKind, T};
 
 use crate::{
-    context::{PathCompletionContext, PathKind},
+    context::{PathCompletionCtx, PathKind},
     patterns::ImmediateLocation,
     CompletionContext, CompletionItem, CompletionItemKind, Completions,
 };
@@ -122,9 +122,9 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
     }
 
     let (can_be_stmt, in_loop_body) = match ctx.path_context {
-        Some(PathCompletionContext {
-            is_trivial_path: true, can_be_stmt, in_loop_body, ..
-        }) => (can_be_stmt, in_loop_body),
+        Some(PathCompletionCtx { is_absolute_path: false, can_be_stmt, in_loop_body, .. }) => {
+            (can_be_stmt, in_loop_body)
+        }
         _ => return,
     };
 
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs
index 3ddbd5e2a2a..a3606c17f5a 100644
--- a/crates/ide_completion/src/completions/qualified_path.rs
+++ b/crates/ide_completion/src/completions/qualified_path.rs
@@ -6,7 +6,7 @@ use syntax::ast;
 
 use crate::{
     completions::module_or_fn_macro,
-    context::{PathCompletionContext, PathKind},
+    context::{PathCompletionCtx, PathKind},
     patterns::ImmediateLocation,
     CompletionContext, Completions,
 };
@@ -15,18 +15,16 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
     if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
         return;
     }
-    let ((path, resolution), kind) = match ctx.path_context {
+    let (qualifier, kind) = match ctx.path_context {
         // let ... else, syntax would come in really handy here right now
-        Some(PathCompletionContext { qualifier: Some(ref qualifier), kind, .. }) => {
-            (qualifier, kind)
-        }
+        Some(PathCompletionCtx { qualifier: Some(ref qualifier), kind, .. }) => (qualifier, kind),
         _ => return,
     };
 
     // special case `<_>::$0` as this doesn't resolve to anything.
-    if path.qualifier().is_none() {
+    if qualifier.path.qualifier().is_none() {
         if matches!(
-            path.segment().and_then(|it| it.kind()),
+            qualifier.path.segment().and_then(|it| it.kind()),
             Some(ast::PathSegmentKind::Type {
                 type_ref: Some(ast::Type::InferType(_)),
                 trait_ref: None,
@@ -42,7 +40,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
         }
     }
 
-    let resolution = match resolution {
+    let resolution = match &qualifier.resolution {
         Some(res) => res,
         None => return,
     };
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index 02f711f51d8..9a2b9c2fa40 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -5,7 +5,7 @@ use ide_db::helpers::{insert_use::ImportScope, SnippetCap};
 use syntax::T;
 
 use crate::{
-    context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem,
+    context::PathCompletionCtx, item::Builder, CompletionContext, CompletionItem,
     CompletionItemKind, Completions, SnippetScope,
 };
 
@@ -21,7 +21,9 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
     }
 
     let can_be_stmt = match ctx.path_context {
-        Some(PathCompletionContext { is_trivial_path: true, can_be_stmt, .. }) => can_be_stmt,
+        Some(PathCompletionCtx {
+            is_absolute_path: false, qualifier: None, can_be_stmt, ..
+        }) => can_be_stmt,
         _ => return,
     };
 
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs
index b7f6d8d883f..c225b37d72d 100644
--- a/crates/ide_completion/src/completions/unqualified_path.rs
+++ b/crates/ide_completion/src/completions/unqualified_path.rs
@@ -5,7 +5,7 @@ use syntax::{ast, AstNode};
 
 use crate::{
     completions::module_or_fn_macro,
-    context::{PathCompletionContext, PathKind},
+    context::{PathCompletionCtx, PathKind},
     patterns::ImmediateLocation,
     CompletionContext, Completions,
 };
@@ -16,11 +16,11 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
         return;
     }
     match ctx.path_context {
-        Some(PathCompletionContext {
+        Some(PathCompletionCtx {
             kind: Some(PathKind::Vis { .. } | PathKind::Attr { .. } | PathKind::Use { .. }),
             ..
         }) => return,
-        Some(PathCompletionContext { is_trivial_path: true, .. }) => (),
+        Some(PathCompletionCtx { is_absolute_path: false, qualifier: None, .. }) => (),
         _ => return,
     }
 
diff --git a/crates/ide_completion/src/completions/use_.rs b/crates/ide_completion/src/completions/use_.rs
index 222f95258e6..eac96c7cba0 100644
--- a/crates/ide_completion/src/completions/use_.rs
+++ b/crates/ide_completion/src/completions/use_.rs
@@ -1,36 +1,31 @@
 //! Completion for use trees
 
-use std::iter;
-
 use hir::ScopeDef;
 use syntax::{ast, AstNode};
 
 use crate::{
-    context::{CompletionContext, PathCompletionContext, PathKind},
+    context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
     Completions,
 };
 
 pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) {
-    let (is_trivial_path, qualifier, use_tree_parent) = match ctx.path_context {
-        Some(PathCompletionContext {
+    let (is_absolute_path, qualifier) = match ctx.path_context {
+        Some(PathCompletionCtx {
             kind: Some(PathKind::Use),
-            is_trivial_path,
-            use_tree_parent,
+            is_absolute_path,
             ref qualifier,
             ..
-        }) => (is_trivial_path, qualifier, use_tree_parent),
+        }) => (is_absolute_path, qualifier),
         _ => return,
     };
 
     match qualifier {
-        Some((path, qualifier)) => {
-            let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
-                .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
-            if is_super_chain {
+        Some(PathQualifierCtx { path, resolution, is_super_chain, use_tree_parent }) => {
+            if *is_super_chain {
                 acc.add_keyword(ctx, "super::");
             }
             // only show `self` in a new use-tree when the qualifier doesn't end in self
-            let not_preceded_by_self = use_tree_parent
+            let not_preceded_by_self = *use_tree_parent
                 && !matches!(
                     path.segment().and_then(|it| it.kind()),
                     Some(ast::PathSegmentKind::SelfKw)
@@ -39,12 +34,12 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
                 acc.add_keyword(ctx, "self");
             }
 
-            let qualifier = match qualifier {
+            let resolution = match resolution {
                 Some(it) => it,
                 None => return,
             };
 
-            match qualifier {
+            match resolution {
                 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
                     let module_scope = module.scope(ctx.db, ctx.module);
                     let unknown_is_current = |name: &hir::Name| {
@@ -82,7 +77,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
             }
         }
         // fresh use tree with leading colon2, only show crate roots
-        None if !is_trivial_path => {
+        None if is_absolute_path => {
             cov_mark::hit!(use_tree_crate_roots_only);
             ctx.process_all_names(&mut |name, res| match res {
                 ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
diff --git a/crates/ide_completion/src/completions/vis.rs b/crates/ide_completion/src/completions/vis.rs
index 0ac1393b53f..9cf96326588 100644
--- a/crates/ide_completion/src/completions/vis.rs
+++ b/crates/ide_completion/src/completions/vis.rs
@@ -1,28 +1,26 @@
 //! Completion for visibility specifiers.
 
-use std::iter;
-
 use hir::ScopeDef;
 
 use crate::{
-    context::{CompletionContext, PathCompletionContext, PathKind},
+    context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx},
     Completions,
 };
 
 pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
-    let (is_trivial_path, qualifier, has_in_token) = match ctx.path_context {
-        Some(PathCompletionContext {
+    let (is_absolute_path, qualifier, has_in_token) = match ctx.path_context {
+        Some(PathCompletionCtx {
             kind: Some(PathKind::Vis { has_in_token }),
-            is_trivial_path,
+            is_absolute_path,
             ref qualifier,
             ..
-        }) => (is_trivial_path, qualifier, has_in_token),
+        }) => (is_absolute_path, qualifier, has_in_token),
         _ => return,
     };
 
     match qualifier {
-        Some((path, qualifier)) => {
-            if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = qualifier {
+        Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
+            if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
                 if let Some(current_module) = ctx.module {
                     let next_towards_current = current_module
                         .path_to_root(ctx.db)
@@ -38,13 +36,11 @@ pub(crate) fn complete_vis(acc: &mut Completions, ctx: &CompletionContext) {
                 }
             }
 
-            let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
-                .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
-            if is_super_chain {
+            if *is_super_chain {
                 acc.add_keyword(ctx, "super::");
             }
         }
-        None if is_trivial_path => {
+        None if !is_absolute_path => {
             if !has_in_token {
                 cov_mark::hit!(kw_completion_in);
                 acc.add_keyword(ctx, "in");
diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs
index 4845427422c..de1a378ea82 100644
--- a/crates/ide_completion/src/context.rs
+++ b/crates/ide_completion/src/context.rs
@@ -36,6 +36,7 @@ pub(crate) enum PatternRefutability {
     Refutable,
     Irrefutable,
 }
+
 pub(crate) enum Visible {
     Yes,
     Editable,
@@ -54,18 +55,13 @@ pub(super) enum PathKind {
 }
 
 #[derive(Debug)]
-pub(crate) struct PathCompletionContext {
+pub(crate) struct PathCompletionCtx {
     /// If this is a call with () already there
     has_call_parens: bool,
-    /// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
-    pub(super) is_trivial_path: bool,
-    /// If not a trivial path, the prefix (qualifier).
-    pub(super) qualifier: Option<(ast::Path, Option<PathResolution>)>,
-    #[allow(dead_code)]
-    /// If not a trivial path, the suffix (parent).
-    pub(super) parent: Option<ast::Path>,
-    /// Whether the qualifier comes from a use tree parent or not
-    pub(super) use_tree_parent: bool,
+    /// Whether this path stars with a `::`.
+    pub(super) is_absolute_path: bool,
+    /// The qualifier of the current path if it exists.
+    pub(super) qualifier: Option<PathQualifierCtx>,
     pub(super) kind: Option<PathKind>,
     /// Whether the path segment has type args or not.
     pub(super) has_type_args: bool,
@@ -75,6 +71,16 @@ pub(crate) struct PathCompletionContext {
 }
 
 #[derive(Debug)]
+pub(crate) struct PathQualifierCtx {
+    pub path: ast::Path,
+    pub resolution: Option<PathResolution>,
+    /// Whether this path consists solely of `super` segments
+    pub is_super_chain: bool,
+    /// Whether the qualifier comes from a use tree parent or not
+    pub use_tree_parent: bool,
+}
+
+#[derive(Debug)]
 pub(super) struct PatternContext {
     pub(super) refutability: PatternRefutability,
     pub(super) param_ctx: Option<(ast::ParamList, ast::Param, ParamKind)>,
@@ -131,7 +137,7 @@ pub(crate) struct CompletionContext<'a> {
 
     pub(super) lifetime_ctx: Option<LifetimeContext>,
     pub(super) pattern_ctx: Option<PatternContext>,
-    pub(super) path_context: Option<PathCompletionContext>,
+    pub(super) path_context: Option<PathCompletionCtx>,
 
     pub(super) locals: Vec<(Name, Local)>,
 
@@ -264,27 +270,29 @@ impl<'a> CompletionContext<'a> {
     }
 
     pub(crate) fn expects_expression(&self) -> bool {
-        matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Expr), .. }))
+        matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Expr), .. }))
     }
 
     pub(crate) fn expects_type(&self) -> bool {
-        matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Type), .. }))
+        matches!(self.path_context, Some(PathCompletionCtx { kind: Some(PathKind::Type), .. }))
     }
 
     pub(crate) fn path_is_call(&self) -> bool {
         self.path_context.as_ref().map_or(false, |it| it.has_call_parens)
     }
 
-    pub(crate) fn is_trivial_path(&self) -> bool {
-        matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: true, .. }))
-    }
-
     pub(crate) fn is_non_trivial_path(&self) -> bool {
-        matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: false, .. }))
+        matches!(
+            self.path_context,
+            Some(
+                PathCompletionCtx { is_absolute_path: true, .. }
+                    | PathCompletionCtx { qualifier: Some(_), .. }
+            )
+        )
     }
 
     pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
-        self.path_context.as_ref().and_then(|it| it.qualifier.as_ref().map(|(it, _)| it))
+        self.path_context.as_ref().and_then(|it| it.qualifier.as_ref().map(|it| &it.path))
     }
 
     pub(crate) fn path_kind(&self) -> Option<PathKind> {
@@ -791,20 +799,18 @@ impl<'a> CompletionContext<'a> {
         sema: &Semantics<RootDatabase>,
         original_file: &SyntaxNode,
         name_ref: ast::NameRef,
-    ) -> Option<(PathCompletionContext, Option<PatternContext>)> {
+    ) -> Option<(PathCompletionCtx, Option<PatternContext>)> {
         let parent = name_ref.syntax().parent()?;
         let segment = ast::PathSegment::cast(parent)?;
         let path = segment.parent_path();
 
-        let mut path_ctx = PathCompletionContext {
+        let mut path_ctx = PathCompletionCtx {
             has_call_parens: false,
-            is_trivial_path: false,
+            is_absolute_path: false,
             qualifier: None,
-            parent: None,
             has_type_args: false,
             can_be_stmt: false,
             in_loop_body: false,
-            use_tree_parent: false,
             kind: None,
         };
         let mut pat_ctx = None;
@@ -859,26 +865,31 @@ impl<'a> CompletionContext<'a> {
         path_ctx.has_type_args = segment.generic_arg_list().is_some();
 
         if let Some((path, use_tree_parent)) = path_or_use_tree_qualifier(&path) {
-            path_ctx.use_tree_parent = use_tree_parent;
+            if !use_tree_parent {
+                path_ctx.is_absolute_path =
+                    path.top_path().segment().map_or(false, |it| it.coloncolon_token().is_some());
+            }
+
             let path = path
                 .segment()
                 .and_then(|it| find_node_in_file(original_file, &it))
                 .map(|it| it.parent_path());
             path_ctx.qualifier = path.map(|path| {
                 let res = sema.resolve_path(&path);
-                (path, res)
+                let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier())
+                    .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
+                PathQualifierCtx { path, resolution: res, is_super_chain, use_tree_parent }
             });
             return Some((path_ctx, pat_ctx));
         }
 
         if let Some(segment) = path.segment() {
             if segment.coloncolon_token().is_some() {
+                path_ctx.is_absolute_path = true;
                 return Some((path_ctx, pat_ctx));
             }
         }
 
-        path_ctx.is_trivial_path = true;
-
         // Find either enclosing expr statement (thing with `;`) or a
         // block. If block, check that we are the last expr.
         path_ctx.can_be_stmt = name_ref
@@ -969,7 +980,7 @@ fn path_or_use_tree_qualifier(path: &ast::Path) -> Option<(ast::Path, bool)> {
     }
     let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
     let use_tree = use_tree_list.syntax().parent().and_then(ast::UseTree::cast)?;
-    use_tree.path().zip(Some(true))
+    Some((use_tree.path()?, true))
 }
 
 fn has_ref(token: &SyntaxToken) -> bool {
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs
index 6be26511238..027fb535271 100644
--- a/crates/ide_completion/src/render.rs
+++ b/crates/ide_completion/src/render.rs
@@ -19,7 +19,7 @@ use ide_db::{
 use syntax::{SmolStr, SyntaxKind, TextRange};
 
 use crate::{
-    context::{PathCompletionContext, PathKind},
+    context::{PathCompletionCtx, PathKind},
     item::{CompletionRelevanceTypeMatch, ImportEdit},
     render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
     CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
@@ -234,7 +234,7 @@ fn render_resolution_(
     // Add `<>` for generic types
     let type_path_no_ty_args = matches!(
         ctx.completion.path_context,
-        Some(PathCompletionContext { kind: Some(PathKind::Type), has_type_args: false, .. })
+        Some(PathCompletionCtx { kind: Some(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() {