aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/src.rs
blob: 20200d1db49d0b392ba92833bf32011b8d92684a (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
98
99
100
101
102
//! Utilities for mapping between hir IDs and the surface syntax.

use hir_expand::InFile;
use ra_arena::map::ArenaMap;
use ra_syntax::ast;

use crate::{
    db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc,
    TypeAliasLoc, UnionLoc,
};

pub trait HasSource {
    type Value;
    fn source(&self, db: &impl DefDatabase) -> InFile<Self::Value>;
}

impl HasSource for FunctionLoc {
    type Value = ast::FnDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::FnDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for TypeAliasLoc {
    type Value = ast::TypeAliasDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for ConstLoc {
    type Value = ast::ConstDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::ConstDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for StaticLoc {
    type Value = ast::StaticDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::StaticDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for ImplLoc {
    type Value = ast::ImplBlock;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for TraitLoc {
    type Value = ast::TraitDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for StructLoc {
    type Value = ast::StructDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for UnionLoc {
    type Value = ast::UnionDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

impl HasSource for EnumLoc {
    type Value = ast::EnumDef;

    fn source(&self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
        let node = self.ast_id.to_node(db);
        InFile::new(self.ast_id.file_id, node)
    }
}

pub trait HasChildSource {
    type ChildId;
    type Value;
    fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
}