Skip to content

Commit e4bdc14

Browse files
Merge #4674
4674: Recursively search submodules to find modules in which a definition is visible. r=matklad a=umanwizard Co-authored-by: Brennan Vincent <[email protected]>
2 parents 9feb15e + d39cbee commit e4bdc14

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

crates/ra_ide/src/references.rs

+27
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,33 @@ mod tests {
615615
);
616616
}
617617

618+
#[test]
619+
fn test_find_all_refs_nested_module() {
620+
let code = r#"
621+
//- /lib.rs
622+
mod foo {
623+
mod bar;
624+
}
625+
626+
fn f<|>() {}
627+
628+
//- /foo/bar.rs
629+
use crate::f;
630+
631+
fn g() {
632+
f();
633+
}
634+
"#;
635+
636+
let (analysis, pos) = analysis_and_position(code);
637+
let refs = analysis.find_all_refs(pos, None).unwrap().unwrap();
638+
check_result(
639+
refs,
640+
"f FN_DEF FileId(1) 25..34 28..29 Other",
641+
&["FileId(2) 11..12 Other", "FileId(2) 27..28 StructLiteral"],
642+
);
643+
}
644+
618645
fn get_all_refs(text: &str) -> ReferenceSearchResult {
619646
let (analysis, position) = single_file_with_position(text);
620647
analysis.find_all_refs(position, None).unwrap().unwrap()

crates/ra_ide_db/src/search.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,33 @@ impl Definition {
124124

125125
let vis = self.visibility(db);
126126

127-
// FIXME:
128-
// The following logic are wrong that it does not search
129-
// for submodules within other files recursively.
130-
131127
if let Some(Visibility::Module(module)) = vis.and_then(|it| it.into()) {
132128
let module: Module = module.into();
133129
let mut res = FxHashMap::default();
134-
let src = module.definition_source(db);
135-
let file_id = src.file_id.original_file(db);
136130

137-
match src.value {
138-
ModuleSource::Module(m) => {
139-
let range = Some(m.syntax().text_range());
140-
res.insert(file_id, range);
141-
}
142-
ModuleSource::SourceFile(_) => {
143-
res.insert(file_id, None);
144-
res.extend(module.children(db).map(|m| {
145-
let src = m.definition_source(db);
146-
(src.file_id.original_file(db), None)
147-
}));
148-
}
131+
let mut to_visit = vec![module];
132+
let mut is_first = true;
133+
while let Some(module) = to_visit.pop() {
134+
let src = module.definition_source(db);
135+
let file_id = src.file_id.original_file(db);
136+
match src.value {
137+
ModuleSource::Module(m) => {
138+
if is_first {
139+
let range = Some(m.syntax().text_range());
140+
res.insert(file_id, range);
141+
} else {
142+
// We have already added the enclosing file to the search scope,
143+
// so do nothing.
144+
}
145+
}
146+
ModuleSource::SourceFile(_) => {
147+
res.insert(file_id, None);
148+
}
149+
};
150+
is_first = false;
151+
to_visit.extend(module.children(db));
149152
}
153+
150154
return SearchScope::new(res);
151155
}
152156

0 commit comments

Comments
 (0)