10637: fix: make `goto_type_definition` multi-token mapping aware r=Veykril a=spookyvision



Co-authored-by: Anatol Ulrich <anatol.ulrich@ferrous-systems.com>
This commit is contained in:
bors[bot] 2021-10-26 10:46:33 +00:00 committed by GitHub
commit ee1d6cffbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,9 +27,20 @@ pub(crate) fn goto_type_definition(
kind if kind.is_trivia() => 0, kind if kind.is_trivia() => 0,
_ => 1, _ => 1,
})?; })?;
let token: SyntaxToken = sema.descend_into_macros_single(token);
let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| { 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);
}
}
};
let range = token.text_range();
sema.descend_into_macros(token)
.iter()
.filter_map(|token| {
let ty = sema.token_ancestors_with_macros(token.clone()).find_map(|node| {
let ty = match_ast! { let ty = match_ast! {
match node { match node {
ast::Expr(it) => sema.type_of_expr(&it)?.original, ast::Expr(it) => sema.type_of_expr(&it)?.original,
@ -51,18 +62,12 @@ pub(crate) fn goto_type_definition(
} }
}; };
Some((ty, node)) Some(ty)
})?; });
ty
let mut res = Vec::new(); })
let mut push = |def: hir::ModuleDef| { .for_each(|ty| {
if let Some(nav) = def.try_to_nav(db) { // collect from each `ty` into the `res` result vec
if !res.contains(&nav) {
res.push(nav);
}
}
};
let ty = ty.strip_references(); let ty = ty.strip_references();
ty.walk(db, |t| { ty.walk(db, |t| {
if let Some(adt) = t.as_adt() { if let Some(adt) = t.as_adt() {
@ -75,8 +80,12 @@ pub(crate) fn goto_type_definition(
push(trait_.into()); push(trait_.into());
} }
}); });
});
Some(RangeInfo::new(node.text_range(), res)) if res.is_empty() {
None
} else {
Some(RangeInfo::new(range, res))
}
} }
#[cfg(test)] #[cfg(test)]