Tweak goto parent module

This commit is contained in:
Aleksey Kladov 2020-02-06 15:36:32 +01:00
parent ff2d77bde6
commit 832dfae250
2 changed files with 37 additions and 1 deletions

View File

@ -11,4 +11,5 @@
call_info_bad_offset call_info_bad_offset
dont_complete_current_use dont_complete_current_use
dont_complete_primitive_in_use dont_complete_primitive_in_use
test_resolve_parent_module_on_module_decl
); );

View File

@ -6,6 +6,7 @@
algo::find_node_at_offset, algo::find_node_at_offset,
ast::{self, AstNode}, ast::{self, AstNode},
}; };
use test_utils::tested_by;
use crate::NavigationTarget; use crate::NavigationTarget;
@ -14,7 +15,21 @@
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
let mut sb = hir::SourceBinder::new(db); let mut sb = hir::SourceBinder::new(db);
let parse = db.parse(position.file_id); let parse = db.parse(position.file_id);
let module = match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
let mut module = find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset);
// 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 {
Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)), Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)),
None => sb.to_module_def(position.file_id), None => sb.to_module_def(position.file_id),
}; };
@ -41,6 +56,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
mod tests { mod tests {
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::Env; use ra_db::Env;
use test_utils::covers;
use crate::{ use crate::{
mock_analysis::{analysis_and_position, MockAnalysis}, mock_analysis::{analysis_and_position, MockAnalysis},
@ -62,6 +78,25 @@ fn test_resolve_parent_module() {
nav.assert_match("foo MODULE FileId(1) [0; 8)"); nav.assert_match("foo MODULE FileId(1) [0; 8)");
} }
#[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)");
}
#[test] #[test]
fn test_resolve_parent_module_for_inline() { fn test_resolve_parent_module_for_inline() {
let (analysis, pos) = analysis_and_position( let (analysis, pos) = analysis_and_position(