From 1caaa201fa55caaedaa124d23934c178bdf15b18 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 7 Dec 2020 18:49:03 +0100 Subject: Remove hir_def/docs.rs module --- crates/hir_def/src/attr.rs | 25 +++++++++++++-- crates/hir_def/src/docs.rs | 79 ---------------------------------------------- crates/hir_def/src/lib.rs | 1 - 3 files changed, 22 insertions(+), 83 deletions(-) delete mode 100644 crates/hir_def/src/docs.rs (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 7825290e6..98293aad3 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -15,7 +15,6 @@ use tt::Subtree; use crate::{ db::DefDatabase, - docs::Documentation, item_tree::{ItemTreeId, ItemTreeNode}, nameres::ModuleSource, path::ModPath, @@ -23,6 +22,22 @@ use crate::{ AdtId, AttrDefId, Lookup, }; +/// Holds documentation +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Documentation(Arc); + +impl Documentation { + pub fn as_str(&self) -> &str { + &self.0 + } +} + +impl Into for Documentation { + fn into(self) -> String { + self.as_str().to_owned() + } +} + #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Attrs { entries: Option>, @@ -102,7 +117,7 @@ impl Attrs { }, ); 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 None } else { @@ -154,7 +169,11 @@ impl Attrs { .intersperse(&SmolStr::new_inline("\n")) // No FromIterator for String .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())) + } } } diff --git a/crates/hir_def/src/docs.rs b/crates/hir_def/src/docs.rs deleted file mode 100644 index 6a27effef..000000000 --- a/crates/hir_def/src/docs.rs +++ /dev/null @@ -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); - -impl Into 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(node: &N) -> Option - where - N: ast::DocCommentsOwner + ast::AttrsOwner, - { - docs_from_ast(node) - } - - pub fn as_str(&self) -> &str { - &*self.0 - } -} - -pub(crate) fn docs_from_ast(node: &N) -> Option -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, - doc_attr_text: Option, -) -> Option { - 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 { - 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 for String - .for_each(|s| docs.push_str(s.as_str())); - if docs.is_empty() { - None - } else { - Some(docs) - } -} diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index b41c5acb2..02ed30e4d 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -31,7 +31,6 @@ pub mod adt; pub mod data; pub mod generics; pub mod lang_item; -pub mod docs; pub mod expr; pub mod body; -- cgit v1.2.3