9597: Use Type::walk for goto_type_definition r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-07-13 23:43:01 +00:00 committed by GitHub
commit 111d0798d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,4 @@
use ide_db::{base_db::Upcast, helpers::pick_best_token, RootDatabase};
use rustc_hash::FxHashSet;
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T};
use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};
@ -55,19 +54,29 @@ pub(crate) fn goto_type_definition(
Some((ty, node))
})?;
let mut res = FxHashSet::default();
let mut workload = vec![ty.strip_references()];
while let Some(ty) = workload.pop() {
if let Some(adt) = ty.as_adt() {
res.insert(adt);
let mut res = Vec::new();
let mut push = |def: hir::ModuleDef| {
if let Some(nav) = def.try_to_nav(db) {
if !res.contains(&nav) {
res.push(nav);
}
}
workload.extend(ty.strip_references().type_arguments());
}
};
Some(RangeInfo::new(
node.text_range(),
res.into_iter().flat_map(|adt| adt.try_to_nav(db)).collect(),
))
let ty = ty.strip_references();
ty.walk(db, |t| {
if let Some(adt) = t.as_adt() {
push(adt.into());
} else if let Some(trait_) = t.as_dyn_trait() {
push(trait_.into());
} else if let Some(traits) = t.as_impl_traits(db) {
traits.into_iter().for_each(|it| push(it.into()));
} else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
push(trait_.into());
}
});
Some(RangeInfo::new(node.text_range(), res))
}
#[cfg(test)]