Skip to content

Commit aa571e3

Browse files
bors[bot]matklad
andauthored
Merge #3030
3030: Tweak goto parent module r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents ff2d77b + 832dfae commit aa571e3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

crates/ra_ide/src/marks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ test_utils::marks!(
1111
call_info_bad_offset
1212
dont_complete_current_use
1313
dont_complete_primitive_in_use
14+
test_resolve_parent_module_on_module_decl
1415
);

crates/ra_ide/src/parent_module.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use ra_syntax::{
66
algo::find_node_at_offset,
77
ast::{self, AstNode},
88
};
9+
use test_utils::tested_by;
910

1011
use crate::NavigationTarget;
1112

@@ -14,7 +15,21 @@ use crate::NavigationTarget;
1415
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
1516
let mut sb = hir::SourceBinder::new(db);
1617
let parse = db.parse(position.file_id);
17-
let module = match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
18+
19+
let mut module = find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset);
20+
21+
// If cursor is literally on `mod foo`, go to the grandpa.
22+
if let Some(m) = &module {
23+
if !m
24+
.item_list()
25+
.map_or(false, |it| it.syntax().text_range().contains_inclusive(position.offset))
26+
{
27+
tested_by!(test_resolve_parent_module_on_module_decl);
28+
module = m.syntax().ancestors().skip(1).find_map(ast::Module::cast);
29+
}
30+
}
31+
32+
let module = match module {
1833
Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)),
1934
None => sb.to_module_def(position.file_id),
2035
};
@@ -41,6 +56,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
4156
mod tests {
4257
use ra_cfg::CfgOptions;
4358
use ra_db::Env;
59+
use test_utils::covers;
4460

4561
use crate::{
4662
mock_analysis::{analysis_and_position, MockAnalysis},
@@ -62,6 +78,25 @@ mod tests {
6278
nav.assert_match("foo MODULE FileId(1) [0; 8)");
6379
}
6480

81+
#[test]
82+
fn test_resolve_parent_module_on_module_decl() {
83+
covers!(test_resolve_parent_module_on_module_decl);
84+
let (analysis, pos) = analysis_and_position(
85+
"
86+
//- /lib.rs
87+
mod foo;
88+
89+
//- /foo.rs
90+
mod <|>bar;
91+
92+
//- /foo/bar.rs
93+
// empty
94+
",
95+
);
96+
let nav = analysis.parent_module(pos).unwrap().pop().unwrap();
97+
nav.assert_match("foo MODULE FileId(1) [0; 8)");
98+
}
99+
65100
#[test]
66101
fn test_resolve_parent_module_for_inline() {
67102
let (analysis, pos) = analysis_and_position(

0 commit comments

Comments
 (0)