diff options
Diffstat (limited to 'crates/ra_analysis')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 109 | ||||
-rw-r--r-- | crates/ra_analysis/src/mock_analysis.rs | 40 |
2 files changed, 7 insertions, 142 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 350a6d627..12df580ba 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -317,112 +317,3 @@ fn analysis_is_send() { | |||
317 | fn is_send<T: Send>() {} | 317 | fn is_send<T: Send>() {} |
318 | is_send::<Analysis>(); | 318 | is_send::<Analysis>(); |
319 | } | 319 | } |
320 | |||
321 | //TODO: move to hir | ||
322 | #[cfg(test)] | ||
323 | mod hir_namres_tests { | ||
324 | use std::sync::Arc; | ||
325 | use ra_db::FilesDatabase; | ||
326 | use ra_syntax::SmolStr; | ||
327 | use hir::{self, db::HirDatabase}; | ||
328 | |||
329 | use crate::{ | ||
330 | AnalysisChange, | ||
331 | mock_analysis::{MockAnalysis, analysis_and_position}, | ||
332 | }; | ||
333 | |||
334 | fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { | ||
335 | let (analysis, pos) = analysis_and_position(fixture); | ||
336 | let db = analysis.imp.db; | ||
337 | let source_root = db.file_source_root(pos.file_id); | ||
338 | let descr = hir::Module::guess_from_position(&*db, pos) | ||
339 | .unwrap() | ||
340 | .unwrap(); | ||
341 | let module_id = descr.module_id; | ||
342 | (db.item_map(source_root).unwrap(), module_id) | ||
343 | } | ||
344 | |||
345 | #[test] | ||
346 | fn test_item_map() { | ||
347 | let (item_map, module_id) = item_map( | ||
348 | " | ||
349 | //- /lib.rs | ||
350 | mod foo; | ||
351 | |||
352 | use crate::foo::bar::Baz; | ||
353 | <|> | ||
354 | |||
355 | //- /foo/mod.rs | ||
356 | pub mod bar; | ||
357 | |||
358 | //- /foo/bar.rs | ||
359 | pub struct Baz; | ||
360 | ", | ||
361 | ); | ||
362 | let name = SmolStr::from("Baz"); | ||
363 | let resolution = &item_map.per_module[&module_id].items[&name]; | ||
364 | assert!(resolution.def_id.is_some()); | ||
365 | } | ||
366 | |||
367 | #[test] | ||
368 | fn typing_inside_a_function_should_not_invalidate_item_map() { | ||
369 | let mock_analysis = MockAnalysis::with_files( | ||
370 | " | ||
371 | //- /lib.rs | ||
372 | mod foo; | ||
373 | |||
374 | use crate::foo::bar::Baz; | ||
375 | |||
376 | fn foo() -> i32 { | ||
377 | 1 + 1 | ||
378 | } | ||
379 | //- /foo/mod.rs | ||
380 | pub mod bar; | ||
381 | |||
382 | //- /foo/bar.rs | ||
383 | pub struct Baz; | ||
384 | ", | ||
385 | ); | ||
386 | |||
387 | let file_id = mock_analysis.id_of("/lib.rs"); | ||
388 | let mut host = mock_analysis.analysis_host(); | ||
389 | |||
390 | let source_root = host.analysis().imp.db.file_source_root(file_id); | ||
391 | |||
392 | { | ||
393 | let db = host.analysis().imp.db; | ||
394 | let events = db.log_executed(|| { | ||
395 | db.item_map(source_root).unwrap(); | ||
396 | }); | ||
397 | assert!(format!("{:?}", events).contains("item_map")) | ||
398 | } | ||
399 | |||
400 | let mut change = AnalysisChange::new(); | ||
401 | |||
402 | change.change_file( | ||
403 | file_id, | ||
404 | " | ||
405 | mod foo; | ||
406 | |||
407 | use crate::foo::bar::Baz; | ||
408 | |||
409 | fn foo() -> i32 { 92 } | ||
410 | " | ||
411 | .to_string(), | ||
412 | ); | ||
413 | |||
414 | host.apply_change(change); | ||
415 | |||
416 | { | ||
417 | let db = host.analysis().imp.db; | ||
418 | let events = db.log_executed(|| { | ||
419 | db.item_map(source_root).unwrap(); | ||
420 | }); | ||
421 | assert!( | ||
422 | !format!("{:?}", events).contains("_item_map"), | ||
423 | "{:#?}", | ||
424 | events | ||
425 | ) | ||
426 | } | ||
427 | } | ||
428 | } | ||
diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index 8e8f969f4..0d9a7a147 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs | |||
@@ -1,9 +1,10 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use relative_path::{RelativePath, RelativePathBuf}; | 3 | use relative_path::{RelativePathBuf}; |
4 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; | 4 | use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; |
5 | use ra_db::mock::FileMap; | ||
5 | 6 | ||
6 | use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver, FilePosition}; | 7 | use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FilePosition}; |
7 | 8 | ||
8 | /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis | 9 | /// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis |
9 | /// from a set of in-memory files. | 10 | /// from a set of in-memory files. |
@@ -76,16 +77,15 @@ impl MockAnalysis { | |||
76 | } | 77 | } |
77 | pub fn analysis_host(self) -> AnalysisHost { | 78 | pub fn analysis_host(self) -> AnalysisHost { |
78 | let mut host = AnalysisHost::default(); | 79 | let mut host = AnalysisHost::default(); |
79 | let mut file_map = Vec::new(); | 80 | let mut file_map = FileMap::default(); |
80 | let mut change = AnalysisChange::new(); | 81 | let mut change = AnalysisChange::new(); |
81 | for (id, (path, contents)) in self.files.into_iter().enumerate() { | 82 | for (path, contents) in self.files.into_iter() { |
82 | let file_id = FileId((id + 1) as u32); | ||
83 | assert!(path.starts_with('/')); | 83 | assert!(path.starts_with('/')); |
84 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); | 84 | let path = RelativePathBuf::from_path(&path[1..]).unwrap(); |
85 | let file_id = file_map.add(path); | ||
85 | change.add_file(file_id, contents); | 86 | change.add_file(file_id, contents); |
86 | file_map.push((file_id, path)); | ||
87 | } | 87 | } |
88 | change.set_file_resolver(Arc::new(FileMap(file_map))); | 88 | change.set_file_resolver(Arc::new(file_map)); |
89 | host.apply_change(change); | 89 | host.apply_change(change); |
90 | host | 90 | host |
91 | } | 91 | } |
@@ -113,29 +113,3 @@ pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) { | |||
113 | let pos = mock.add_file_with_position("/main.rs", code); | 113 | let pos = mock.add_file_with_position("/main.rs", code); |
114 | (mock.analysis(), pos) | 114 | (mock.analysis(), pos) |
115 | } | 115 | } |
116 | |||
117 | #[derive(Debug)] | ||
118 | struct FileMap(Vec<(FileId, RelativePathBuf)>); | ||
119 | |||
120 | impl FileMap { | ||
121 | fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a { | ||
122 | self.0 | ||
123 | .iter() | ||
124 | .map(|(id, path)| (*id, path.as_relative_path())) | ||
125 | } | ||
126 | |||
127 | fn path(&self, id: FileId) -> &RelativePath { | ||
128 | self.iter().find(|&(it, _)| it == id).unwrap().1 | ||
129 | } | ||
130 | } | ||
131 | |||
132 | impl FileResolver for FileMap { | ||
133 | fn file_stem(&self, id: FileId) -> String { | ||
134 | self.path(id).file_stem().unwrap().to_string() | ||
135 | } | ||
136 | fn resolve(&self, id: FileId, rel: &RelativePath) -> Option<FileId> { | ||
137 | let path = self.path(id).join(rel).normalize(); | ||
138 | let id = self.iter().find(|&(_, p)| path == p)?.0; | ||
139 | Some(id) | ||
140 | } | ||
141 | } | ||