aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model/docs.rs
blob: 8533b4f5e6376c40d81d05264216077574cf9d11 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! FIXME: write short doc here

use std::sync::Arc;

use ra_syntax::ast;

use crate::{
    db::{AstDatabase, DefDatabase, HirDatabase},
    Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static,
    Struct, StructField, Trait, TypeAlias, Union,
};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DocDef {
    Module(Module),
    StructField(StructField),
    Adt(Adt),
    EnumVariant(EnumVariant),
    Static(Static),
    Const(Const),
    Function(Function),
    Trait(Trait),
    TypeAlias(TypeAlias),
    MacroDef(MacroDef),
}

impl_froms!(
    DocDef: Module,
    StructField,
    Adt(Struct, Enum, Union),
    EnumVariant,
    Static,
    Const,
    Function,
    Trait,
    TypeAlias,
    MacroDef
);

/// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Documentation(Arc<str>);

impl Documentation {
    fn new(s: &str) -> Documentation {
        Documentation(s.into())
    }

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

impl Into<String> for Documentation {
    fn into(self) -> String {
        self.as_str().to_owned()
    }
}

pub trait Docs {
    fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>;
}

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

pub(crate) fn documentation_query(
    db: &(impl DefDatabase + AstDatabase),
    def: DocDef,
) -> Option<Documentation> {
    match def {
        DocDef::Module(it) => docs_from_ast(&it.declaration_source(db)?.ast),
        DocDef::StructField(it) => match it.source(db).ast {
            FieldSource::Named(named) => docs_from_ast(&named),
            FieldSource::Pos(..) => None,
        },
        DocDef::Adt(it) => match it {
            Adt::Struct(it) => docs_from_ast(&it.source(db).ast),
            Adt::Enum(it) => docs_from_ast(&it.source(db).ast),
            Adt::Union(it) => docs_from_ast(&it.source(db).ast),
        },
        DocDef::EnumVariant(it) => docs_from_ast(&it.source(db).ast),
        DocDef::Static(it) => docs_from_ast(&it.source(db).ast),
        DocDef::Const(it) => docs_from_ast(&it.source(db).ast),
        DocDef::Function(it) => docs_from_ast(&it.source(db).ast),
        DocDef::Trait(it) => docs_from_ast(&it.source(db).ast),
        DocDef::TypeAlias(it) => docs_from_ast(&it.source(db).ast),
        DocDef::MacroDef(it) => docs_from_ast(&it.source(db).ast),
    }
}

impl<T: Into<DocDef> + Copy> Docs for T {
    fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
        db.documentation((*self).into())
    }
}