aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_impl/module.rs
blob: f7d15c55e9242874d7bccc0f1e24ad2d7cb97fd8 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use ra_db::FileId;
use ra_syntax::{ast, SyntaxNode, TreeArc, AstNode};

use crate::{
    Module, ModuleSource, Problem, Name,
    nameres::{CrateModuleId, ImportId},
    HirDatabase, PersistentHirDatabase,
    HirFileId, SourceItemId,
};

impl ModuleSource {
    pub(crate) fn new(
        db: &impl PersistentHirDatabase,
        file_id: Option<FileId>,
        decl_id: Option<SourceItemId>,
    ) -> ModuleSource {
        match (file_id, decl_id) {
            (Some(file_id), _) => {
                let source_file = db.parse(file_id);
                ModuleSource::SourceFile(source_file)
            }
            (None, Some(item_id)) => {
                let module = db.file_item(item_id);
                let module = ast::Module::cast(&*module).unwrap();
                assert!(module.item_list().is_some(), "expected inline module");
                ModuleSource::Module(module.to_owned())
            }
            (None, None) => panic!(),
        }
    }
}

impl Module {
    fn with_module_id(&self, module_id: CrateModuleId) -> Module {
        Module { module_id, krate: self.krate }
    }

    pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> {
        let def_map = db.crate_def_map(self.krate);
        let parent = def_map[self.module_id].parent?;
        def_map[parent].children.iter().find_map(|(name, module_id)| {
            if *module_id == self.module_id {
                Some(name.clone())
            } else {
                None
            }
        })
    }

    pub(crate) fn definition_source_impl(
        &self,
        db: &impl PersistentHirDatabase,
    ) -> (HirFileId, ModuleSource) {
        let def_map = db.crate_def_map(self.krate);
        let decl_id = def_map[self.module_id].declaration;
        let file_id = def_map[self.module_id].definition;
        let module_source = ModuleSource::new(db, file_id, decl_id);
        let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id);
        (file_id, module_source)
    }

    pub(crate) fn declaration_source_impl(
        &self,
        db: &impl HirDatabase,
    ) -> Option<(HirFileId, TreeArc<ast::Module>)> {
        let def_map = db.crate_def_map(self.krate);
        let decl = def_map[self.module_id].declaration?;
        let syntax_node = db.file_item(decl);
        let ast = ast::Module::cast(&syntax_node).unwrap().to_owned();
        Some((decl.file_id, ast))
    }

    pub(crate) fn import_source_impl(
        &self,
        db: &impl HirDatabase,
        import: ImportId,
    ) -> TreeArc<ast::PathSegment> {
        let (file_id, source) = self.definition_source(db);
        let (_, source_map) = db.raw_items_with_source_map(file_id.original_file(db));
        source_map.get(&source, import)
    }

    pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module {
        let def_map = db.crate_def_map(self.krate);
        self.with_module_id(def_map.root())
    }

    /// Finds a child module with the specified name.
    pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option<Module> {
        let def_map = db.crate_def_map(self.krate);
        let child_id = def_map[self.module_id].children.get(name)?;
        Some(self.with_module_id(*child_id))
    }

    /// Iterates over all child modules.
    pub(crate) fn children_impl(
        &self,
        db: &impl PersistentHirDatabase,
    ) -> impl Iterator<Item = Module> {
        let def_map = db.crate_def_map(self.krate);
        let children = def_map[self.module_id]
            .children
            .iter()
            .map(|(_, module_id)| self.with_module_id(*module_id))
            .collect::<Vec<_>>();
        children.into_iter()
    }

    pub(crate) fn parent_impl(&self, db: &impl PersistentHirDatabase) -> Option<Module> {
        let def_map = db.crate_def_map(self.krate);
        let parent_id = def_map[self.module_id].parent?;
        Some(self.with_module_id(parent_id))
    }

    pub(crate) fn problems_impl(
        &self,
        db: &impl HirDatabase,
    ) -> Vec<(TreeArc<SyntaxNode>, Problem)> {
        let def_map = db.crate_def_map(self.krate);
        let (my_file_id, _) = self.definition_source(db);
        // FIXME: not entirely corret filterint by module
        def_map
            .problems()
            .iter()
            .filter(|(source_item_id, _problem)| my_file_id == source_item_id.file_id)
            .map(|(source_item_id, problem)| (db.file_item(*source_item_id), problem.clone()))
            .collect()
    }
}