rust/crates/ide/src/parent_module.rs

164 lines
3.6 KiB
Rust
Raw Normal View History

use hir::Semantics;
2020-10-24 03:39:57 -05:00
use ide_db::base_db::{CrateId, FileId, FilePosition};
2020-08-13 09:39:16 -05:00
use ide_db::RootDatabase;
use itertools::Itertools;
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},
};
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**
// |===
/// This returns `Vec` because a module may be included from several places.
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))
{
2021-03-08 14:19:44 -06:00
cov_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);
}
}
match module {
Some(module) => sema
.to_def(&module)
.into_iter()
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),
None => sema
.to_module_defs(position.file_id)
.map(|module| NavigationTarget::from_module_to_decl(db, module))
.collect(),
}
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);
sema.to_module_defs(file_id).map(|module| module.krate().into()).unique().collect()
2019-02-08 04:50:18 -06:00
}
2019-01-11 09:17:20 -06:00
#[cfg(test)]
mod tests {
2021-02-09 09:13:11 -06:00
use ide_db::base_db::FileRange;
2021-02-09 09:13:11 -06:00
use crate::fixture;
fn check(ra_fixture: &str) {
let (analysis, position, expected) = fixture::annotations(ra_fixture);
let navs = analysis.parent_module(position).unwrap();
let navs = navs
.iter()
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
.collect::<Vec<_>>();
assert_eq!(expected.into_iter().map(|(fr, _)| fr).collect::<Vec<_>>(), navs);
2021-02-09 09:13:11 -06:00
}
2019-01-11 09:17:20 -06:00
#[test]
fn test_resolve_parent_module() {
2021-02-09 09:13:11 -06:00
check(
r#"
//- /lib.rs
2021-02-09 09:21:09 -06:00
mod foo;
//^^^
2021-02-09 09:13:11 -06:00
//- /foo.rs
$0// empty
"#,
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() {
2021-03-08 14:19:44 -06:00
cov_mark::check!(test_resolve_parent_module_on_module_decl);
2021-02-09 09:13:11 -06:00
check(
r#"
//- /lib.rs
2021-02-09 09:21:09 -06:00
mod foo;
//^^^
2021-02-09 09:13:11 -06:00
//- /foo.rs
mod $0bar;
2020-02-06 08:36:32 -06:00
2021-02-09 09:13:11 -06:00
//- /foo/bar.rs
// empty
"#,
2020-02-06 08:36:32 -06:00
);
}
2019-01-11 09:17:20 -06:00
#[test]
fn test_resolve_parent_module_for_inline() {
2021-02-09 09:13:11 -06:00
check(
2020-06-24 05:01:17 -05:00
r#"
2021-02-09 09:13:11 -06:00
//- /lib.rs
mod foo {
mod bar {
mod baz { $0 }
2021-02-09 09:21:09 -06:00
} //^^^
2021-02-09 09:13:11 -06:00
}
2020-06-24 05:01:17 -05:00
"#,
2019-03-25 15:03:32 -05:00
);
}
2021-02-09 09:29:15 -06:00
#[test]
fn test_resolve_multi_parent_module() {
check(
2021-02-09 09:29:15 -06:00
r#"
//- /main.rs
mod foo;
//^^^
#[path = "foo.rs"]
mod bar;
//^^^
//- /foo.rs
$0
"#,
);
}
#[test]
fn test_resolve_crate_root() {
let (analysis, file_id) = fixture::file(
r#"
2021-02-09 09:29:15 -06:00
//- /foo.rs
$0
//- /main.rs
mod foo;
2021-02-09 09:29:15 -06:00
"#,
);
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 1);
}
#[test]
fn test_resolve_multi_parent_crate() {
let (analysis, file_id) = fixture::file(
r#"
//- /baz.rs
$0
//- /foo.rs crate:foo
mod baz;
//- /bar.rs crate:bar
mod baz;
"#,
);
assert_eq!(analysis.crate_for(file_id).unwrap().len(), 2);
}
2019-01-11 09:17:20 -06:00
}