2019-09-30 03:58:53 -05:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
use hir::Semantics;
|
|
|
|
use ra_db::{CrateId, FileId, FilePosition};
|
2020-02-06 05:52:32 -06:00
|
|
|
use ra_ide_db::RootDatabase;
|
2020-01-16 10:33:07 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
algo::find_node_at_offset,
|
|
|
|
ast::{self, AstNode},
|
|
|
|
};
|
2020-02-06 08:36:32 -06:00
|
|
|
use test_utils::tested_by;
|
2019-01-11 09:17:20 -06:00
|
|
|
|
2020-02-06 05:52:32 -06:00
|
|
|
use crate::NavigationTarget;
|
2019-01-11 09:17:20 -06: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 12:02:42 -06:00
|
|
|
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let source_file = sema.parse(position.file_id);
|
2020-02-06 08:36:32 -06:00
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
let mut module = find_node_at_offset::<ast::Module>(source_file.syntax(), position.offset);
|
2020-02-06 08:36:32 -06:00
|
|
|
|
|
|
|
// If cursor is literally on `mod foo`, go to the grandpa.
|
|
|
|
if let Some(m) = &module {
|
|
|
|
if !m
|
|
|
|
.item_list()
|
|
|
|
.map_or(false, |it| it.syntax().text_range().contains_inclusive(position.offset))
|
|
|
|
{
|
|
|
|
tested_by!(test_resolve_parent_module_on_module_decl);
|
|
|
|
module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let module = match module {
|
2020-02-18 11:35:10 -06:00
|
|
|
Some(module) => sema.to_def(&module),
|
|
|
|
None => sema.to_module_def(position.file_id),
|
2020-01-16 10:33:07 -06:00
|
|
|
};
|
|
|
|
let module = match module {
|
2019-01-15 12:02:42 -06:00
|
|
|
None => return Vec::new(),
|
2019-01-11 09:17:20 -06:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
2019-01-15 09:50:16 -06:00
|
|
|
let nav = NavigationTarget::from_module_to_decl(db, module);
|
2019-01-15 12:02:42 -06:00
|
|
|
vec![nav]
|
2019-01-11 09:17:20 -06:00
|
|
|
}
|
|
|
|
|
2019-02-08 04:50:18 -06:00
|
|
|
/// Returns `Vec` for the same reason as `parent_module`
|
|
|
|
pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let module = match sema.to_module_def(file_id) {
|
2020-01-16 10:33:07 -06:00
|
|
|
Some(it) => it,
|
|
|
|
None => return Vec::new(),
|
|
|
|
};
|
2019-10-30 05:10:38 -05:00
|
|
|
let krate = module.krate();
|
2019-12-08 05:44:14 -06:00
|
|
|
vec![krate.into()]
|
2019-02-08 04:50:18 -06:00
|
|
|
}
|
|
|
|
|
2019-01-11 09:17:20 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-22 04:55:03 -06:00
|
|
|
use ra_cfg::CfgOptions;
|
|
|
|
use ra_db::Env;
|
2020-02-06 08:36:32 -06:00
|
|
|
use test_utils::covers;
|
2019-11-22 04:55:03 -06:00
|
|
|
|
2019-03-25 15:03:32 -05:00
|
|
|
use crate::{
|
|
|
|
mock_analysis::{analysis_and_position, MockAnalysis},
|
2019-07-04 15:05:17 -05:00
|
|
|
AnalysisChange, CrateGraph,
|
2019-03-25 15:03:32 -05:00
|
|
|
Edition::Edition2018,
|
2019-07-04 15:05:17 -05:00
|
|
|
};
|
2019-01-11 09:17:20 -06: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 12:56:20 -06:00
|
|
|
nav.assert_match("foo MODULE FileId(1) [0; 8)");
|
2019-01-11 09:17:20 -06:00
|
|
|
}
|
|
|
|
|
2020-02-06 08:36:32 -06:00
|
|
|
#[test]
|
|
|
|
fn test_resolve_parent_module_on_module_decl() {
|
|
|
|
covers!(test_resolve_parent_module_on_module_decl);
|
|
|
|
let (analysis, pos) = analysis_and_position(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
mod <|>bar;
|
|
|
|
|
|
|
|
//- /foo/bar.rs
|
|
|
|
// empty
|
|
|
|
",
|
|
|
|
);
|
|
|
|
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
|
|
|
|
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)");
|
|
|
|
}
|
2019-03-25 15:03:32 -05: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 04:55:03 -06:00
|
|
|
let crate_id = crate_graph.add_crate_root(
|
|
|
|
root_file,
|
|
|
|
Edition2018,
|
2020-03-08 08:26:57 -05:00
|
|
|
None,
|
2019-11-22 04:55:03 -06:00
|
|
|
CfgOptions::default(),
|
|
|
|
Env::default(),
|
2020-03-10 22:04:02 -05:00
|
|
|
Default::default(),
|
2020-03-18 07:56:46 -05:00
|
|
|
Default::default(),
|
2019-11-22 04:55:03 -06:00
|
|
|
);
|
2019-03-25 15:03:32 -05: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 09:17:20 -06:00
|
|
|
}
|