Inline PathQualifierCtx

This commit is contained in:
Lukas Wirth 2022-06-17 17:27:12 +02:00
parent d6f161ffa9
commit 85b68b1f7d
10 changed files with 39 additions and 48 deletions

View file

@ -18,9 +18,7 @@ use syntax::{
use crate::{ use crate::{
completions::module_or_attr, completions::module_or_attr,
context::{ context::{CompletionContext, IdentContext, PathCompletionCtx, PathKind, Qualified},
CompletionContext, IdentContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified,
},
item::CompletionItem, item::CompletionItem,
Completions, Completions,
}; };
@ -84,7 +82,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
}; };
match qualified { match qualified {
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { Qualified::With { resolution, is_super_chain, .. } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }

View file

@ -5,7 +5,7 @@ use itertools::Itertools;
use syntax::SmolStr; use syntax::SmolStr;
use crate::{ use crate::{
context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
item::CompletionItem, item::CompletionItem,
Completions, Completions,
}; };
@ -21,7 +21,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let core = ctx.famous_defs().core(); let core = ctx.famous_defs().core();
match qualified { match qualified {
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { Qualified::With { resolution, is_super_chain, .. } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }

View file

@ -4,9 +4,7 @@ use hir::ScopeDef;
use ide_db::FxHashSet; use ide_db::FxHashSet;
use crate::{ use crate::{
context::{ context::{NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified},
NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified,
},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -67,7 +65,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
.into_iter() .into_iter()
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, ctx, item)), .for_each(|item| add_assoc_item(acc, ctx, item)),
Qualified::With(PathQualifierCtx { resolution, .. }) => { Qualified::With { resolution, .. } => {
let resolution = match resolution { let resolution = match resolution {
Some(it) => it, Some(it) => it,
None => return, None => return,

View file

@ -2,7 +2,7 @@
use crate::{ use crate::{
completions::module_or_fn_macro, completions::module_or_fn_macro,
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, context::{ItemListKind, PathCompletionCtx, PathKind, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -44,7 +44,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
} }
match qualified { 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 { if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
for (name, def) in module.scope(ctx.db, Some(ctx.module)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
if let Some(def) = module_or_fn_macro(ctx.db, def) { if let Some(def) = module_or_fn_macro(ctx.db, def) {

View file

@ -5,7 +5,7 @@ use ide_db::FxHashSet;
use syntax::ast::Pat; use syntax::ast::Pat;
use crate::{ use crate::{
context::{PathCompletionCtx, PathQualifierCtx, PatternRefutability, Qualified}, context::{PathCompletionCtx, PatternRefutability, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -114,7 +114,7 @@ fn pattern_path_completion(
PathCompletionCtx { qualified, .. }: &PathCompletionCtx, PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
) { ) {
match qualified { match qualified {
Qualified::With(PathQualifierCtx { resolution, is_super_chain, .. }) => { Qualified::With { resolution, is_super_chain, .. } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }

View file

@ -5,10 +5,7 @@ use ide_db::FxHashSet;
use syntax::{ast, AstNode}; use syntax::{ast, AstNode};
use crate::{ use crate::{
context::{ context::{PathCompletionCtx, PathKind, Qualified, TypeAscriptionTarget, TypeLocation},
PathCompletionCtx, PathKind, PathQualifierCtx, Qualified, TypeAscriptionTarget,
TypeLocation,
},
render::render_type_inference, render::render_type_inference,
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -61,7 +58,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
.into_iter() .into_iter()
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
.for_each(|item| add_assoc_item(acc, item)), .for_each(|item| add_assoc_item(acc, item)),
Qualified::With(PathQualifierCtx { resolution, .. }) => { Qualified::With { resolution, .. } => {
let resolution = match resolution { let resolution = match resolution {
Some(it) => it, Some(it) => it,
None => return, None => return,

View file

@ -6,25 +6,30 @@ use syntax::{ast, AstNode};
use crate::{ use crate::{
context::{ context::{
CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified,
PathQualifierCtx, Qualified,
}, },
item::Builder, item::Builder,
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
}; };
pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) { 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 { Some(NameRefContext {
kind: Some(NameRefKind::Path(PathCompletionCtx { kind: PathKind::Use, qualified, .. })), kind:
Some(NameRefKind::Path(PathCompletionCtx {
kind: PathKind::Use,
qualified,
use_tree_parent,
..
})),
nameref, nameref,
.. ..
}) => (qualified, nameref), }) => (qualified, nameref, use_tree_parent),
_ => return, _ => return,
}; };
match qualified { match qualified {
Qualified::With(PathQualifierCtx { path, resolution, is_super_chain, use_tree_parent }) => { Qualified::With { path, resolution, is_super_chain } => {
if *is_super_chain { if *is_super_chain {
acc.add_keyword(ctx, "super::"); acc.add_keyword(ctx, "super::");
} }

View file

@ -3,7 +3,7 @@
use hir::ScopeDef; use hir::ScopeDef;
use crate::{ use crate::{
context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
Completions, Completions,
}; };
@ -16,7 +16,7 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext)
}; };
match qualified { 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 // 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 { if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
let next_towards_current = ctx let next_towards_current = ctx

View file

@ -64,6 +64,8 @@ pub(crate) struct PathCompletionCtx {
pub(super) kind: PathKind, pub(super) kind: 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,
/// Whether the qualifier comes from a use tree parent or not
pub(crate) use_tree_parent: bool,
} }
impl PathCompletionCtx { impl PathCompletionCtx {
@ -149,24 +151,18 @@ pub(super) enum ItemListKind {
#[derive(Debug)] #[derive(Debug)]
pub(super) enum Qualified { pub(super) enum Qualified {
No, No,
With(PathQualifierCtx), With {
path: ast::Path,
resolution: Option<PathResolution>,
/// Whether this path consists solely of `super` segments
is_super_chain: bool,
},
/// <_>:: /// <_>::
Infer, Infer,
/// Whether the path is an absolute path /// Whether the path is an absolute path
Absolute, 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<PathResolution>,
/// 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. /// The state of the pattern we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct PatternContext { pub(super) struct PatternContext {
@ -410,7 +406,7 @@ impl<'a> CompletionContext<'a> {
pub(crate) fn path_qual(&self) -> Option<&ast::Path> { pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
self.path_context().and_then(|it| match &it.qualified { self.path_context().and_then(|it| match &it.qualified {
Qualified::With(it) => Some(&it.path), Qualified::With { path, .. } => Some(path),
_ => None, _ => None,
}) })
} }

View file

@ -13,8 +13,8 @@ use syntax::{
use crate::context::{ use crate::context::{
CompletionContext, DotAccess, DotAccessKind, IdentContext, ItemListKind, LifetimeContext, CompletionContext, DotAccess, DotAccessKind, IdentContext, ItemListKind, LifetimeContext,
LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamKind, PathCompletionCtx, LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamKind, PathCompletionCtx,
PathKind, PathQualifierCtx, PatternContext, PatternRefutability, Qualified, QualifierCtx, PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx, TypeAscriptionTarget,
TypeAscriptionTarget, TypeLocation, COMPLETION_MARKER, TypeLocation, COMPLETION_MARKER,
}; };
impl<'a> CompletionContext<'a> { impl<'a> CompletionContext<'a> {
@ -589,6 +589,7 @@ impl<'a> CompletionContext<'a> {
parent: path.parent_path(), parent: path.parent_path(),
kind: PathKind::Item { kind: ItemListKind::SourceFile }, kind: PathKind::Item { kind: ItemListKind::SourceFile },
has_type_args: false, has_type_args: false,
use_tree_parent: false,
}; };
let is_in_block = |it: &SyntaxNode| { let is_in_block = |it: &SyntaxNode| {
@ -853,6 +854,7 @@ impl<'a> CompletionContext<'a> {
// calculate the qualifier context // calculate the qualifier context
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) {
path_ctx.use_tree_parent = use_tree_parent;
if !use_tree_parent && segment.coloncolon_token().is_some() { if !use_tree_parent && segment.coloncolon_token().is_some() {
path_ctx.qualified = Qualified::Absolute; path_ctx.qualified = Qualified::Absolute;
} else { } else {
@ -878,12 +880,7 @@ impl<'a> CompletionContext<'a> {
let is_super_chain = let is_super_chain =
iter::successors(Some(path.clone()), |p| p.qualifier()) iter::successors(Some(path.clone()), |p| p.qualifier())
.all(|p| p.segment().and_then(|s| s.super_token()).is_some()); .all(|p| p.segment().and_then(|s| s.super_token()).is_some());
Qualified::With(PathQualifierCtx { Qualified::With { path, resolution: res, is_super_chain }
path,
resolution: res,
is_super_chain,
use_tree_parent,
})
} }
}; };
} }