about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAaron Loucks <aloucks@cofront.net>2020-06-03 06:14:56 -0400
committerAaron Loucks <aloucks@cofront.net>2020-06-03 06:54:41 -0400
commit85c4edb0afbc7cc855c434e5e7ec69aa469e0c4b (patch)
tree56464d5e77119bafcff03711de24dba775d54beb
parent5837acce532e0cd65a1c0cb8c03cc18a4c22f327 (diff)
downloadrust-85c4edb0afbc7cc855c434e5e7ec69aa469e0c4b.tar.gz
rust-85c4edb0afbc7cc855c434e5e7ec69aa469e0c4b.zip
Consolidate documentation expansion and merging
Removes the duplicated `expand_doc_attrs` and `merge_doc_comments_and_attrs`
functions from `ra_ide` and exposes the same functionality via
`ra_hir::Documentation::from_ast`.
-rw-r--r--crates/ra_hir_def/src/docs.rs7
-rw-r--r--crates/ra_ide/src/hover.rs60
2 files changed, 14 insertions, 53 deletions
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
index ab43cd3d339..2630b3d895e 100644
--- a/crates/ra_hir_def/src/docs.rs
+++ b/crates/ra_hir_def/src/docs.rs
@@ -29,6 +29,13 @@ impl 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
     }
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs
index 731fc367353..9636cd0d6af 100644
--- a/crates/ra_ide/src/hover.rs
+++ b/crates/ra_ide/src/hover.rs
@@ -1,8 +1,8 @@
 use std::iter::once;
 
 use hir::{
-    Adt, AsAssocItem, AssocItemContainer, FieldSource, HasSource, HirDisplay, ModuleDef,
-    ModuleSource, Semantics,
+    Adt, AsAssocItem, AssocItemContainer, Documentation, FieldSource, HasSource, HirDisplay,
+    ModuleDef, ModuleSource, Semantics,
 };
 use itertools::Itertools;
 use ra_db::SourceDatabase;
@@ -10,12 +10,7 @@ use ra_ide_db::{
     defs::{classify_name, classify_name_ref, Definition},
     RootDatabase,
 };
-use ra_syntax::{
-    ast::{self, DocCommentsOwner},
-    match_ast, AstNode,
-    SyntaxKind::*,
-    SyntaxToken, TokenAtOffset,
-};
+use ra_syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset};
 
 use crate::{
     display::{macro_label, rust_code_markup, rust_code_markup_with_doc, ShortLabel},
@@ -169,18 +164,14 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
     return match def {
         Definition::Macro(it) => {
             let src = it.source(db);
-            let doc_comment_text = src.value.doc_comment_text();
-            let doc_attr_text = expand_doc_attrs(&src.value);
-            let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
+            let docs = Documentation::from_ast(&src.value).map(Into::into);
             hover_text(docs, Some(macro_label(&src.value)), mod_path)
         }
         Definition::Field(it) => {
             let src = it.source(db);
             match src.value {
                 FieldSource::Named(it) => {
-                    let doc_comment_text = it.doc_comment_text();
-                    let doc_attr_text = expand_doc_attrs(&it);
-                    let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
+                    let docs = Documentation::from_ast(&it).map(Into::into);
                     hover_text(docs, it.short_label(), mod_path)
                 }
                 _ => None,
@@ -189,9 +180,7 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
         Definition::ModuleDef(it) => match it {
             ModuleDef::Module(it) => match it.definition_source(db).value {
                 ModuleSource::Module(it) => {
-                    let doc_comment_text = it.doc_comment_text();
-                    let doc_attr_text = expand_doc_attrs(&it);
-                    let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
+                    let docs = Documentation::from_ast(&it).map(Into::into);
                     hover_text(docs, it.short_label(), mod_path)
                 }
                 _ => None,
@@ -220,46 +209,11 @@ fn hover_text_from_name_kind(db: &RootDatabase, def: Definition) -> Option<Strin
         A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel + ast::AttrsOwner,
     {
         let src = def.source(db);
-        let doc_comment_text = src.value.doc_comment_text();
-        let doc_attr_text = expand_doc_attrs(&src.value);
-        let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
+        let docs = Documentation::from_ast(&src.value).map(Into::into);
         hover_text(docs, src.value.short_label(), mod_path)
     }
 }
 
-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.push_str("\n\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();
-    for attr in owner.attrs() {
-        if let Some(("doc", value)) =
-            attr.as_simple_key_value().as_ref().map(|(k, v)| (k.as_str(), v.as_str()))
-        {
-            docs.push_str(value);
-            docs.push_str("\n\n");
-        }
-    }
-    if docs.is_empty() {
-        None
-    } else {
-        Some(docs.trim_end_matches("\n\n").to_owned())
-    }
-}
-
 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
     return tokens.max_by_key(priority);
     fn priority(n: &SyntaxToken) -> usize {