aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/traits.rs
blob: 877d73d666d7b5fbcc405a18c3beb431400485b3 (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
//! HIR for trait definitions.

use std::sync::Arc;

use hir_expand::{
    name::{AsName, Name},
    AstId,
};

use ra_syntax::ast::{self, NameOwner};

use crate::{
    db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
    TypeAliasLoc,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraitData {
    pub name: Option<Name>,
    pub items: Vec<AssocItemId>,
    pub auto: bool,
}

impl TraitData {
    pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
        let src = tr.source(db);
        let name = src.value.name().map(|n| n.as_name());
        let auto = src.value.is_auto();
        let ast_id_map = db.ast_id_map(src.file_id);
        let items = if let Some(item_list) = src.value.item_list() {
            item_list
                .impl_items()
                .map(|item_node| match item_node {
                    ast::ImplItem::FnDef(it) => FunctionLoc {
                        container: ContainerId::TraitId(tr),
                        ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
                    }
                    .intern(db)
                    .into(),
                    ast::ImplItem::ConstDef(it) => ConstLoc {
                        container: ContainerId::TraitId(tr),
                        ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
                    }
                    .intern(db)
                    .into(),
                    ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
                        container: ContainerId::TraitId(tr),
                        ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
                    }
                    .intern(db)
                    .into(),
                })
                .collect()
        } else {
            Vec::new()
        };
        Arc::new(TraitData { name, items, auto })
    }
}