aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/docs.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2020-12-07 17:49:03 +0000
committerLukas Wirth <[email protected]>2020-12-07 18:58:17 +0000
commit1caaa201fa55caaedaa124d23934c178bdf15b18 (patch)
tree2b4dcb3c1df22ad3bd05034e314ad0a35ca0d2c0 /crates/hir_def/src/docs.rs
parentb3652ef2886e01f772559aa90df4c45e7c7fb1fd (diff)
Remove hir_def/docs.rs module
Diffstat (limited to 'crates/hir_def/src/docs.rs')
-rw-r--r--crates/hir_def/src/docs.rs79
1 files changed, 0 insertions, 79 deletions
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 @@
1//! Defines hir documentation.
2//!
3//! This really shouldn't exist, instead, we should deshugar doc comments into attributes, see
4//! https://github.com/rust-analyzer/rust-analyzer/issues/2148#issuecomment-550519102
5
6use std::sync::Arc;
7
8use itertools::Itertools;
9use syntax::{ast, SmolStr};
10
11/// Holds documentation
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct Documentation(Arc<str>);
14
15impl Into<String> for Documentation {
16 fn into(self) -> String {
17 self.as_str().to_owned()
18 }
19}
20
21impl Documentation {
22 pub fn new(s: &str) -> Documentation {
23 Documentation(s.into())
24 }
25
26 pub fn from_ast<N>(node: &N) -> Option<Documentation>
27 where
28 N: ast::DocCommentsOwner + ast::AttrsOwner,
29 {
30 docs_from_ast(node)
31 }
32
33 pub fn as_str(&self) -> &str {
34 &*self.0
35 }
36}
37
38pub(crate) fn docs_from_ast<N>(node: &N) -> Option<Documentation>
39where
40 N: ast::DocCommentsOwner + ast::AttrsOwner,
41{
42 let doc_comment_text = node.doc_comment_text();
43 let doc_attr_text = expand_doc_attrs(node);
44 let docs = merge_doc_comments_and_attrs(doc_comment_text, doc_attr_text);
45 docs.map(|it| Documentation::new(&it))
46}
47
48fn merge_doc_comments_and_attrs(
49 doc_comment_text: Option<String>,
50 doc_attr_text: Option<String>,
51) -> Option<String> {
52 match (doc_comment_text, doc_attr_text) {
53 (Some(mut comment_text), Some(attr_text)) => {
54 comment_text.reserve(attr_text.len() + 1);
55 comment_text.push('\n');
56 comment_text.push_str(&attr_text);
57 Some(comment_text)
58 }
59 (Some(comment_text), None) => Some(comment_text),
60 (None, Some(attr_text)) => Some(attr_text),
61 (None, None) => None,
62 }
63}
64
65fn expand_doc_attrs(owner: &dyn ast::AttrsOwner) -> Option<String> {
66 let mut docs = String::new();
67 owner
68 .attrs()
69 .filter_map(|attr| attr.as_simple_key_value().filter(|(key, _)| key == "doc"))
70 .map(|(_, value)| value)
71 .intersperse(SmolStr::new_inline("\n"))
72 // No FromIterator<SmolStr> for String
73 .for_each(|s| docs.push_str(s.as_str()));
74 if docs.is_empty() {
75 None
76 } else {
77 Some(docs)
78 }
79}