From 85b68b1f7dd9a1983a4bfa0b9ab0212aea18de42 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 17 Jun 2022 17:27:12 +0200 Subject: [PATCH] Inline PathQualifierCtx --- .../src/completions/attribute.rs | 6 ++--- .../src/completions/attribute/derive.rs | 4 ++-- crates/ide-completion/src/completions/expr.rs | 6 ++--- .../src/completions/item_list.rs | 4 ++-- .../ide-completion/src/completions/pattern.rs | 4 ++-- crates/ide-completion/src/completions/type.rs | 7 ++---- crates/ide-completion/src/completions/use_.rs | 17 +++++++++----- crates/ide-completion/src/completions/vis.rs | 4 ++-- crates/ide-completion/src/context.rs | 22 ++++++++----------- crates/ide-completion/src/context/analysis.rs | 13 +++++------ 10 files changed, 39 insertions(+), 48 deletions(-) diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs index 096e24eab3..fe5bdeec66 100644 --- a/crates/ide-completion/src/completions/attribute.rs +++ b/crates/ide-completion/src/completions/attribute.rs @@ -18,9 +18,7 @@ use syntax::{ use crate::{ completions::module_or_attr, - context::{ - CompletionContext, IdentContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified, - }, + context::{CompletionContext, IdentContext, PathCompletionCtx, PathKind, Qualified}, item::CompletionItem, Completions, }; @@ -84,7 +82,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) }; match qualified { - Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { + Qualified::With { resolution, is_super_chain, .. } => { if *is_super_chain { acc.add_keyword(ctx, "super::"); } diff --git a/crates/ide-completion/src/completions/attribute/derive.rs b/crates/ide-completion/src/completions/attribute/derive.rs index 0f78fc988d..aefb986535 100644 --- a/crates/ide-completion/src/completions/attribute/derive.rs +++ b/crates/ide-completion/src/completions/attribute/derive.rs @@ -5,7 +5,7 @@ use itertools::Itertools; use syntax::SmolStr; use crate::{ - context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, + context::{CompletionContext, PathCompletionCtx, PathKind, Qualified}, item::CompletionItem, Completions, }; @@ -21,7 +21,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { let core = ctx.famous_defs().core(); match qualified { - Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { + Qualified::With { resolution, is_super_chain, .. } => { if *is_super_chain { acc.add_keyword(ctx, "super::"); } diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index 41e1dc9f91..fcd2144809 100644 --- a/crates/ide-completion/src/completions/expr.rs +++ b/crates/ide-completion/src/completions/expr.rs @@ -4,9 +4,7 @@ use hir::ScopeDef; use ide_db::FxHashSet; use crate::{ - context::{ - NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified, - }, + context::{NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified}, CompletionContext, Completions, }; @@ -67,7 +65,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) .into_iter() .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .for_each(|item| add_assoc_item(acc, ctx, item)), - Qualified::With(PathQualifierCtx { resolution, .. }) => { + Qualified::With { resolution, .. } => { let resolution = match resolution { Some(it) => it, None => return, diff --git a/crates/ide-completion/src/completions/item_list.rs b/crates/ide-completion/src/completions/item_list.rs index 9c8fbb84f5..2e03935086 100644 --- a/crates/ide-completion/src/completions/item_list.rs +++ b/crates/ide-completion/src/completions/item_list.rs @@ -2,7 +2,7 @@ use crate::{ completions::module_or_fn_macro, - context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, + context::{ItemListKind, PathCompletionCtx, PathKind, Qualified}, CompletionContext, Completions, }; @@ -44,7 +44,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) } match qualified { - Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { + Qualified::With { resolution, is_super_chain, .. } => { if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution { for (name, def) in module.scope(ctx.db, Some(ctx.module)) { if let Some(def) = module_or_fn_macro(ctx.db, def) { diff --git a/crates/ide-completion/src/completions/pattern.rs b/crates/ide-completion/src/completions/pattern.rs index 8dedcc35ec..b2630798bb 100644 --- a/crates/ide-completion/src/completions/pattern.rs +++ b/crates/ide-completion/src/completions/pattern.rs @@ -5,7 +5,7 @@ use ide_db::FxHashSet; use syntax::ast::Pat; use crate::{ - context::{PathCompletionCtx, PathQualifierCtx, PatternRefutability, Qualified}, + context::{PathCompletionCtx, PatternRefutability, Qualified}, CompletionContext, Completions, }; @@ -114,7 +114,7 @@ fn pattern_path_completion( PathCompletionCtx { qualified, .. }: &PathCompletionCtx, ) { match qualified { - Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { + Qualified::With { resolution, is_super_chain, .. } => { if *is_super_chain { acc.add_keyword(ctx, "super::"); } diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index d2dab9f4ed..b6666ef1a4 100644 --- a/crates/ide-completion/src/completions/type.rs +++ b/crates/ide-completion/src/completions/type.rs @@ -5,10 +5,7 @@ use ide_db::FxHashSet; use syntax::{ast, AstNode}; use crate::{ - context::{ - PathCompletionCtx, PathKind, PathQualifierCtx, Qualified, TypeAscriptionTarget, - TypeLocation, - }, + context::{PathCompletionCtx, PathKind, Qualified, TypeAscriptionTarget, TypeLocation}, render::render_type_inference, CompletionContext, Completions, }; @@ -61,7 +58,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) .into_iter() .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .for_each(|item| add_assoc_item(acc, item)), - Qualified::With(PathQualifierCtx { resolution, .. }) => { + Qualified::With { resolution, .. } => { let resolution = match resolution { Some(it) => it, None => return, diff --git a/crates/ide-completion/src/completions/use_.rs b/crates/ide-completion/src/completions/use_.rs index 33b50f22f5..64fa426395 100644 --- a/crates/ide-completion/src/completions/use_.rs +++ b/crates/ide-completion/src/completions/use_.rs @@ -6,25 +6,30 @@ use syntax::{ast, AstNode}; use crate::{ context::{ - CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, - PathQualifierCtx, Qualified, + CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified, }, item::Builder, CompletionItem, CompletionItemKind, CompletionRelevance, Completions, }; pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) { - let (qualified, name_ref) = match ctx.nameref_ctx() { + let (qualified, name_ref, use_tree_parent) = match ctx.nameref_ctx() { Some(NameRefContext { - kind: Some(NameRefKind::Path(PathCompletionCtx { kind: PathKind::Use, qualified, .. })), + kind: + Some(NameRefKind::Path(PathCompletionCtx { + kind: PathKind::Use, + qualified, + use_tree_parent, + .. + })), nameref, .. - }) => (qualified, nameref), + }) => (qualified, nameref, use_tree_parent), _ => return, }; match qualified { - Qualified::With(PathQualifierCtx { path, resolution, is_super_chain, use_tree_parent }) => { + Qualified::With { path, resolution, is_super_chain } => { if *is_super_chain { acc.add_keyword(ctx, "super::"); } diff --git a/crates/ide-completion/src/completions/vis.rs b/crates/ide-completion/src/completions/vis.rs index d27fc8f446..319976737e 100644 --- a/crates/ide-completion/src/completions/vis.rs +++ b/crates/ide-completion/src/completions/vis.rs @@ -3,7 +3,7 @@ use hir::ScopeDef; use crate::{ - context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, + context::{CompletionContext, PathCompletionCtx, PathKind, Qualified}, Completions, }; @@ -16,7 +16,7 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext) }; match qualified { - Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { + Qualified::With { resolution, is_super_chain, .. } => { // Try completing next child module of the path that is still a parent of the current module if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution { let next_towards_current = ctx diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index e2bf4284f5..6af24f8748 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -64,6 +64,8 @@ pub(crate) struct PathCompletionCtx { pub(super) kind: PathKind, /// Whether the path segment has type args or not. pub(super) has_type_args: bool, + /// Whether the qualifier comes from a use tree parent or not + pub(crate) use_tree_parent: bool, } impl PathCompletionCtx { @@ -149,24 +151,18 @@ pub(super) enum ItemListKind { #[derive(Debug)] pub(super) enum Qualified { No, - With(PathQualifierCtx), + With { + path: ast::Path, + resolution: Option, + /// Whether this path consists solely of `super` segments + is_super_chain: bool, + }, /// <_>:: Infer, /// Whether the path is an absolute path Absolute, } -/// The path qualifier state of the path we are completing. -#[derive(Debug)] -pub(crate) struct PathQualifierCtx { - pub(crate) path: ast::Path, - pub(crate) resolution: Option, - /// Whether this path consists solely of `super` segments - pub(crate) is_super_chain: bool, - /// Whether the qualifier comes from a use tree parent or not - pub(crate) use_tree_parent: bool, -} - /// The state of the pattern we are completing. #[derive(Debug)] pub(super) struct PatternContext { @@ -410,7 +406,7 @@ impl<'a> CompletionContext<'a> { pub(crate) fn path_qual(&self) -> Option<&ast::Path> { self.path_context().and_then(|it| match &it.qualified { - Qualified::With(it) => Some(&it.path), + Qualified::With { path, .. } => Some(path), _ => None, }) } diff --git a/crates/ide-completion/src/context/analysis.rs b/crates/ide-completion/src/context/analysis.rs index 57d6682b4b..25b384d323 100644 --- a/crates/ide-completion/src/context/analysis.rs +++ b/crates/ide-completion/src/context/analysis.rs @@ -13,8 +13,8 @@ use syntax::{ use crate::context::{ CompletionContext, DotAccess, DotAccessKind, IdentContext, ItemListKind, LifetimeContext, LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamKind, PathCompletionCtx, - PathKind, PathQualifierCtx, PatternContext, PatternRefutability, Qualified, QualifierCtx, - TypeAscriptionTarget, TypeLocation, COMPLETION_MARKER, + PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx, TypeAscriptionTarget, + TypeLocation, COMPLETION_MARKER, }; impl<'a> CompletionContext<'a> { @@ -589,6 +589,7 @@ impl<'a> CompletionContext<'a> { parent: path.parent_path(), kind: PathKind::Item { kind: ItemListKind::SourceFile }, has_type_args: false, + use_tree_parent: false, }; let is_in_block = |it: &SyntaxNode| { @@ -853,6 +854,7 @@ impl<'a> CompletionContext<'a> { // calculate the qualifier context 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 && segment.coloncolon_token().is_some() { path_ctx.qualified = Qualified::Absolute; } else { @@ -878,12 +880,7 @@ impl<'a> CompletionContext<'a> { let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier()) .all(|p| p.segment().and_then(|s| s.super_token()).is_some()); - Qualified::With(PathQualifierCtx { - path, - resolution: res, - is_super_chain, - use_tree_parent, - }) + Qualified::With { path, resolution: res, is_super_chain } } }; }