Move existing_derives into PathKind::Derive

This commit is contained in:
Lukas Wirth 2022-06-17 16:56:21 +02:00
parent 531060f103
commit 2f2ea77d88
4 changed files with 23 additions and 22 deletions

View file

@ -11,8 +11,10 @@ use crate::{
}; };
pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) { pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let qualified = match ctx.path_context() { let (qualified, existing_derives) = match ctx.path_context() {
Some(&PathCompletionCtx { kind: PathKind::Derive, ref qualified, .. }) => qualified, Some(PathCompletionCtx {
kind: PathKind::Derive { existing_derives }, qualified, ..
}) => (qualified, existing_derives),
_ => return, _ => return,
}; };
@ -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)) { for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
let add_def = match def { let add_def = match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => { 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, ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true,
_ => false, _ => false,
@ -48,7 +50,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
ctx.process_all_names(&mut |name, def| { ctx.process_all_names(&mut |name, def| {
let mac = match def { let mac = match def {
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) 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 mac
} }
@ -74,7 +76,7 @@ pub(crate) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext) {
let mut components = vec![derive_completion.label]; let mut components = vec![derive_completion.label];
components.extend(derive_completion.dependencies.iter().filter( components.extend(derive_completion.dependencies.iter().filter(
|&&dependency| { |&&dependency| {
!ctx.existing_derives !existing_derives
.iter() .iter()
.map(|it| it.name(ctx.db)) .map(|it| it.name(ctx.db))
.any(|it| it.to_smol_str() == dependency) .any(|it| it.to_smol_str() == dependency)

View file

@ -120,7 +120,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
kind @ (PathKind::Expr { .. } kind @ (PathKind::Expr { .. }
| PathKind::Type { .. } | PathKind::Type { .. }
| PathKind::Attr { .. } | PathKind::Attr { .. }
| PathKind::Derive | PathKind::Derive { .. }
| PathKind::Pat), | 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 { .. }, ItemInNs::Macros(mac)) => mac.is_attr(ctx.db),
(PathKind::Attr { .. }, _) => false, (PathKind::Attr { .. }, _) => false,
(PathKind::Derive, ItemInNs::Macros(mac)) => { (PathKind::Derive { existing_derives }, ItemInNs::Macros(mac)) => {
mac.is_derive(ctx.db) && !ctx.existing_derives.contains(&mac) mac.is_derive(ctx.db) && !existing_derives.contains(&mac)
} }
(PathKind::Derive, _) => false, (PathKind::Derive { .. }, _) => false,
} }
}; };

View file

@ -101,7 +101,9 @@ pub(super) enum PathKind {
kind: AttrKind, kind: AttrKind,
annotated_item_kind: Option<SyntaxKind>, annotated_item_kind: Option<SyntaxKind>,
}, },
Derive, Derive {
existing_derives: FxHashSet<hir::Macro>,
},
/// Path in item position, that is inside an (Assoc)ItemList /// Path in item position, that is inside an (Assoc)ItemList
Item { Item {
kind: ItemListKind, kind: ItemListKind,
@ -332,8 +334,6 @@ pub(crate) struct CompletionContext<'a> {
pub(super) pattern_ctx: Option<PatternContext>, pub(super) pattern_ctx: Option<PatternContext>,
pub(super) qualifier_ctx: QualifierCtx, pub(super) qualifier_ctx: QualifierCtx,
pub(super) existing_derives: FxHashSet<hir::Macro>,
pub(super) locals: FxHashMap<Name, Local>, pub(super) locals: FxHashMap<Name, Local>,
} }
@ -556,7 +556,6 @@ impl<'a> CompletionContext<'a> {
ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None }, ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None },
pattern_ctx: None, pattern_ctx: None,
qualifier_ctx: Default::default(), qualifier_ctx: Default::default(),
existing_derives: Default::default(),
locals, locals,
}; };
ctx.expand_and_fill( ctx.expand_and_fill(

View file

@ -339,14 +339,6 @@ impl<'a> CompletionContext<'a> {
// Overwrite the path kind for derives // Overwrite the path kind for derives
if let Some((original_file, file_with_fake_ident, offset, origin_attr)) = derive_ctx { 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)) = if let Some(ast::NameLike::NameRef(name_ref)) =
find_node_at_offset(&file_with_fake_ident, offset) find_node_at_offset(&file_with_fake_ident, offset)
{ {
@ -354,7 +346,15 @@ impl<'a> CompletionContext<'a> {
let (mut nameref_ctx, _, _) = let (mut nameref_ctx, _, _) =
Self::classify_name_ref(&self.sema, &original_file, name_ref, parent); Self::classify_name_ref(&self.sema, &original_file, name_ref, parent);
if let Some(NameRefKind::Path(path_ctx)) = &mut nameref_ctx.kind { 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); self.ident_ctx = IdentContext::NameRef(nameref_ctx);
return Some(()); return Some(());