aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/docs.rs
diff options
context:
space:
mode:
authorSeivan Heidari <[email protected]>2019-11-25 00:54:54 +0000
committerSeivan Heidari <[email protected]>2019-11-25 00:54:54 +0000
commit15ea338ac991707d330288ba4d1bf5daa0fc75d9 (patch)
tree16aeab28bcdb07d36aae28e3fb4a385614865a48 /crates/ra_hir_def/src/docs.rs
parenteb7363d167c7a9f7c73cb950b621eb1dce493318 (diff)
parentf7f9757b6b144385ab8ce57b15764473b1f57331 (diff)
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir_def/src/docs.rs')
-rw-r--r--crates/ra_hir_def/src/docs.rs71
1 files changed, 71 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
new file mode 100644
index 000000000..90a8627bc
--- /dev/null
+++ b/crates/ra_hir_def/src/docs.rs
@@ -0,0 +1,71 @@
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 hir_expand::either::Either;
9use ra_syntax::ast;
10
11use crate::{db::DefDatabase, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup};
12
13/// Holds documentation
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Documentation(Arc<str>);
16
17impl Into<String> for Documentation {
18 fn into(self) -> String {
19 self.as_str().to_owned()
20 }
21}
22
23impl Documentation {
24 fn new(s: &str) -> Documentation {
25 Documentation(s.into())
26 }
27
28 pub fn as_str(&self) -> &str {
29 &*self.0
30 }
31
32 pub(crate) fn documentation_query(
33 db: &impl DefDatabase,
34 def: AttrDefId,
35 ) -> Option<Documentation> {
36 match def {
37 AttrDefId::ModuleId(module) => {
38 let def_map = db.crate_def_map(module.krate);
39 let src = def_map[module.module_id].declaration_source(db)?;
40 docs_from_ast(&src.value)
41 }
42 AttrDefId::StructFieldId(it) => {
43 let src = it.parent.child_source(db);
44 match &src.value[it.local_id] {
45 Either::A(_tuple) => None,
46 Either::B(record) => docs_from_ast(record),
47 }
48 }
49 AttrDefId::AdtId(it) => match it {
50 AdtId::StructId(it) => docs_from_ast(&it.0.source(db).value),
51 AdtId::EnumId(it) => docs_from_ast(&it.source(db).value),
52 AdtId::UnionId(it) => docs_from_ast(&it.0.source(db).value),
53 },
54 AttrDefId::EnumVariantId(it) => {
55 let src = it.parent.child_source(db);
56 docs_from_ast(&src.value[it.local_id])
57 }
58 AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value),
59 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id.to_node(db)),
60 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
61 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
62 AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
63 AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value),
64 AttrDefId::ImplId(_) => None,
65 }
66 }
67}
68
69pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
70 node.doc_comment_text().map(|it| Documentation::new(&it))
71}