diff options
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 122 |
1 files changed, 119 insertions, 3 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index a1912e90b..350a6d627 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -9,11 +9,18 @@ extern crate relative_path; | |||
9 | extern crate rustc_hash; | 9 | extern crate rustc_hash; |
10 | extern crate salsa; | 10 | extern crate salsa; |
11 | 11 | ||
12 | mod arena; | 12 | macro_rules! ctry { |
13 | ($expr:expr) => { | ||
14 | match $expr { | ||
15 | None => return Ok(None), | ||
16 | Some(it) => it, | ||
17 | } | ||
18 | }; | ||
19 | } | ||
20 | |||
13 | mod db; | 21 | mod db; |
14 | mod imp; | 22 | mod imp; |
15 | mod completion; | 23 | mod completion; |
16 | mod hir; | ||
17 | mod symbol_index; | 24 | mod symbol_index; |
18 | pub mod mock_analysis; | 25 | pub mod mock_analysis; |
19 | 26 | ||
@@ -31,11 +38,11 @@ use crate::{ | |||
31 | 38 | ||
32 | pub use crate::{ | 39 | pub use crate::{ |
33 | completion::CompletionItem, | 40 | completion::CompletionItem, |
34 | hir::FnSignatureInfo, | ||
35 | }; | 41 | }; |
36 | pub use ra_editor::{ | 42 | pub use ra_editor::{ |
37 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, | 43 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, |
38 | }; | 44 | }; |
45 | pub use hir::FnSignatureInfo; | ||
39 | 46 | ||
40 | pub use ra_db::{ | 47 | pub use ra_db::{ |
41 | Canceled, Cancelable, FilePosition, | 48 | Canceled, Cancelable, FilePosition, |
@@ -310,3 +317,112 @@ fn analysis_is_send() { | |||
310 | fn is_send<T: Send>() {} | 317 | fn is_send<T: Send>() {} |
311 | is_send::<Analysis>(); | 318 | is_send::<Analysis>(); |
312 | } | 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 | } | ||