diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs index 56e51d32e2..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::"); } @@ -112,6 +110,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) }); acc.add_nameref_keywords_with_colon(ctx); } + Qualified::Infer => {} } let attributes = annotated_item_kind.and_then(|kind| { diff --git a/crates/ide-completion/src/completions/attribute/derive.rs b/crates/ide-completion/src/completions/attribute/derive.rs index 0d4a292e0f..aefb986535 100644 --- a/crates/ide-completion/src/completions/attribute/derive.rs +++ b/crates/ide-completion/src/completions/attribute/derive.rs @@ -5,21 +5,23 @@ use itertools::Itertools; use syntax::SmolStr; use crate::{ - context::{CompletionContext, PathCompletionCtx, PathKind, PathQualifierCtx, Qualified}, + context::{CompletionContext, PathCompletionCtx, PathKind, Qualified}, item::CompletionItem, Completions, }; pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { - let qualified = match ctx.path_context() { - Some(&PathCompletionCtx { kind: PathKind::Derive, ref qualified, .. }) => qualified, + let (qualified, existing_derives) = match ctx.path_context() { + Some(PathCompletionCtx { + kind: PathKind::Derive { existing_derives }, qualified, .. + }) => (qualified, existing_derives), _ => return, }; 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::"); } @@ -32,7 +34,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) { let add_def = match def { ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => { - !ctx.existing_derives.contains(&mac) && mac.is_derive(ctx.db) + !existing_derives.contains(&mac) && mac.is_derive(ctx.db) } ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true, _ => false, @@ -48,7 +50,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { ctx.process_all_names(&mut |name, def| { let mac = match def { ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) - if !ctx.existing_derives.contains(&mac) && mac.is_derive(ctx.db) => + if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) => { mac } @@ -74,7 +76,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { let mut components = vec![derive_completion.label]; components.extend(derive_completion.dependencies.iter().filter( |&&dependency| { - !ctx.existing_derives + !existing_derives .iter() .map(|it| it.name(ctx.db)) .any(|it| it.to_smol_str() == dependency) @@ -99,6 +101,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { }); acc.add_nameref_keywords_with_colon(ctx); } + Qualified::Infer => {} } } diff --git a/crates/ide-completion/src/completions/expr.rs b/crates/ide-completion/src/completions/expr.rs index f0a8529c78..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, }; @@ -61,15 +59,13 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) }; match qualified { - Qualified::With(PathQualifierCtx { is_infer_qualifier, resolution, .. }) => { - if *is_infer_qualifier { - ctx.traits_in_scope() - .0 - .into_iter() - .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) - .for_each(|item| add_assoc_item(acc, ctx, item)); - return; - } + Qualified::Infer => ctx + .traits_in_scope() + .0 + .into_iter() + .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) + .for_each(|item| add_assoc_item(acc, ctx, item)), + Qualified::With { resolution, .. } => { let resolution = match resolution { Some(it) => it, None => return, diff --git a/crates/ide-completion/src/completions/flyimport.rs b/crates/ide-completion/src/completions/flyimport.rs index 6266bcef34..33ae365cff 100644 --- a/crates/ide-completion/src/completions/flyimport.rs +++ b/crates/ide-completion/src/completions/flyimport.rs @@ -120,7 +120,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) kind @ (PathKind::Expr { .. } | PathKind::Type { .. } | PathKind::Attr { .. } - | PathKind::Derive + | PathKind::Derive { .. } | PathKind::Pat), .. })), @@ -188,10 +188,10 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) (PathKind::Attr { .. }, ItemInNs::Macros(mac)) => mac.is_attr(ctx.db), (PathKind::Attr { .. }, _) => false, - (PathKind::Derive, ItemInNs::Macros(mac)) => { - mac.is_derive(ctx.db) && !ctx.existing_derives.contains(&mac) + (PathKind::Derive { existing_derives }, ItemInNs::Macros(mac)) => { + mac.is_derive(ctx.db) && !existing_derives.contains(&mac) } - (PathKind::Derive, _) => false, + (PathKind::Derive { .. }, _) => false, } }; diff --git a/crates/ide-completion/src/completions/item_list.rs b/crates/ide-completion/src/completions/item_list.rs index e3f3b7f528..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) { @@ -66,7 +66,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) }); acc.add_nameref_keywords_with_colon(ctx); } - Qualified::No => {} + Qualified::Infer | Qualified::No => {} } } diff --git a/crates/ide-completion/src/completions/pattern.rs b/crates/ide-completion/src/completions/pattern.rs index b7710ae70d..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::"); } @@ -208,5 +208,6 @@ fn pattern_path_completion( acc.add_nameref_keywords_with_colon(ctx); } + Qualified::Infer => {} } } diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index f2418d76cc..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, }; @@ -55,15 +52,13 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) }; match qualified { - Qualified::With(PathQualifierCtx { is_infer_qualifier, resolution, .. }) => { - if *is_infer_qualifier { - ctx.traits_in_scope() - .0 - .into_iter() - .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) - .for_each(|item| add_assoc_item(acc, item)); - return; - } + Qualified::Infer => ctx + .traits_in_scope() + .0 + .into_iter() + .flat_map(|it| hir::Trait::from(it).items(ctx.sema.db)) + .for_each(|item| add_assoc_item(acc, item)), + 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 3b6ce8fc1a..64fa426395 100644 --- a/crates/ide-completion/src/completions/use_.rs +++ b/crates/ide-completion/src/completions/use_.rs @@ -6,31 +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::"); } @@ -136,5 +135,6 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) }); acc.add_nameref_keywords_with_colon(ctx); } + Qualified::Infer => {} } } diff --git a/crates/ide-completion/src/completions/vis.rs b/crates/ide-completion/src/completions/vis.rs index b971808626..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 @@ -37,7 +37,7 @@ pub(crate) fn complete_vis_path(acc: &mut Completions, ctx: &CompletionContext) acc.add_keyword(ctx, "super::"); } } - Qualified::Absolute => {} + Qualified::Absolute | Qualified::Infer => {} Qualified::No => { if !has_in_token { cov_mark::hit!(kw_completion_in); diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index 4ed083a524..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 { @@ -101,7 +103,9 @@ pub(super) enum PathKind { kind: AttrKind, annotated_item_kind: Option, }, - Derive, + Derive { + existing_derives: FxHashSet, + }, /// Path in item position, that is inside an (Assoc)ItemList Item { kind: ItemListKind, @@ -147,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, - /// <_> - pub(crate) is_infer_qualifier: bool, -} - /// The state of the pattern we are completing. #[derive(Debug)] pub(super) struct PatternContext { @@ -318,13 +316,16 @@ pub(crate) struct CompletionContext<'a> { pub(super) expected_type: Option, /// The parent function of the cursor position if it exists. + // FIXME: This probably doesn't belong here pub(super) function_def: Option, /// The parent impl of the cursor position if it exists. + // FIXME: This probably doesn't belong here pub(super) impl_def: Option, /// Are we completing inside a let statement with a missing semicolon? // FIXME: This should be part of PathKind::Expr pub(super) incomplete_let: bool, + // FIXME: This shouldn't exist pub(super) previous_token: Option, pub(super) ident_ctx: IdentContext, @@ -332,8 +333,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) pattern_ctx: Option, pub(super) qualifier_ctx: QualifierCtx, - pub(super) existing_derives: FxHashSet, - pub(super) locals: FxHashMap, } @@ -354,6 +353,7 @@ impl<'a> CompletionContext<'a> { } } + // FIXME: This shouldn't exist pub(crate) fn previous_token_is(&self, kind: SyntaxKind) -> bool { self.previous_token.as_ref().map_or(false, |tok| tok.kind() == kind) } @@ -406,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, }) } @@ -556,7 +556,6 @@ impl<'a> CompletionContext<'a> { ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None }, pattern_ctx: None, qualifier_ctx: Default::default(), - existing_derives: Default::default(), locals, }; ctx.expand_and_fill( diff --git a/crates/ide-completion/src/context/analysis.rs b/crates/ide-completion/src/context/analysis.rs index 8d38677df6..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> { @@ -339,14 +339,6 @@ impl<'a> CompletionContext<'a> { // Overwrite the path kind for derives if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx { - self.existing_derives = self - .sema - .resolve_derive_macro(&origin_attr) - .into_iter() - .flatten() - .flatten() - .collect(); - if let Some(ast::NameLike::NameRef(name_ref)) = find_node_at_offset(&file_with_fake_ident, offset) { @@ -354,7 +346,15 @@ impl<'a> CompletionContext<'a> { let (mut nameref_ctx, _, _) = Self::classify_name_ref(&self.sema, &original_file, name_ref, parent); if let Some(NameRefKind::Path(path_ctx)) = &mut nameref_ctx.kind { - path_ctx.kind = PathKind::Derive; + path_ctx.kind = PathKind::Derive { + existing_derives: self + .sema + .resolve_derive_macro(&origin_attr) + .into_iter() + .flatten() + .flatten() + .collect(), + }; } self.ident_ctx = IdentContext::NameRef(nameref_ctx); return Some(()); @@ -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 { @@ -861,10 +863,6 @@ impl<'a> CompletionContext<'a> { .and_then(|it| find_node_in_file(original_file, &it)) .map(|it| it.parent_path()); if let Some(path) = path { - let res = sema.resolve_path(&path); - let is_super_chain = iter::successors(Some(path.clone()), |p| p.qualifier()) - .all(|p| p.segment().and_then(|s| s.super_token()).is_some()); - // `<_>::$0` let is_infer_qualifier = path.qualifier().is_none() && matches!( @@ -875,13 +873,15 @@ impl<'a> CompletionContext<'a> { }) ); - path_ctx.qualified = Qualified::With(PathQualifierCtx { - path, - resolution: res, - is_super_chain, - use_tree_parent, - is_infer_qualifier, - }) + path_ctx.qualified = if is_infer_qualifier { + Qualified::Infer + } else { + let res = sema.resolve_path(&path); + 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 { path, resolution: res, is_super_chain } + } }; } } else if let Some(segment) = path.segment() {