diff options
author | Aleksey Kladov <[email protected]> | 2019-01-11 15:17:20 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-11 15:32:22 +0000 |
commit | dda916bc4d51383fcf84f736bd12c7a77c445fb0 (patch) | |
tree | 1d1a8246105eba1a16fd066365ea095ff0377740 /crates/ra_ide_api/src/parent_module.rs | |
parent | 8a5f74a24f726a839f3a0e154cfadec23040e14c (diff) |
fix tests
Diffstat (limited to 'crates/ra_ide_api/src/parent_module.rs')
-rw-r--r-- | crates/ra_ide_api/src/parent_module.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs new file mode 100644 index 000000000..d345839a3 --- /dev/null +++ b/crates/ra_ide_api/src/parent_module.rs | |||
@@ -0,0 +1,52 @@ | |||
1 | use ra_db::{Cancelable, FilePosition}; | ||
2 | |||
3 | use crate::{NavigationTarget, db::RootDatabase}; | ||
4 | |||
5 | /// This returns `Vec` because a module may be included from several places. We | ||
6 | /// don't handle this case yet though, so the Vec has length at most one. | ||
7 | pub(crate) fn parent_module( | ||
8 | db: &RootDatabase, | ||
9 | position: FilePosition, | ||
10 | ) -> Cancelable<Vec<NavigationTarget>> { | ||
11 | let module = match hir::source_binder::module_from_position(db, position)? { | ||
12 | None => return Ok(Vec::new()), | ||
13 | Some(it) => it, | ||
14 | }; | ||
15 | let nav = NavigationTarget::from_module(db, module)?; | ||
16 | Ok(vec![nav]) | ||
17 | } | ||
18 | |||
19 | #[cfg(test)] | ||
20 | mod tests { | ||
21 | use crate::mock_analysis::analysis_and_position; | ||
22 | |||
23 | #[test] | ||
24 | fn test_resolve_parent_module() { | ||
25 | let (analysis, pos) = analysis_and_position( | ||
26 | " | ||
27 | //- /lib.rs | ||
28 | mod foo; | ||
29 | //- /foo.rs | ||
30 | <|>// empty | ||
31 | ", | ||
32 | ); | ||
33 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); | ||
34 | nav.assert_match("foo SOURCE_FILE FileId(2) [0; 10)"); | ||
35 | } | ||
36 | |||
37 | #[test] | ||
38 | fn test_resolve_parent_module_for_inline() { | ||
39 | let (analysis, pos) = analysis_and_position( | ||
40 | " | ||
41 | //- /lib.rs | ||
42 | mod foo { | ||
43 | mod bar { | ||
44 | mod baz { <|> } | ||
45 | } | ||
46 | } | ||
47 | ", | ||
48 | ); | ||
49 | let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); | ||
50 | nav.assert_match("baz MODULE FileId(1) [32; 44)"); | ||
51 | } | ||
52 | } | ||