aboutsummaryrefslogtreecommitdiff
path: root/crates/libanalysis/src/module_map_db/mod.rs
blob: 777f7a38ad4a142ce8531c50f035982755f607a3 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
mod descr;

use std::sync::Arc;
use {
    FileId,
    db::{
        BoxedQuery, Query, QueryCtx
    },
    module_map::resolve_submodule,
};

pub(crate) fn queries(acc: &mut Vec<BoxedQuery>) {
    acc.push(MODULE_DESCR.into());
    acc.push(RESOLVE_SUBMODULE.into());
    acc.push(PARENT_MODULE.into());
}

impl<'a> QueryCtx<'a> {
    fn module_descr(&self, file_id: FileId) -> Arc<descr::ModuleDescr> {
        self.get(MODULE_DESCR, file_id)
    }
    fn resolve_submodule(&self, file_id: FileId, submod: descr::Submodule) -> Arc<Vec<FileId>> {
        self.get(RESOLVE_SUBMODULE, (file_id, submod))
    }
}

pub(crate) const MODULE_DESCR: Query<FileId, descr::ModuleDescr> = Query {
    id: 30,
    f: |ctx, &file_id| {
        let file = ctx.file_syntax(file_id);
        descr::ModuleDescr::new(file.ast())
    }
};

pub(crate) const RESOLVE_SUBMODULE: Query<(FileId, descr::Submodule), Vec<FileId>> = Query {
    id: 31,
    f: |ctx, params| {
        let files = ctx.file_set();
        resolve_submodule(params.0, &params.1.name, &files.1).0
    }
};

pub(crate) const PARENT_MODULE: Query<FileId, Vec<FileId>> = Query {
    id: 40,
    f: |ctx, file_id| {
        let files = ctx.file_set();
        let res = files.0.iter()
            .map(|&parent_id| (parent_id, ctx.module_descr(parent_id)))
            .filter(|(parent_id, descr)| {
                descr.submodules.iter()
                    .any(|subm| {
                        ctx.resolve_submodule(*parent_id, subm.clone())
                            .iter()
                            .any(|it| it == file_id)
                    })
            })
            .map(|(id, _)| id)
            .collect();
        res
    }
};

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use im;
    use relative_path::{RelativePath, RelativePathBuf};
    use {
        db::{Query, Db, State},
        imp::FileResolverImp,
        FileId, FileResolver,
    };
    use super::*;

    #[derive(Debug)]
    struct FileMap(im::HashMap<FileId, RelativePathBuf>);

    impl FileResolver for FileMap {
        fn file_stem(&self, file_id: FileId) -> String {
            self.0[&file_id].file_stem().unwrap().to_string()
        }
        fn resolve(&self, file_id: FileId, rel: &RelativePath) -> Option<FileId> {
            let path = self.0[&file_id].join(rel).normalize();
            self.0.iter()
                .filter_map(|&(id, ref p)| Some(id).filter(|_| p == &path))
                .next()
        }
    }

    struct Fixture {
        next_file_id: u32,
        fm: im::HashMap<FileId, RelativePathBuf>,
        db: Db,
    }

    impl Fixture {
        fn new() -> Fixture {
            Fixture {
                next_file_id: 1,
                fm: im::HashMap::new(),
                db: Db::new(State::default()),
            }
        }
        fn add_file(&mut self, path: &str, text: &str) -> FileId {
            assert!(path.starts_with("/"));
            let file_id = FileId(self.next_file_id);
            self.next_file_id += 1;
            self.fm.insert(file_id, RelativePathBuf::from(&path[1..]));
            let mut new_state = self.db.state().clone();
            new_state.file_map.insert(file_id, text.to_string().into_boxed_str().into());
            new_state.resolver = FileResolverImp::new(
                Arc::new(FileMap(self.fm.clone()))
            );
            self.db = self.db.with_state(new_state, &[file_id], true);
            file_id
        }
        fn remove_file(&mut self, file_id: FileId) {
            self.fm.remove(&file_id);
            let mut new_state = self.db.state().clone();
            new_state.file_map.remove(&file_id);
            new_state.resolver = FileResolverImp::new(
                Arc::new(FileMap(self.fm.clone()))
            );
            self.db = self.db.with_state(new_state, &[file_id], true);
        }
        fn change_file(&mut self, file_id: FileId, new_text: &str) {
            let mut new_state = self.db.state().clone();
            new_state.file_map.insert(file_id, new_text.to_string().into_boxed_str().into());
            self.db = self.db.with_state(new_state, &[file_id], false);
        }
        fn check_parent_modules(
            &self,
            file_id: FileId,
            expected: &[FileId],
            queries: &[(u16, u64)]
        ) {
            let (actual, events) = self.db.get(PARENT_MODULE, file_id);
            assert_eq!(actual.as_slice(), expected);
            let mut counts = HashMap::new();
            events.into_iter()
               .for_each(|event| *counts.entry(event).or_insert(0) += 1);
            for &(query_id, expected_count) in queries.iter() {
                let actual_count = *counts.get(&query_id).unwrap_or(&0);
                assert_eq!(
                    actual_count,
                    expected_count,
                    "counts for {} differ",
                    query_id,
                )
            }

        }
    }

    #[test]
    fn test_parent_module() {
        let mut f = Fixture::new();
        let foo = f.add_file("/foo.rs", "");
        f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]);

        let lib = f.add_file("/lib.rs", "mod foo;");
        f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]);
        f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 0)]);

        f.change_file(lib, "");
        f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]);

        f.change_file(lib, "mod foo;");
        f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]);

        f.change_file(lib, "mod bar;");
        f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 1)]);

        f.change_file(lib, "mod foo;");
        f.check_parent_modules(foo, &[lib], &[(MODULE_DESCR.id, 1)]);

        f.remove_file(lib);
        f.check_parent_modules(foo, &[], &[(MODULE_DESCR.id, 0)]);
    }
}