2019-01-11 09:17:20 -06:00
|
|
|
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<Vec<NavigationTarget>> {
|
2019-01-15 09:13:11 -06:00
|
|
|
let module = match hir::source_binder::module_from_position(db, position) {
|
2019-01-11 09:17:20 -06:00
|
|
|
None => return Ok(Vec::new()),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-01-15 09:50:16 -06:00
|
|
|
let nav = NavigationTarget::from_module_to_decl(db, module);
|
2019-01-11 09:17:20 -06:00
|
|
|
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();
|
2019-01-13 12:56:20 -06:00
|
|
|
nav.assert_match("foo MODULE FileId(1) [0; 8)");
|
2019-01-11 09:17:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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)");
|
|
|
|
}
|
|
|
|
}
|