aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/docs.rs
blob: 0539a77d40c4172fa9441d1e487ec9cd81bebc0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! 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 either::Either;
use ra_syntax::ast;

use crate::{
    db::DefDatabase,
    src::{HasChildSource, HasSource},
    AdtId, AttrDefId, Lookup,
};

/// 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 {
    fn new(s: &str) -> Documentation {
        Documentation(s.into())
    }

    pub fn as_str(&self) -> &str {
        &*self.0
    }

    pub(crate) fn documentation_query(
        db: &dyn DefDatabase,
        def: AttrDefId,
    ) -> Option<Documentation> {
        match def {
            AttrDefId::ModuleId(module) => {
                let def_map = db.crate_def_map(module.krate);
                let src = def_map[module.local_id].declaration_source(db)?;
                docs_from_ast(&src.value)
            }
            AttrDefId::StructFieldId(it) => {
                let src = it.parent.child_source(db);
                match &src.value[it.local_id] {
                    Either::Left(_tuple) => None,
                    Either::Right(record) => docs_from_ast(record),
                }
            }
            AttrDefId::AdtId(it) => match it {
                AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value),
                AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value),
                AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            },
            AttrDefId::EnumVariantId(it) => {
                let src = it.parent.child_source(db);
                docs_from_ast(&src.value[it.local_id])
            }
            AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db.upcast())),
            AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            AttrDefId::FunctionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            AttrDefId::TypeAliasId(it) => docs_from_ast(&it.lookup(db).source(db).value),
            AttrDefId::ImplId(_) => None,
        }
    }
}

pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> {
    node.doc_comment_text().map(|it| Documentation::new(&it))
}