2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-12-05 13:28:31 +00:00
|
|
|
use ra_db::{CrateId, FileId, FilePosition, SourceDatabase};
|
2019-01-11 15:17:20 +00:00
|
|
|
|
2019-07-04 20:05:17 +00:00
|
|
|
use crate::{db::RootDatabase, NavigationTarget};
|
2019-01-11 15:17:20 +00:00
|
|
|
|
|
|
|
/// 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.
|
2019-01-15 18:02:42 +00:00
|
|
|
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
|
2019-09-16 10:48:54 +00:00
|
|
|
let src = hir::ModuleSource::from_position(db, position);
|
|
|
|
let module = match hir::Module::from_definition(
|
|
|
|
db,
|
2019-11-28 09:50:26 +00:00
|
|
|
hir::InFile { file_id: position.file_id.into(), value: src },
|
2019-09-16 10:48:54 +00:00
|
|
|
) {
|
2019-01-15 18:02:42 +00:00
|
|
|
None => return Vec::new(),
|
2019-01-11 15:17:20 +00:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-01-15 15:50:16 +00:00
|
|
|
let nav = NavigationTarget::from_module_to_decl(db, module);
|
2019-01-15 18:02:42 +00:00
|
|
|
vec![nav]
|
2019-01-11 15:17:20 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 10:50:18 +00:00
|
|
|
/// Returns `Vec` for the same reason as `parent_module`
|
|
|
|
pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
2019-12-05 13:28:31 +00:00
|
|
|
let source_file = db.parse(file_id).tree();
|
|
|
|
let src = hir::ModuleSource::SourceFile(source_file);
|
2019-09-16 10:48:54 +00:00
|
|
|
let module =
|
2019-11-28 09:50:26 +00:00
|
|
|
match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src })
|
2019-11-20 06:40:36 +00:00
|
|
|
{
|
2019-09-16 10:48:54 +00:00
|
|
|
Some(it) => it,
|
|
|
|
None => return Vec::new(),
|
|
|
|
};
|
2019-10-30 10:10:38 +00:00
|
|
|
let krate = module.krate();
|
2019-12-08 11:44:14 +00:00
|
|
|
vec![krate.into()]
|
2019-02-08 10:50:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:17:20 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-22 10:55:03 +00:00
|
|
|
use ra_cfg::CfgOptions;
|
|
|
|
use ra_db::Env;
|
|
|
|
|
2019-03-25 20:03:32 +00:00
|
|
|
use crate::{
|
|
|
|
mock_analysis::{analysis_and_position, MockAnalysis},
|
2019-07-04 20:05:17 +00:00
|
|
|
AnalysisChange, CrateGraph,
|
2019-03-25 20:03:32 +00:00
|
|
|
Edition::Edition2018,
|
2019-07-04 20:05:17 +00:00
|
|
|
};
|
2019-01-11 15:17:20 +00:00
|
|
|
|
|
|
|
#[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 18:56:20 +00:00
|
|
|
nav.assert_match("foo MODULE FileId(1) [0; 8)");
|
2019-01-11 15:17:20 +00: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)");
|
|
|
|
}
|
2019-03-25 20:03:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_resolve_crate_root() {
|
|
|
|
let mock = MockAnalysis::with_files(
|
|
|
|
"
|
|
|
|
//- /bar.rs
|
|
|
|
mod foo;
|
|
|
|
//- /foo.rs
|
|
|
|
// empty <|>
|
|
|
|
",
|
|
|
|
);
|
|
|
|
let root_file = mock.id_of("/bar.rs");
|
|
|
|
let mod_file = mock.id_of("/foo.rs");
|
|
|
|
let mut host = mock.analysis_host();
|
|
|
|
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
|
|
|
|
|
|
|
|
let mut crate_graph = CrateGraph::default();
|
2019-11-22 10:55:03 +00:00
|
|
|
let crate_id = crate_graph.add_crate_root(
|
|
|
|
root_file,
|
|
|
|
Edition2018,
|
|
|
|
CfgOptions::default(),
|
|
|
|
Env::default(),
|
|
|
|
);
|
2019-03-25 20:03:32 +00:00
|
|
|
let mut change = AnalysisChange::new();
|
|
|
|
change.set_crate_graph(crate_graph);
|
|
|
|
host.apply_change(change);
|
|
|
|
|
|
|
|
assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]);
|
|
|
|
}
|
2019-01-11 15:17:20 +00:00
|
|
|
}
|