Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide_completion/src/completions/qualified_path.rs')
| -rw-r--r-- | crates/ide_completion/src/completions/qualified_path.rs | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index 4abf7374b3..b5c3d83c16 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -7,17 +7,23 @@ use rustc_hash::FxHashSet; use syntax::{ast, AstNode}; use crate::{ - context::PathCompletionContext, patterns::ImmediateLocation, CompletionContext, Completions, + context::{PathCompletionContext, PathKind}, + patterns::ImmediateLocation, + CompletionContext, Completions, }; pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() { return; } - let (path, use_tree_parent) = match &ctx.path_context { - Some(PathCompletionContext { qualifier: Some(qualifier), use_tree_parent, .. }) => { - (qualifier, *use_tree_parent) - } + let (path, use_tree_parent, kind) = match ctx.path_context { + // let ... else, syntax would come in really handy here right now + Some(PathCompletionContext { + qualifier: Some(ref qualifier), + use_tree_parent, + kind, + .. + }) => (qualifier, use_tree_parent, kind), _ => return, }; @@ -44,7 +50,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } return; } - Some(ImmediateLocation::Visibility(_)) => { + _ => (), + } + + match kind { + Some(PathKind::Vis { .. }) => { if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { if let Some(current_module) = ctx.scope.module() { if let Some(next) = current_module @@ -61,7 +71,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } return; } - Some(ImmediateLocation::Attribute(_)) => { + Some(PathKind::Attr) => { if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { for (name, def) in module.scope(ctx.db, context_module) { let add_resolution = match def { @@ -76,37 +86,38 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } return; } + Some(PathKind::Use) => { + if iter::successors(Some(path.clone()), |p| p.qualifier()) + .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) + { + acc.add_keyword(ctx, "super::"); + } + // only show `self` in a new use-tree when the qualifier doesn't end in self + if use_tree_parent + && !matches!( + path.segment().and_then(|it| it.kind()), + Some(ast::PathSegmentKind::SelfKw) + ) + { + acc.add_keyword(ctx, "self"); + } + } _ => (), } - if ctx.in_use_tree() { - if iter::successors(Some(path.clone()), |p| p.qualifier()) - .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) - { - acc.add_keyword(ctx, "super::"); - } - // only show `self` in a new use-tree when the qualifier doesn't end in self - if use_tree_parent - && !matches!( - path.segment().and_then(|it| it.kind()), - Some(ast::PathSegmentKind::SelfKw) - ) - { - acc.add_keyword(ctx, "self"); - } + if !matches!(kind, Some(PathKind::Pat)) { + // Add associated types on type parameters and `Self`. + resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| { + acc.add_type_alias(ctx, alias); + None::<()> + }); } - // Add associated types on type parameters and `Self`. - resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| { - acc.add_type_alias(ctx, alias); - None::<()> - }); - match resolution { hir::PathResolution::Def(hir::ModuleDef::Module(module)) => { let module_scope = module.scope(ctx.db, context_module); for (name, def) in module_scope { - if ctx.in_use_tree() { + if let Some(PathKind::Use) = kind { if let ScopeDef::Unknown = def { if let Some(ast::NameLike::NameRef(name_ref)) = ctx.name_syntax.as_ref() { if name_ref.syntax().text() == name.to_smol_str().as_str() { |