From 8ef9703740bfe31f9706a843938e628acedb9968 Mon Sep 17 00:00:00 2001 From: Brennan Vincent Date: Sun, 31 May 2020 12:01:49 -0400 Subject: recursively search submodules --- crates/ra_ide_db/src/search.rs | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'crates/ra_ide_db/src') diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 589f44771..335a1ad03 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs @@ -124,29 +124,33 @@ impl Definition { let vis = self.visibility(db); - // FIXME: - // The following logic are wrong that it does not search - // for submodules within other files recursively. - if let Some(Visibility::Module(module)) = vis.and_then(|it| it.into()) { let module: Module = module.into(); let mut res = FxHashMap::default(); - let src = module.definition_source(db); - let file_id = src.file_id.original_file(db); - match src.value { - ModuleSource::Module(m) => { - let range = Some(m.syntax().text_range()); - res.insert(file_id, range); - } - ModuleSource::SourceFile(_) => { - res.insert(file_id, None); - res.extend(module.children(db).map(|m| { - let src = m.definition_source(db); - (src.file_id.original_file(db), None) - })); - } + let mut to_visit = vec![module]; + let mut is_first = true; + while let Some(module) = to_visit.pop() { + let src = module.definition_source(db); + let file_id = src.file_id.original_file(db); + match src.value { + ModuleSource::Module(m) => { + if is_first { + let range = Some(m.syntax().text_range()); + res.insert(file_id, range); + } else { + // We have already added the enclosing file to the search scope, + // so do nothing. + } + } + ModuleSource::SourceFile(_) => { + res.insert(file_id, None); + } + }; + is_first = false; + to_visit.extend(module.children(db)); } + return SearchScope::new(res); } -- cgit v1.2.3