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 @@
};
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,
};
@ -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)

View File

@ -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,
}
};

View File

@ -101,7 +101,9 @@ pub(super) enum PathKind {
kind: AttrKind,
annotated_item_kind: Option<SyntaxKind>,
},
Derive,
Derive {
existing_derives: FxHashSet<hir::Macro>,
},
/// Path in item position, that is inside an (Assoc)ItemList
Item {
kind: ItemListKind,
@ -332,8 +334,6 @@ pub(crate) struct CompletionContext<'a> {
pub(super) pattern_ctx: Option<PatternContext>,
pub(super) qualifier_ctx: QualifierCtx,
pub(super) existing_derives: FxHashSet<hir::Macro>,
pub(super) locals: FxHashMap<Name, Local>,
}
@ -556,7 +556,6 @@ pub(super) fn new(
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(

View File

@ -339,14 +339,6 @@ fn fill(
// 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 @@ fn fill(
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(());