]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/test_db.rs
Update completions test output
[rust.git] / crates / hir_ty / src / test_db.rs
1 //! Database used for testing `hir`.
2
3 use std::{
4     fmt, panic,
5     sync::{Arc, Mutex},
6 };
7
8 use base_db::{
9     salsa, AnchoredPath, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast,
10 };
11 use hir_def::{db::DefDatabase, ModuleId};
12 use hir_expand::db::AstDatabase;
13 use rustc_hash::{FxHashMap, FxHashSet};
14 use syntax::TextRange;
15 use test_utils::extract_annotations;
16
17 #[salsa::database(
18     base_db::SourceDatabaseExtStorage,
19     base_db::SourceDatabaseStorage,
20     hir_expand::db::AstDatabaseStorage,
21     hir_def::db::InternDatabaseStorage,
22     hir_def::db::DefDatabaseStorage,
23     crate::db::HirDatabaseStorage
24 )]
25 #[derive(Default)]
26 pub(crate) struct TestDB {
27     storage: salsa::Storage<TestDB>,
28     events: Mutex<Option<Vec<salsa::Event>>>,
29 }
30 impl fmt::Debug for TestDB {
31     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32         f.debug_struct("TestDB").finish()
33     }
34 }
35
36 impl Upcast<dyn AstDatabase> for TestDB {
37     fn upcast(&self) -> &(dyn AstDatabase + 'static) {
38         &*self
39     }
40 }
41
42 impl Upcast<dyn DefDatabase> for TestDB {
43     fn upcast(&self) -> &(dyn DefDatabase + 'static) {
44         &*self
45     }
46 }
47
48 impl salsa::Database for TestDB {
49     fn salsa_event(&self, event: salsa::Event) {
50         let mut events = self.events.lock().unwrap();
51         if let Some(events) = &mut *events {
52             events.push(event);
53         }
54     }
55 }
56
57 impl salsa::ParallelDatabase for TestDB {
58     fn snapshot(&self) -> salsa::Snapshot<TestDB> {
59         salsa::Snapshot::new(TestDB {
60             storage: self.storage.snapshot(),
61             events: Default::default(),
62         })
63     }
64 }
65
66 impl panic::RefUnwindSafe for TestDB {}
67
68 impl FileLoader for TestDB {
69     fn file_text(&self, file_id: FileId) -> Arc<String> {
70         FileLoaderDelegate(self).file_text(file_id)
71     }
72     fn resolve_path(&self, path: AnchoredPath) -> Option<FileId> {
73         FileLoaderDelegate(self).resolve_path(path)
74     }
75     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
76         FileLoaderDelegate(self).relevant_crates(file_id)
77     }
78 }
79
80 impl TestDB {
81     pub(crate) fn module_for_file(&self, file_id: FileId) -> ModuleId {
82         for &krate in self.relevant_crates(file_id).iter() {
83             let crate_def_map = self.crate_def_map(krate);
84             for (local_id, data) in crate_def_map.modules.iter() {
85                 if data.origin.file_id() == Some(file_id) {
86                     return ModuleId { krate, local_id };
87                 }
88             }
89         }
90         panic!("Can't find module for file")
91     }
92
93     pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
94         let mut files = Vec::new();
95         let crate_graph = self.crate_graph();
96         for krate in crate_graph.iter() {
97             let crate_def_map = self.crate_def_map(krate);
98             for (module_id, _) in crate_def_map.modules.iter() {
99                 let file_id = crate_def_map[module_id].origin.file_id();
100                 files.extend(file_id)
101             }
102         }
103         files
104             .into_iter()
105             .filter_map(|file_id| {
106                 let text = self.file_text(file_id);
107                 let annotations = extract_annotations(&text);
108                 if annotations.is_empty() {
109                     return None;
110                 }
111                 Some((file_id, annotations))
112             })
113             .collect()
114     }
115 }
116
117 impl TestDB {
118     pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event> {
119         *self.events.lock().unwrap() = Some(Vec::new());
120         f();
121         self.events.lock().unwrap().take().unwrap()
122     }
123
124     pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
125         let events = self.log(f);
126         events
127             .into_iter()
128             .filter_map(|e| match e.kind {
129                 // This pretty horrible, but `Debug` is the only way to inspect
130                 // QueryDescriptor at the moment.
131                 salsa::EventKind::WillExecute { database_key } => {
132                     Some(format!("{:?}", database_key.debug(self)))
133                 }
134                 _ => None,
135             })
136             .collect()
137     }
138 }