Move ide crates to new hir::Macro

This commit is contained in:
Lukas Wirth 2022-03-08 23:52:26 +01:00
parent c04b0f435b
commit eba90936c1
24 changed files with 61 additions and 67 deletions

View file

@ -180,7 +180,7 @@ impl<'a> DeclValidator<'a> {
AttrDefId::ImplId(iid) => Some(iid.lookup(self.db.upcast()).container.into()),
AttrDefId::ExternBlockId(id) => Some(id.lookup(self.db.upcast()).container.into()),
// These warnings should not explore macro definitions at all
AttrDefId::MacroDefId(_) => None,
AttrDefId::MacroId(_) => None,
AttrDefId::AdtId(aid) => match aid {
AdtId::StructId(sid) => Some(sid.lookup(self.db.upcast()).container.into()),
AdtId::EnumId(eid) => Some(eid.lookup(self.db.upcast()).container.into()),

View file

@ -5,7 +5,6 @@ mod tests;
mod intra_doc_links;
use either::Either;
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options as CMarkOptions};
use stdx::format_to;
@ -173,7 +172,7 @@ pub(crate) fn resolve_doc_path_for_def(
link: &str,
ns: Option<hir::Namespace>,
) -> Option<Definition> {
let def = match def {
match def {
Definition::Module(it) => it.resolve_doc_path(db, link, ns),
Definition::Function(it) => it.resolve_doc_path(db, link, ns),
Definition::Adt(it) => it.resolve_doc_path(db, link, ns),
@ -191,11 +190,8 @@ pub(crate) fn resolve_doc_path_for_def(
| Definition::Local(_)
| Definition::GenericParam(_)
| Definition::Label(_) => None,
}?;
match def {
Either::Left(def) => Some(Definition::from(def)),
Either::Right(def) => Some(Definition::Macro(def)),
}
.map(Definition::from)
}
pub(crate) fn doc_attributes(

View file

@ -211,6 +211,7 @@ impl TryToNav for hir::ModuleDef {
hir::ModuleDef::Static(it) => it.try_to_nav(db),
hir::ModuleDef::Trait(it) => it.try_to_nav(db),
hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
hir::ModuleDef::Macro(it) => it.try_to_nav(db),
hir::ModuleDef::BuiltinType(_) => None,
}
}
@ -332,7 +333,7 @@ impl TryToNav for hir::Field {
}
}
impl TryToNav for hir::MacroDef {
impl TryToNav for hir::Macro {
fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> {
let src = self.source(db)?;
let name_owner: &dyn ast::HasName = match &src.value {
@ -343,7 +344,7 @@ impl TryToNav for hir::MacroDef {
let mut res = NavigationTarget::from_named(
db,
src.as_ref().with_value(name_owner),
self.kind().into(),
self.kind(db).into(),
);
res.docs = self.docs(db);
Some(res)

View file

@ -320,7 +320,7 @@ fn highlight_def(
) -> Highlight {
let db = sema.db;
let mut h = match def {
Definition::Macro(m) => Highlight::new(HlTag::Symbol(m.kind().into())),
Definition::Macro(m) => Highlight::new(HlTag::Symbol(m.kind(sema.db).into())),
Definition::Field(_) => Highlight::new(HlTag::Symbol(SymbolKind::Field)),
Definition::Module(module) => {
let mut h = Highlight::new(HlTag::Symbol(SymbolKind::Module));

View file

@ -130,9 +130,6 @@ impl Ref {
ScopeDef::ModuleDef(def) => {
Some(Ref { visible_name: name, def: Definition::from(def) })
}
ScopeDef::MacroDef(def) => {
Some(Ref { visible_name: name, def: Definition::Macro(def) })
}
_ => None,
}
}

View file

@ -199,6 +199,8 @@ fn target_data_for_def(
let syntax = in_file_source.value.syntax();
(vis_offset(syntax), in_file_source.value.visibility(), syntax.text_range(), file_id)
}
// FIXME
hir::ModuleDef::Macro(_) => return None,
// Enum variants can't be private, we can't modify builtin types
hir::ModuleDef::Variant(_) | hir::ModuleDef::BuiltinType(_) => return None,
};

View file

@ -44,7 +44,6 @@ pub(crate) fn replace_qualified_name_with_use(
// only offer replacement for non assoc items
match ctx.sema.resolve_path(&path)? {
hir::PathResolution::Def(def) if def.as_assoc_item(ctx.sema.db).is_none() => (),
hir::PathResolution::Macro(_) => (),
_ => return None,
}
// then search for an import for the first path segment of what we want to replace

View file

@ -21,7 +21,7 @@ pub(crate) mod vis;
use std::iter;
use hir::{known, ScopeDef};
use hir::{db::HirDatabase, known, ScopeDef};
use ide_db::SymbolKind;
use crate::{
@ -40,17 +40,17 @@ use crate::{
CompletionContext, CompletionItem, CompletionItemKind,
};
fn module_or_attr(def: ScopeDef) -> Option<ScopeDef> {
fn module_or_attr(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
match def {
ScopeDef::MacroDef(mac) if mac.is_attr() => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(db) => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}
}
fn module_or_fn_macro(def: ScopeDef) -> Option<ScopeDef> {
fn module_or_fn_macro(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
match def {
ScopeDef::MacroDef(mac) if mac.is_fn_like() => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(db) => Some(def),
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}

View file

@ -93,7 +93,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
};
for (name, def) in module.scope(ctx.db, ctx.module) {
if let Some(def) = module_or_attr(def) {
if let Some(def) = module_or_attr(ctx.db, def) {
acc.add_resolution(ctx, name, def);
}
}
@ -104,7 +104,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
// only show modules in a fresh UseTree
None => {
ctx.process_all_names(&mut |name, def| {
if let Some(def) = module_or_attr(def) {
if let Some(def) = module_or_attr(ctx.db, def) {
acc.add_resolution(ctx, name, def);
}
});

View file

@ -1,5 +1,5 @@
//! Completion for derives
use hir::{HasAttrs, MacroDef, MacroKind};
use hir::{HasAttrs, Macro, MacroKind};
use ide_db::{
imports::{import_assets::ImportAssets, insert_use::ImportScope},
SymbolKind,
@ -24,9 +24,9 @@ pub(super) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, at
}
let name = name.to_smol_str();
let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
let (label, lookup) = match (core, mac.module(ctx.db).krate()) {
// show derive dependencies for `core`/`std` derives
Some((core, mac_krate)) if core == mac_krate => {
(Some(core), mac_krate) if core == mac_krate => {
if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
.iter()
.find(|derive_completion| derive_completion.label == name)
@ -63,11 +63,11 @@ pub(super) fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, at
flyimport_derive(acc, ctx);
}
fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, MacroDef)> {
fn get_derives_in_scope(ctx: &CompletionContext) -> Vec<(hir::Name, Macro)> {
let mut result = Vec::default();
ctx.process_all_names(&mut |name, scope_def| {
if let hir::ScopeDef::MacroDef(mac) = scope_def {
if mac.kind() == hir::MacroKind::Derive {
if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) = scope_def {
if mac.kind(ctx.db) == hir::MacroKind::Derive {
result.push((name, mac));
}
}
@ -99,7 +99,7 @@ fn flyimport_derive(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
hir::ItemInNs::Macros(mac) => Some((import, mac)),
_ => None,
})
.filter(|&(_, mac)| mac.kind() == MacroKind::Derive)
.filter(|&(_, mac)| mac.kind(ctx.db) == MacroKind::Derive)
.filter(|&(_, mac)| !ctx.is_item_hidden(&hir::ItemInNs::Macros(mac)))
.sorted_by_key(|(import, _)| {
compute_fuzzy_completion_order_key(&import.import_path, &user_input_lowercased)

View file

@ -146,7 +146,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
Some(kind) => kind,
None => {
return match import.original_item {
ItemInNs::Macros(mac) => mac.is_fn_like(),
ItemInNs::Macros(mac) => mac.is_fn_like(ctx.db),
_ => true,
}
}
@ -160,7 +160,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
(
PathKind::Expr | PathKind::Type | PathKind::Mac | PathKind::Pat,
ItemInNs::Macros(mac),
) => mac.is_fn_like(),
) => mac.is_fn_like(ctx.db),
(PathKind::Mac, _) => true,
(PathKind::Expr, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
@ -171,7 +171,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
(PathKind::Type, ItemInNs::Types(_)) => true,
(PathKind::Type, ItemInNs::Values(_)) => false,
(PathKind::Attr { .. }, ItemInNs::Macros(mac)) => mac.is_attr(),
(PathKind::Attr { .. }, ItemInNs::Macros(mac)) => mac.is_attr(ctx.db),
(PathKind::Attr { .. }, _) => false,
}
};

View file

@ -77,9 +77,9 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
}
hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),
hir::ModuleDef::Const(..) | hir::ModuleDef::Module(..) => refutable,
hir::ModuleDef::Macro(mac) => mac.is_fn_like(ctx.db),
_ => false,
},
hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
Some(hir::Adt::Struct(strukt)) => {
acc.add_struct_pat(ctx, strukt, Some(name.clone()));
@ -117,7 +117,9 @@ fn pattern_path_completion(
let module_scope = module.scope(ctx.db, ctx.module);
for (name, def) in module_scope {
let add_resolution = match def {
ScopeDef::MacroDef(m) if m.is_fn_like() => true,
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => {
mac.is_fn_like(ctx.db)
}
ScopeDef::ModuleDef(_) => true,
_ => false,
};

View file

@ -52,7 +52,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
for (name, def) in module.scope(ctx.db, ctx.module) {
if let Some(def) = module_or_fn_macro(def) {
if let Some(def) = module_or_fn_macro(ctx.db, def) {
acc.add_resolution(ctx, name, def);
}
}
@ -81,7 +81,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
for (name, def) in module_scope {
let add_resolution = match def {
// Don't suggest attribute macros and derives.
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
// no values in type places
ScopeDef::ModuleDef(
hir::ModuleDef::Function(_)

View file

@ -36,7 +36,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
// only show macros in {Assoc}ItemList
ctx.process_all_names(&mut |name, def| {
if let Some(def) = module_or_fn_macro(def) {
if let Some(def) = module_or_fn_macro(ctx.db, def) {
acc.add_resolution(ctx, name, def);
}
});
@ -45,7 +45,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
Some(ImmediateLocation::TypeBound) => {
ctx.process_all_names(&mut |name, res| {
let add_resolution = match res {
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
ScopeDef::ModuleDef(hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_)) => {
true
}
@ -94,7 +94,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
!ctx.previous_token_is(syntax::T![impl]) && !ctx.previous_token_is(syntax::T![for])
}
// Don't suggest attribute macros and derives.
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
// no values in type places
ScopeDef::ModuleDef(
hir::ModuleDef::Function(_)

View file

@ -56,9 +56,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
cov_mark::hit!(dont_complete_current_use);
continue;
}
ScopeDef::ModuleDef(_) | ScopeDef::MacroDef(_) | ScopeDef::Unknown => {
true
}
ScopeDef::ModuleDef(_) | ScopeDef::Unknown => true,
_ => false,
};

View file

@ -171,7 +171,9 @@ fn render_resolution_(
ScopeDef::ModuleDef(Variant(var)) if ctx.completion.pattern_ctx.is_none() => {
return render_variant(ctx, import_to_add, Some(local_name), var, None);
}
ScopeDef::MacroDef(mac) => return render_macro(ctx, import_to_add, local_name, mac),
ScopeDef::ModuleDef(Macro(mac)) => {
return render_macro(ctx, import_to_add, local_name, mac)
}
ScopeDef::Unknown => {
let mut item = CompletionItem::new(
CompletionItemKind::UnresolvedReference,
@ -274,7 +276,6 @@ fn scope_def_docs(db: &RootDatabase, resolution: ScopeDef) -> Option<hir::Docume
fn scope_def_is_deprecated(ctx: &RenderContext<'_>, resolution: ScopeDef) -> bool {
match resolution {
ScopeDef::ModuleDef(it) => ctx.is_deprecated_assoc_item(it),
ScopeDef::MacroDef(it) => ctx.is_deprecated(it),
ScopeDef::GenericParam(it) => ctx.is_deprecated(it),
ScopeDef::AdtSelfType(it) => ctx.is_deprecated(it),
_ => false,

View file

@ -18,7 +18,7 @@ pub(crate) fn render_macro(
ctx: RenderContext<'_>,
import_to_add: Option<ImportEdit>,
name: hir::Name,
macro_: hir::MacroDef,
macro_: hir::Macro,
) -> CompletionItem {
let _p = profile::span("render_macro");
render(ctx, name, macro_, import_to_add)
@ -27,7 +27,7 @@ pub(crate) fn render_macro(
fn render(
ctx @ RenderContext { completion, .. }: RenderContext<'_>,
name: hir::Name,
macro_: hir::MacroDef,
macro_: hir::Macro,
import_to_add: Option<ImportEdit>,
) -> CompletionItem {
let source_range = if completion.is_immediately_after_macro_bang() {
@ -40,14 +40,14 @@ fn render(
let name = name.to_smol_str();
let docs = ctx.docs(macro_);
let docs_str = docs.as_ref().map(Documentation::as_str).unwrap_or_default();
let (bra, ket) =
if macro_.is_fn_like() { guess_macro_braces(&name, docs_str) } else { ("", "") };
let is_fn_like = macro_.is_fn_like(completion.db);
let (bra, ket) = if is_fn_like { guess_macro_braces(&name, docs_str) } else { ("", "") };
let needs_bang = macro_.is_fn_like()
&& !matches!(completion.path_kind(), Some(PathKind::Mac | PathKind::Use));
let needs_bang =
is_fn_like && !matches!(completion.path_kind(), Some(PathKind::Mac | PathKind::Use));
let mut item = CompletionItem::new(
SymbolKind::from(macro_.kind()),
SymbolKind::from(macro_.kind(completion.db)),
source_range,
label(&ctx, needs_bang, bra, ket, &name),
);
@ -103,7 +103,7 @@ fn banged_name(name: &str) -> SmolStr {
SmolStr::from_iter([name, "!"])
}
fn detail(sema: &Semantics<RootDatabase>, macro_: hir::MacroDef) -> Option<String> {
fn detail(sema: &Semantics<RootDatabase>, macro_: hir::Macro) -> Option<String> {
// FIXME: This is parsing the file!
let InFile { file_id, value } = macro_.source(sema.db)?;
let _ = sema.parse_or_expand(file_id);

View file

@ -181,7 +181,6 @@ fn import_edits(
let resolve = |import: &GreenNode| {
let path = ast::Path::cast(SyntaxNode::new_root(import.clone()))?;
let item = match ctx.scope.speculative_resolve(&path)? {
hir::PathResolution::Macro(mac) => mac.into(),
hir::PathResolution::Def(def) => def.into(),
_ => return None,
};

View file

@ -8,7 +8,7 @@
use arrayvec::ArrayVec;
use hir::{
Adt, AsAssocItem, AssocItem, BuiltinAttr, BuiltinType, Const, Field, Function, GenericParam,
HasVisibility, Impl, ItemInNs, Label, Local, MacroDef, Module, ModuleDef, Name, PathResolution,
HasVisibility, Impl, ItemInNs, Label, Local, Macro, Module, ModuleDef, Name, PathResolution,
Semantics, Static, ToolModule, Trait, TypeAlias, Variant, Visibility,
};
use stdx::impl_from;
@ -22,7 +22,7 @@ use crate::RootDatabase;
// FIXME: a more precise name would probably be `Symbol`?
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum Definition {
Macro(MacroDef),
Macro(Macro),
Field(Field),
Module(Module),
Function(Function),
@ -48,7 +48,7 @@ impl Definition {
pub fn module(&self, db: &RootDatabase) -> Option<Module> {
let module = match self {
Definition::Macro(it) => it.module(db)?,
Definition::Macro(it) => it.module(db),
Definition::Module(it) => it.parent(db)?,
Definition::Field(it) => it.parent_def(db).module(db),
Definition::Function(it) => it.module(db),
@ -493,7 +493,6 @@ impl From<PathResolution> for Definition {
PathResolution::Local(local) => Definition::Local(local),
PathResolution::TypeParam(par) => Definition::GenericParam(par.into()),
PathResolution::ConstParam(par) => Definition::GenericParam(par.into()),
PathResolution::Macro(def) => Definition::Macro(def),
PathResolution::SelfType(impl_def) => Definition::SelfType(impl_def),
PathResolution::BuiltinAttr(attr) => Definition::BuiltinAttr(attr),
PathResolution::ToolModule(tool) => Definition::ToolModule(tool),
@ -512,6 +511,7 @@ impl From<ModuleDef> for Definition {
ModuleDef::Static(it) => Definition::Static(it),
ModuleDef::Trait(it) => Definition::Trait(it),
ModuleDef::TypeAlias(it) => Definition::TypeAlias(it),
ModuleDef::Macro(it) => Definition::Macro(it),
ModuleDef::BuiltinType(it) => Definition::BuiltinType(it),
}
}

View file

@ -1,5 +1,5 @@
//! See [`FamousDefs`].
use hir::{Crate, Enum, MacroDef, Module, ScopeDef, Semantics, Trait};
use hir::{Crate, Enum, Macro, Module, ScopeDef, Semantics, Trait};
use crate::RootDatabase;
@ -84,7 +84,7 @@ impl FamousDefs<'_, '_> {
self.find_trait("core:marker:Copy")
}
pub fn core_macros_builtin_derive(&self) -> Option<MacroDef> {
pub fn core_macros_builtin_derive(&self) -> Option<Macro> {
self.find_macro("core:macros:builtin:derive")
}
@ -118,9 +118,9 @@ impl FamousDefs<'_, '_> {
}
}
fn find_macro(&self, path: &str) -> Option<MacroDef> {
fn find_macro(&self, path: &str) -> Option<Macro> {
match self.find_def(path)? {
hir::ScopeDef::MacroDef(it) => Some(it),
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(it)) => Some(it),
_ => None,
}
}

View file

@ -3,7 +3,7 @@
use std::collections::VecDeque;
use base_db::FileId;
use hir::{ItemInNs, MacroDef, ModuleDef, Name, Semantics};
use hir::{ItemInNs, Macro, ModuleDef, Name, Semantics};
use syntax::{
ast::{self, make},
AstToken, SyntaxKind, SyntaxToken, TokenAtOffset,
@ -15,7 +15,7 @@ pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
match item {
ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).name(db),
ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).name(db),
ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).name(db),
ItemInNs::Macros(macro_def_id) => Macro::from(macro_def_id).name(db),
}
}

View file

@ -1,7 +1,7 @@
//! Look up accessible paths for items.
use hir::{
AsAssocItem, AssocItem, AssocItemContainer, Crate, ItemInNs, MacroDef, ModPath, Module,
ModuleDef, PathResolution, PrefixKind, ScopeDef, Semantics, SemanticsScope, Type,
AsAssocItem, AssocItem, AssocItemContainer, Crate, ItemInNs, ModPath, Module, ModuleDef,
PathResolution, PrefixKind, ScopeDef, Semantics, SemanticsScope, Type,
};
use itertools::Itertools;
use rustc_hash::FxHashSet;
@ -432,7 +432,7 @@ fn module_with_segment_name(
let mut current_module = match candidate {
ItemInNs::Types(module_def_id) => ModuleDef::from(module_def_id).module(db),
ItemInNs::Values(module_def_id) => ModuleDef::from(module_def_id).module(db),
ItemInNs::Macros(macro_def_id) => MacroDef::from(macro_def_id).module(db),
ItemInNs::Macros(macro_def_id) => ModuleDef::from(macro_def_id).module(db),
};
while let Some(module) = current_module {
if let Some(module_name) = module.name(db) {

View file

@ -225,7 +225,6 @@ impl<'a> Ctx<'a> {
hir::PathResolution::Local(_)
| hir::PathResolution::ConstParam(_)
| hir::PathResolution::SelfType(_)
| hir::PathResolution::Macro(_)
| hir::PathResolution::AssocItem(_)
| hir::PathResolution::BuiltinAttr(_)
| hir::PathResolution::ToolModule(_) => (),

View file

@ -271,7 +271,7 @@ impl Definition {
}
if let Definition::Macro(macro_def) = self {
return match macro_def.kind() {
return match macro_def.kind(db) {
hir::MacroKind::Declarative => {
if macro_def.attrs(db).by_key("macro_export").exists() {
SearchScope::reverse_dependencies(db, module.krate())