Remove hir_def/docs.rs module

This commit is contained in:
Lukas Wirth 2020-12-07 18:49:03 +01:00
parent b3652ef288
commit 1caaa201fa
6 changed files with 63 additions and 114 deletions

View file

@ -1,6 +1,9 @@
//! Attributes & documentation for hir types. //! Attributes & documentation for hir types.
use hir_def::{ use hir_def::{
attr::Attrs, docs::Documentation, path::ModPath, resolver::HasResolver, AttrDefId, ModuleDefId, attr::{Attrs, Documentation},
path::ModPath,
resolver::HasResolver,
AttrDefId, ModuleDefId,
}; };
use hir_expand::hygiene::Hygiene; use hir_expand::hygiene::Hygiene;
use hir_ty::db::HirDatabase; use hir_ty::db::HirDatabase;

View file

@ -44,10 +44,9 @@ pub use crate::{
pub use hir_def::{ pub use hir_def::{
adt::StructKind, adt::StructKind,
attr::Attrs, attr::{Attrs, Documentation},
body::scope::ExprScopes, body::scope::ExprScopes,
builtin_type::BuiltinType, builtin_type::BuiltinType,
docs::Documentation,
find_path::PrefixKind, find_path::PrefixKind,
import_map, import_map,
item_scope::ItemInNs, item_scope::ItemInNs,

View file

@ -15,7 +15,6 @@ use tt::Subtree;
use crate::{ use crate::{
db::DefDatabase, db::DefDatabase,
docs::Documentation,
item_tree::{ItemTreeId, ItemTreeNode}, item_tree::{ItemTreeId, ItemTreeNode},
nameres::ModuleSource, nameres::ModuleSource,
path::ModPath, path::ModPath,
@ -23,6 +22,22 @@ use crate::{
AdtId, AttrDefId, Lookup, AdtId, AttrDefId, Lookup,
}; };
/// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Documentation {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Into<String> for Documentation {
fn into(self) -> String {
self.as_str().to_owned()
}
}
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Attrs { pub struct Attrs {
entries: Option<Arc<[Attr]>>, entries: Option<Arc<[Attr]>>,
@ -102,7 +117,7 @@ impl Attrs {
}, },
); );
let mut attrs = owner.attrs().peekable(); let mut attrs = owner.attrs().peekable();
let entries = if attrs.peek().is_none() { let entries = if attrs.peek().is_none() && docs.is_none() {
// Avoid heap allocation // Avoid heap allocation
None None
} else { } else {
@ -154,7 +169,11 @@ impl Attrs {
.intersperse(&SmolStr::new_inline("\n")) .intersperse(&SmolStr::new_inline("\n"))
// No FromIterator<SmolStr> for String // No FromIterator<SmolStr> for String
.for_each(|s| docs.push_str(s.as_str())); .for_each(|s| docs.push_str(s.as_str()));
if docs.is_empty() { None } else { Some(docs) }.map(|it| Documentation::new(&it)) if docs.is_empty() {
None
} else {
Some(Documentation(docs.into()))
}
} }
} }

View file

@ -1,79 +0,0 @@
//! Defines hir documentation.
//!
//! This really shouldn't exist, instead, we should deshugar doc comments into attributes, see
//! https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102
use std::sync::Arc;
use itertools::Itertools;
use syntax::{ast, SmolStr};
/// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);
impl Into<String> for Documentation {
fn into(self) -> String {
self.as_str().to_owned()
}
}
impl Documentation {
pub fn new(s: &str) -> Documentation {
Documentation(s.into())
}
pub fn from_ast<N>(node: &N) -> Option<Documentation>
where
N: ast::DocCommentsOwner + ast::AttrsOwner,
{
docs_from_ast(node)
}
pub fn as_str(&self) -> &str {
&*self.0
}
}
pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation>
where
N: ast::DocCommentsOwner + ast::AttrsOwner,
{
let doc_comment_text = node.doc_comment_text();
let doc_attr_text = expand_doc_attrs(node);
let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
docs.map(|it| Documentation::new(&it))
}
fn merge_doc_comments_and_attrs(
doc_comment_text: Option<String>,
doc_attr_text: Option<String>,
) -> Option<String> {
match (doc_comment_text, doc_attr_text) {
(Some(mut comment_text), Some(attr_text)) => {
comment_text.reserve(attr_text.len() + 1);
comment_text.push('\n');
comment_text.push_str(&attr_text);
Some(comment_text)
}
(Some(comment_text), None) => Some(comment_text),
(None, Some(attr_text)) => Some(attr_text),
(None, None) => None,
}
}
fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
let mut docs = String::new();
owner
.attrs()
.filter_map(|attr| attr.as_simple_key_value().filter(|(key, _)| key == "doc"))
.map(|(_, value)| value)
.intersperse(SmolStr::new_inline("\n"))
// No FromIterator<SmolStr> for String
.for_each(|s| docs.push_str(s.as_str()));
if docs.is_empty() {
None
} else {
Some(docs)
}
}

View file

@ -31,7 +31,6 @@ pub mod adt;
pub mod data; pub mod data;
pub mod generics; pub mod generics;
pub mod lang_item; pub mod lang_item;
pub mod docs;
pub mod expr; pub mod expr;
pub mod body; pub mod body;

View file

@ -1,6 +1,6 @@
use hir::{ use hir::{
Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay, Adt, AsAssocItem, AssocItemContainer, FieldSource, HasAttrs, HasSource, HirDisplay, Module,
Module, ModuleDef, ModuleSource, Semantics, ModuleDef, ModuleSource, Semantics,
}; };
use ide_db::base_db::SourceDatabase; use ide_db::base_db::SourceDatabase;
use ide_db::{ use ide_db::{
@ -319,31 +319,27 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
let mod_path = definition_mod_path(db, &def); let mod_path = definition_mod_path(db, &def);
return match def { return match def {
Definition::Macro(it) => { Definition::Macro(it) => {
let src = it.source(db); let label = macro_label(&it.source(db).value);
let docs = Documentation::from_ast(&src.value).map(Into::into); from_def_source_labeled(db, it, Some(label), mod_path)
hover_markup(docs, Some(macro_label(&src.value)), mod_path)
} }
Definition::Field(it) => { Definition::Field(def) => {
let src = it.source(db); let src = def.source(db).value;
match src.value { if let FieldSource::Named(it) = src {
FieldSource::Named(it) => { from_def_source_labeled(db, def, it.short_label(), mod_path)
let docs = Documentation::from_ast(&it).map(Into::into); } else {
hover_markup(docs, it.short_label(), mod_path) None
}
_ => None,
} }
} }
Definition::ModuleDef(it) => match it { Definition::ModuleDef(it) => match it {
ModuleDef::Module(it) => match it.definition_source(db).value { ModuleDef::Module(it) => from_def_source_labeled(
ModuleSource::Module(it) => { db,
let docs = Documentation::from_ast(&it).map(Into::into); it,
hover_markup(docs, it.short_label(), mod_path) match it.definition_source(db).value {
} ModuleSource::Module(it) => it.short_label(),
ModuleSource::SourceFile(it) => { ModuleSource::SourceFile(it) => it.short_label(),
let docs = Documentation::from_ast(&it).map(Into::into); },
hover_markup(docs, it.short_label(), mod_path) mod_path,
} ),
},
ModuleDef::Function(it) => from_def_source(db, it, mod_path), ModuleDef::Function(it) => from_def_source(db, it, mod_path),
ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path),
ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path), 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> {
fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>
where where
D: HasSource<Ast = A>, D: HasSource<Ast = A> + HasAttrs + Copy,
A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner, A: ShortLabel,
{ {
let src = def.source(db); let short_label = def.source(db).value.short_label();
let docs = Documentation::from_ast(&src.value).map(Into::into); from_def_source_labeled(db, def, short_label, mod_path)
hover_markup(docs, src.value.short_label(), mod_path) }
fn from_def_source_labeled<D>(
db: &RootDatabase,
def: D,
short_label: Option<String>,
mod_path: Option<String>,
) -> Option<Markup>
where
D: HasAttrs,
{
let docs = def.attrs(db).docs().map(Into::into);
hover_markup(docs, short_label, mod_path)
} }
} }