Skip to content

Commit 8eddc64

Browse files
committed
Simplify
1 parent b157552 commit 8eddc64

File tree

5 files changed

+157
-156
lines changed

5 files changed

+157
-156
lines changed

crates/hir/src/attrs.rs

+30-66
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,22 @@ use hir_def::{
66
path::{ModPath, Path},
77
per_ns::Namespace,
88
resolver::{HasResolver, Resolver, TypeNs},
9-
AssocItemId, AttrDefId, GenericParamId, ModuleDefId,
9+
AssocItemId, AttrDefId, ModuleDefId,
1010
};
1111
use hir_expand::{hygiene::Hygiene, name::Name};
1212
use hir_ty::db::HirDatabase;
1313
use syntax::{ast, AstNode};
1414

1515
use crate::{
16-
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, Enum, ExternCrateDecl, Field,
17-
Function, GenericParam, Impl, LifetimeParam, Macro, Module, ModuleDef, Static, Struct, Trait,
18-
TraitAlias, TypeAlias, TypeParam, Union, Variant, VariantDef,
16+
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
17+
Field, Function, GenericParam, Impl, LifetimeParam, Macro, Module, ModuleDef, Static, Struct,
18+
Trait, TraitAlias, TypeAlias, TypeParam, Union, Variant, VariantDef,
1919
};
2020

2121
pub trait HasAttrs {
2222
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner;
23-
fn resolve_doc_path(
24-
self,
25-
db: &dyn HirDatabase,
26-
link: &str,
27-
ns: Option<Namespace>,
28-
) -> Option<DocLinkDef>;
29-
}
30-
31-
/// Subset of `ide_db::Definition` that doc links can resolve to.
32-
pub enum DocLinkDef {
33-
ModuleDef(ModuleDef),
34-
Field(Field),
35-
SelfType(Trait),
23+
#[doc(hidden)]
24+
fn attr_id(self) -> AttrDefId;
3625
}
3726

3827
macro_rules! impl_has_attrs {
@@ -42,14 +31,8 @@ macro_rules! impl_has_attrs {
4231
let def = AttrDefId::$def_id(self.into());
4332
db.attrs_with_owner(def)
4433
}
45-
fn resolve_doc_path(
46-
self,
47-
db: &dyn HirDatabase,
48-
link: &str,
49-
ns: Option<Namespace>
50-
) -> Option<DocLinkDef> {
51-
let def = AttrDefId::$def_id(self.into());
52-
resolve_doc_path(db, def, link, ns)
34+
fn attr_id(self) -> AttrDefId {
35+
AttrDefId::$def_id(self.into())
5336
}
5437
}
5538
)*};
@@ -69,6 +52,7 @@ impl_has_attrs![
6952
(Module, ModuleId),
7053
(GenericParam, GenericParamId),
7154
(Impl, ImplId),
55+
(ExternCrateDecl, ExternCrateId),
7256
];
7357

7458
macro_rules! impl_has_attrs_enum {
@@ -77,13 +61,8 @@ macro_rules! impl_has_attrs_enum {
7761
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
7862
$enum::$variant(self).attrs(db)
7963
}
80-
fn resolve_doc_path(
81-
self,
82-
db: &dyn HirDatabase,
83-
link: &str,
84-
ns: Option<Namespace>
85-
) -> Option<DocLinkDef> {
86-
$enum::$variant(self).resolve_doc_path(db, link, ns)
64+
fn attr_id(self) -> AttrDefId {
65+
$enum::$variant(self).attr_id()
8766
}
8867
}
8968
)*};
@@ -100,45 +79,35 @@ impl HasAttrs for AssocItem {
10079
AssocItem::TypeAlias(it) => it.attrs(db),
10180
}
10281
}
103-
104-
fn resolve_doc_path(
105-
self,
106-
db: &dyn HirDatabase,
107-
link: &str,
108-
ns: Option<Namespace>,
109-
) -> Option<DocLinkDef> {
82+
fn attr_id(self) -> AttrDefId {
11083
match self {
111-
AssocItem::Function(it) => it.resolve_doc_path(db, link, ns),
112-
AssocItem::Const(it) => it.resolve_doc_path(db, link, ns),
113-
AssocItem::TypeAlias(it) => it.resolve_doc_path(db, link, ns),
84+
AssocItem::Function(it) => it.attr_id(),
85+
AssocItem::Const(it) => it.attr_id(),
86+
AssocItem::TypeAlias(it) => it.attr_id(),
11487
}
11588
}
11689
}
11790

118-
impl HasAttrs for ExternCrateDecl {
119-
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
120-
let def = AttrDefId::ExternCrateId(self.into());
121-
db.attrs_with_owner(def)
122-
}
123-
fn resolve_doc_path(
124-
self,
125-
db: &dyn HirDatabase,
126-
link: &str,
127-
ns: Option<Namespace>,
128-
) -> Option<DocLinkDef> {
129-
let def = AttrDefId::ExternCrateId(self.into());
130-
resolve_doc_path(db, def, link, ns)
131-
}
91+
/// Resolves the item `link` points to in the scope of `def`.
92+
pub fn resolve_doc_path_on(
93+
db: &dyn HirDatabase,
94+
def: impl HasAttrs,
95+
link: &str,
96+
ns: Option<Namespace>,
97+
) -> Option<DocLinkDef> {
98+
// AttrDefId::FieldId(it) => it.parent.resolver(db.upcast()),
99+
// AttrDefId::EnumVariantId(it) => it.parent.resolver(db.upcast()),
100+
101+
resolve_doc_path_on_(db, link, def.attr_id(), ns)
132102
}
133103

134-
/// Resolves the item `link` points to in the scope of `def`.
135-
fn resolve_doc_path(
104+
fn resolve_doc_path_on_(
136105
db: &dyn HirDatabase,
137-
def: AttrDefId,
138106
link: &str,
107+
attr_id: AttrDefId,
139108
ns: Option<Namespace>,
140109
) -> Option<DocLinkDef> {
141-
let resolver = match def {
110+
let resolver = match attr_id {
142111
AttrDefId::ModuleId(it) => it.resolver(db.upcast()),
143112
AttrDefId::FieldId(it) => it.parent.resolver(db.upcast()),
144113
AttrDefId::AdtId(it) => it.resolver(db.upcast()),
@@ -154,12 +123,7 @@ fn resolve_doc_path(
154123
AttrDefId::UseId(it) => it.resolver(db.upcast()),
155124
AttrDefId::MacroId(it) => it.resolver(db.upcast()),
156125
AttrDefId::ExternCrateId(it) => it.resolver(db.upcast()),
157-
AttrDefId::GenericParamId(it) => match it {
158-
GenericParamId::TypeParamId(it) => it.parent(),
159-
GenericParamId::ConstParamId(it) => it.parent(),
160-
GenericParamId::LifetimeParamId(it) => it.parent,
161-
}
162-
.resolver(db.upcast()),
126+
AttrDefId::GenericParamId(_) => return None,
163127
};
164128

165129
let mut modpath = modpath_from_str(db, link)?;

crates/hir/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ use triomphe::Arc;
8888
use crate::db::{DefDatabase, HirDatabase};
8989

9090
pub use crate::{
91-
attrs::{DocLinkDef, HasAttrs},
91+
attrs::{resolve_doc_path_on, HasAttrs},
9292
diagnostics::{
9393
AnyDiagnostic, BreakOutsideOfLoop, CaseType, ExpectedFunction, InactiveCode,
9494
IncoherentImpl, IncorrectCase, InvalidDeriveTarget, MacroDefError, MacroError,
@@ -4839,3 +4839,10 @@ pub enum ItemContainer {
48394839
ExternBlock(),
48404840
Crate(CrateId),
48414841
}
4842+
4843+
/// Subset of `ide_db::Definition` that doc links can resolve to.
4844+
pub enum DocLinkDef {
4845+
ModuleDef(ModuleDef),
4846+
Field(Field),
4847+
SelfType(Trait),
4848+
}

0 commit comments

Comments
 (0)