Skip to content

Commit 1caaa20

Browse files
committed
Remove hir_def/docs.rs module
1 parent b3652ef commit 1caaa20

File tree

6 files changed

+63
-114
lines changed

6 files changed

+63
-114
lines changed

crates/hir/src/attrs.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Attributes & documentation for hir types.
22
use hir_def::{
3-
attr::Attrs, docs::Documentation, path::ModPath, resolver::HasResolver, AttrDefId, ModuleDefId,
3+
attr::{Attrs, Documentation},
4+
path::ModPath,
5+
resolver::HasResolver,
6+
AttrDefId, ModuleDefId,
47
};
58
use hir_expand::hygiene::Hygiene;
69
use hir_ty::db::HirDatabase;

crates/hir/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ pub use crate::{
4444

4545
pub use hir_def::{
4646
adt::StructKind,
47-
attr::Attrs,
47+
attr::{Attrs, Documentation},
4848
body::scope::ExprScopes,
4949
builtin_type::BuiltinType,
50-
docs::Documentation,
5150
find_path::PrefixKind,
5251
import_map,
5352
item_scope::ItemInNs,

crates/hir_def/src/attr.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ use tt::Subtree;
1515

1616
use crate::{
1717
db::DefDatabase,
18-
docs::Documentation,
1918
item_tree::{ItemTreeId, ItemTreeNode},
2019
nameres::ModuleSource,
2120
path::ModPath,
2221
src::HasChildSource,
2322
AdtId, AttrDefId, Lookup,
2423
};
2524

25+
/// Holds documentation
26+
#[derive(Debug, Clone, PartialEq, Eq)]
27+
pub struct Documentation(Arc<str>);
28+
29+
impl Documentation {
30+
pub fn as_str(&self) -> &str {
31+
&self.0
32+
}
33+
}
34+
35+
impl Into<String> for Documentation {
36+
fn into(self) -> String {
37+
self.as_str().to_owned()
38+
}
39+
}
40+
2641
#[derive(Default, Debug, Clone, PartialEq, Eq)]
2742
pub struct Attrs {
2843
entries: Option<Arc<[Attr]>>,
@@ -102,7 +117,7 @@ impl Attrs {
102117
},
103118
);
104119
let mut attrs = owner.attrs().peekable();
105-
let entries = if attrs.peek().is_none() {
120+
let entries = if attrs.peek().is_none() && docs.is_none() {
106121
// Avoid heap allocation
107122
None
108123
} else {
@@ -154,7 +169,11 @@ impl Attrs {
154169
.intersperse(&SmolStr::new_inline("\n"))
155170
// No FromIterator<SmolStr> for String
156171
.for_each(|s| docs.push_str(s.as_str()));
157-
if docs.is_empty() { None } else { Some(docs) }.map(|it| Documentation::new(&it))
172+
if docs.is_empty() {
173+
None
174+
} else {
175+
Some(Documentation(docs.into()))
176+
}
158177
}
159178
}
160179

crates/hir_def/src/docs.rs

Lines changed: 0 additions & 79 deletions
This file was deleted.

crates/hir_def/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub mod adt;
3131
pub mod data;
3232
pub mod generics;
3333
pub mod lang_item;
34-
pub mod docs;
3534

3635
pub mod expr;
3736
pub mod body;

crates/ide/src/hover.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::{
2-
Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
3-
Module, ModuleDef, ModuleSource, Semantics,
2+
Adt, AsAssocItem, AssocItemContainer, FieldSource, HasAttrs, HasSource, HirDisplay, Module,
3+
ModuleDef, ModuleSource, Semantics,
44
};
55
use ide_db::base_db::SourceDatabase;
66
use ide_db::{
@@ -319,31 +319,27 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
319319
let mod_path = definition_mod_path(db, &def);
320320
return match def {
321321
Definition::Macro(it) => {
322-
let src = it.source(db);
323-
let docs = Documentation::from_ast(&src.value).map(Into::into);
324-
hover_markup(docs, Some(macro_label(&src.value)), mod_path)
322+
let label = macro_label(&it.source(db).value);
323+
from_def_source_labeled(db, it, Some(label), mod_path)
325324
}
326-
Definition::Field(it) => {
327-
let src = it.source(db);
328-
match src.value {
329-
FieldSource::Named(it) => {
330-
let docs = Documentation::from_ast(&it).map(Into::into);
331-
hover_markup(docs, it.short_label(), mod_path)
332-
}
333-
_ => None,
325+
Definition::Field(def) => {
326+
let src = def.source(db).value;
327+
if let FieldSource::Named(it) = src {
328+
from_def_source_labeled(db, def, it.short_label(), mod_path)
329+
} else {
330+
None
334331
}
335332
}
336333
Definition::ModuleDef(it) => match it {
337-
ModuleDef::Module(it) => match it.definition_source(db).value {
338-
ModuleSource::Module(it) => {
339-
let docs = Documentation::from_ast(&it).map(Into::into);
340-
hover_markup(docs, it.short_label(), mod_path)
341-
}
342-
ModuleSource::SourceFile(it) => {
343-
let docs = Documentation::from_ast(&it).map(Into::into);
344-
hover_markup(docs, it.short_label(), mod_path)
345-
}
346-
},
334+
ModuleDef::Module(it) => from_def_source_labeled(
335+
db,
336+
it,
337+
match it.definition_source(db).value {
338+
ModuleSource::Module(it) => it.short_label(),
339+
ModuleSource::SourceFile(it) => it.short_label(),
340+
},
341+
mod_path,
342+
),
347343
ModuleDef::Function(it) => from_def_source(db, it, mod_path),
348344
ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path),
349345
ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path),
@@ -371,12 +367,24 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
371367

372368
fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>
373369
where
374-
D: HasSource<Ast = A>,
375-
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
370+
D: HasSource<Ast = A> + HasAttrs + Copy,
371+
A: ShortLabel,
372+
{
373+
let short_label = def.source(db).value.short_label();
374+
from_def_source_labeled(db, def, short_label, mod_path)
375+
}
376+
377+
fn from_def_source_labeled<D>(
378+
db: &RootDatabase,
379+
def: D,
380+
short_label: Option<String>,
381+
mod_path: Option<String>,
382+
) -> Option<Markup>
383+
where
384+
D: HasAttrs,
376385
{
377-
let src = def.source(db);
378-
let docs = Documentation::from_ast(&src.value).map(Into::into);
379-
hover_markup(docs, src.value.short_label(), mod_path)
386+
let docs = def.attrs(db).docs().map(Into::into);
387+
hover_markup(docs, short_label, mod_path)
380388
}
381389
}
382390

0 commit comments

Comments
 (0)