rust/crates/ide/src/parent_module.rs

132 lines
3.5 KiB
Rust
Raw Normal View History

2020-08-13 09:25:38 -05:00
use base_db::{CrateId, FileId, FilePosition};
use hir::Semantics;
2020-08-13 09:39:16 -05:00
use ide_db::RootDatabase;
2020-08-12 11:26:51 -05:00
use syntax::{
2020-01-16 10:33:07 -06:00
algo::find_node_at_offset,
ast::{self, AstNode},
};
2020-05-20 05:59:20 -05:00
use test_utils::mark;
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
// Feature: Parent Module
//
// Navigates to the parent module of the current module.
//
// |===
// | Editor | Action Name
//
// | VS Code | **Rust Analyzer: Locate parent module**
// |===
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> {
let sema = Semantics::new(db);
let source_file = sema.parse(position.file_id);
2020-02-06 08:36:32 -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))
{
2020-05-20 05:59:20 -05:00
mark::hit!(test_resolve_parent_module_on_module_decl);
2020-02-06 08:36:32 -06:00
module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
}
}
let module = match module {
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,
};
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> {
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 {
2020-05-20 05:59:20 -05:00
use test_utils::mark;
2020-10-02 09:13:48 -05:00
use crate::mock_analysis::{analysis_and_position, single_file};
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();
2020-10-02 09:13:48 -05:00
nav.assert_match("foo MODULE FileId(0) 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() {
2020-05-20 05:59:20 -05:00
mark::check!(test_resolve_parent_module_on_module_decl);
2020-02-06 08:36:32 -06:00
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();
2020-10-02 09:13:48 -05:00
nav.assert_match("foo MODULE FileId(0) 0..8");
2020-02-06 08:36:32 -06:00
}
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();
2020-10-02 09:13:48 -05:00
nav.assert_match("baz MODULE FileId(0) 32..44");
2019-01-11 09:17:20 -06:00
}
2019-03-25 15:03:32 -05:00
#[test]
fn test_resolve_crate_root() {
2020-10-02 09:13:48 -05:00
let (analysis, file_id) = single_file(
2020-06-24 05:01:17 -05:00
r#"
2020-10-02 09:13:48 -05:00
//- /main.rs
2020-06-24 05:01:17 -05:00
mod foo;
//- /foo.rs
2020-10-02 09:13:48 -05:00
<|>
2020-06-24 05:01:17 -05:00
"#,
2019-03-25 15:03:32 -05:00
);
2020-10-02 09:13:48 -05:00
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
2019-03-25 15:03:32 -05:00
}
2019-01-11 09:17:20 -06:00
}