From dda916bc4d51383fcf84f736bd12c7a77c445fb0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 11 Jan 2019 18:17:20 +0300 Subject: fix tests --- crates/ra_ide_api/src/parent_module.rs | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 crates/ra_ide_api/src/parent_module.rs (limited to 'crates/ra_ide_api/src/parent_module.rs') 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 @@ +use ra_db::{Cancelable, FilePosition}; + +use crate::{NavigationTarget, db::RootDatabase}; + +/// This returns `Vec` because a module may be included from several places. We +/// don't handle this case yet though, so the Vec has length at most one. +pub(crate) fn parent_module( + db: &RootDatabase, + position: FilePosition, +) -> Cancelable> { + let module = match hir::source_binder::module_from_position(db, position)? { + None => return Ok(Vec::new()), + Some(it) => it, + }; + let nav = NavigationTarget::from_module(db, module)?; + Ok(vec![nav]) +} + +#[cfg(test)] +mod tests { + use crate::mock_analysis::analysis_and_position; + + #[test] + fn test_resolve_parent_module() { + let (analysis, pos) = analysis_and_position( + " + //- /lib.rs + mod foo; + //- /foo.rs + <|>// empty + ", + ); + let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); + nav.assert_match("foo SOURCE_FILE FileId(2) [0; 10)"); + } + + #[test] + fn test_resolve_parent_module_for_inline() { + let (analysis, pos) = analysis_and_position( + " + //- /lib.rs + mod foo { + mod bar { + mod baz { <|> } + } + } + ", + ); + let nav = analysis.parent_module(pos).unwrap().pop().unwrap(); + nav.assert_match("baz MODULE FileId(1) [32; 44)"); + } +} -- cgit v1.2.3