diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-11 15:35:41 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-11 15:35:41 +0000 |
commit | aad1bf877e4ba5ce9e28e8bde14f790ef8d1551b (patch) | |
tree | 9d3ea7f166adcd09fb77cfdb7fc1bad03efc95cd /crates/ra_ide_api/src/parent_module.rs | |
parent | 0b83bde6e2f3782ea6acd907fa0a634912cebb3d (diff) | |
parent | f23a13bfa7bae7e34070bfd14d22b70a82315022 (diff) |
Merge #496
496: Include two element ranges into the nav. r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
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 | } | ||