aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/mock.rs97
1 files changed, 16 insertions, 81 deletions
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs
index ab97a09b9..48723fa02 100644
--- a/crates/ra_hir/src/mock.rs
+++ b/crates/ra_hir/src/mock.rs
@@ -2,15 +2,15 @@
2 2
3use std::{panic, sync::Arc}; 3use std::{panic, sync::Arc};
4 4
5use hir_def::{db::DefDatabase2, ModuleId};
5use hir_expand::diagnostics::DiagnosticSink; 6use hir_expand::diagnostics::DiagnosticSink;
6use parking_lot::Mutex; 7use parking_lot::Mutex;
7use ra_cfg::CfgOptions; 8use ra_cfg::CfgOptions;
8use ra_db::{ 9use ra_db::{
9 salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, FilePosition, 10 salsa, CrateGraph, CrateId, Edition, FileId, FileLoader, FileLoaderDelegate, RelativePath,
10 RelativePath, RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId, 11 RelativePathBuf, SourceDatabase, SourceDatabaseExt, SourceRoot, SourceRootId,
11}; 12};
12use rustc_hash::FxHashMap; 13use rustc_hash::FxHashMap;
13use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
14 14
15use crate::{db, debug::HirDebugHelper}; 15use crate::{db, debug::HirDebugHelper};
16 16
@@ -63,12 +63,6 @@ impl HirDebugHelper for MockDatabase {
63} 63}
64 64
65impl MockDatabase { 65impl MockDatabase {
66 pub fn with_files(fixture: &str) -> MockDatabase {
67 let (db, position) = MockDatabase::from_fixture(fixture);
68 assert!(position.is_none());
69 db
70 }
71
72 pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) { 66 pub fn with_single_file(text: &str) -> (MockDatabase, SourceRoot, FileId) {
73 let mut db = MockDatabase::default(); 67 let mut db = MockDatabase::default();
74 let mut source_root = SourceRoot::default(); 68 let mut source_root = SourceRoot::default();
@@ -86,67 +80,21 @@ impl MockDatabase {
86 80
87 pub fn diagnostics(&self) -> String { 81 pub fn diagnostics(&self) -> String {
88 let mut buf = String::new(); 82 let mut buf = String::new();
89 let mut files: Vec<FileId> = self.files.values().copied().collect(); 83 let crate_graph = self.crate_graph();
90 files.sort(); 84 for krate in crate_graph.iter().next() {
91 for file in files { 85 let crate_def_map = self.crate_def_map(krate);
92 let src = crate::Source { 86 for (module_id, _) in crate_def_map.modules.iter() {
93 file_id: file.into(), 87 let module_id = ModuleId { krate, module_id };
94 ast: crate::ModuleSource::new(self, Some(file), None), 88 let module = crate::Module::from(module_id);
95 }; 89 module.diagnostics(
96 let module = crate::Module::from_definition(self, src).unwrap(); 90 self,
97 module.diagnostics( 91 &mut DiagnosticSink::new(|d| {
98 self, 92 buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message());
99 &mut DiagnosticSink::new(|d| { 93 }),
100 buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); 94 )
101 }),
102 )
103 }
104 buf
105 }
106
107 fn from_fixture(fixture: &str) -> (MockDatabase, Option<FilePosition>) {
108 let mut db = MockDatabase::default();
109
110 let pos = db.add_fixture(fixture);
111
112 (db, pos)
113 }
114
115 fn add_fixture(&mut self, fixture: &str) -> Option<FilePosition> {
116 let mut position = None;
117 let mut source_root = SourceRoot::default();
118 let mut source_root_id = WORKSPACE;
119 let mut source_root_prefix = "/".to_string();
120 for entry in parse_fixture(fixture) {
121 if entry.meta.starts_with("root") {
122 self.set_source_root(source_root_id, Arc::new(source_root));
123 source_root = SourceRoot::default();
124
125 source_root_id = SourceRootId(source_root_id.0 + 1);
126 source_root_prefix = entry.meta["root".len()..].trim().to_string();
127 continue;
128 }
129 if entry.text.contains(CURSOR_MARKER) {
130 assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
131 position = Some(self.add_file_with_position(
132 source_root_id,
133 &source_root_prefix,
134 &mut source_root,
135 &entry.meta,
136 &entry.text,
137 ));
138 } else {
139 self.add_file(
140 source_root_id,
141 &source_root_prefix,
142 &mut source_root,
143 &entry.meta,
144 &entry.text,
145 );
146 } 95 }
147 } 96 }
148 self.set_source_root(source_root_id, Arc::new(source_root)); 97 buf
149 position
150 } 98 }
151 99
152 fn add_file( 100 fn add_file(
@@ -183,19 +131,6 @@ impl MockDatabase {
183 } 131 }
184 file_id 132 file_id
185 } 133 }
186
187 fn add_file_with_position(
188 &mut self,
189 source_root_id: SourceRootId,
190 source_root_prefix: &str,
191 source_root: &mut SourceRoot,
192 path: &str,
193 text: &str,
194 ) -> FilePosition {
195 let (offset, text) = extract_offset(text);
196 let file_id = self.add_file(source_root_id, source_root_prefix, source_root, path, &text);
197 FilePosition { file_id, offset }
198 }
199} 134}
200 135
201impl salsa::Database for MockDatabase { 136impl salsa::Database for MockDatabase {