From 2490807ca53197310600a4d5a353a645c4a52667 Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Mon, 25 Oct 2021 23:43:58 +0200 Subject: [PATCH 1/3] fix: make `goto_type_definition` multi-token mapping aware --- crates/ide/src/goto_type_definition.rs | 94 +++++++++++++++----------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index 3acf3b0367..ce22ef049c 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs @@ -1,5 +1,5 @@ use ide_db::{base_db::Upcast, helpers::pick_best_token, RootDatabase}; -use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T}; +use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TextRange, T}; use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo}; @@ -27,32 +27,6 @@ pub(crate) fn goto_type_definition( kind if kind.is_trivia() => 0, _ => 1, })?; - let token: SyntaxToken = sema.descend_into_macros_single(token); - - let (ty, node) = sema.token_ancestors_with_macros(token).find_map(|node| { - let ty = match_ast! { - match node { - ast::Expr(it) => sema.type_of_expr(&it)?.original, - ast::Pat(it) => sema.type_of_pat(&it)?.original, - ast::SelfParam(it) => sema.type_of_self(&it)?, - ast::Type(it) => sema.resolve_type(&it)?, - ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?, - // can't match on RecordExprField directly as `ast::Expr` will match an iteration too early otherwise - ast::NameRef(it) => { - if let Some(record_field) = ast::RecordExprField::for_name_ref(&it) { - let (_, _, ty) = sema.resolve_record_field(&record_field)?; - ty - } else { - let record_field = ast::RecordPatField::for_field_name_ref(&it)?; - sema.resolve_record_pat_field(&record_field)?.ty(db) - } - }, - _ => return None, - } - }; - - Some((ty, node)) - })?; let mut res = Vec::new(); let mut push = |def: hir::ModuleDef| { @@ -63,20 +37,60 @@ pub(crate) fn goto_type_definition( } }; - 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()); - } - }); + // TODO this became pretty baroque after refactoring for `descend_into_macros(_many)` + let range = sema + .descend_into_macros(token) + .iter() + .filter_map(|token| { + let ty_range = sema.token_ancestors_with_macros(token.clone()).find_map(|node| { + let ty = match_ast! { + match node { + ast::Expr(it) => sema.type_of_expr(&it)?.original, + ast::Pat(it) => sema.type_of_pat(&it)?.original, + ast::SelfParam(it) => sema.type_of_self(&it)?, + ast::Type(it) => sema.resolve_type(&it)?, + ast::RecordField(it) => sema.to_def(&it).map(|d| d.ty(db.upcast()))?, + // can't match on RecordExprField directly as `ast::Expr` will match an iteration too early otherwise + ast::NameRef(it) => { + if let Some(record_field) = ast::RecordExprField::for_name_ref(&it) { + let (_, _, ty) = sema.resolve_record_field(&record_field)?; + ty + } else { + let record_field = ast::RecordPatField::for_field_name_ref(&it)?; + sema.resolve_record_pat_field(&record_field)?.ty(db) + } + }, + _ => return None, + } + }; - Some(RangeInfo::new(node.text_range(), res)) + let range = node.text_range(); + Some((ty, range.start(), range.end())) + }); + ty_range + }) + .inspect(|(ty, _range_start, _range_end)| { + // collect from each `ty` into the `res` result vec + 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()); + } + }); + }) // reduce all ranges into a single umbrella span (TODO fishy?) + .map(|(_, range_start, range_end)| (range_start, range_end)) + .reduce(|(start_acc, end_acc), (start_cur, end_cur)| { + (start_acc.min(start_cur), end_acc.max(end_cur)) + }) + .map(|(range_start, range_end)| TextRange::new(range_start, range_end))?; // TODO easy to miss `?` bail + + Some(RangeInfo::new(range, res)) } #[cfg(test)] From 686f8fbea3a3f07744da7df1a26e05353a431a40 Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Tue, 26 Oct 2021 12:21:18 +0200 Subject: [PATCH 2/3] simplify --- crates/ide/src/goto_type_definition.rs | 29 +++++++++++--------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index ce22ef049c..4a999142ec 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs @@ -36,13 +36,11 @@ pub(crate) fn goto_type_definition( } } }; - - // TODO this became pretty baroque after refactoring for `descend_into_macros(_many)` - let range = sema - .descend_into_macros(token) + let range = token.text_range(); + sema.descend_into_macros(token) .iter() .filter_map(|token| { - let ty_range = sema.token_ancestors_with_macros(token.clone()).find_map(|node| { + let ty = sema.token_ancestors_with_macros(token.clone()).find_map(|node| { let ty = match_ast! { match node { ast::Expr(it) => sema.type_of_expr(&it)?.original, @@ -64,12 +62,11 @@ pub(crate) fn goto_type_definition( } }; - let range = node.text_range(); - Some((ty, range.start(), range.end())) + Some(ty) }); - ty_range + ty }) - .inspect(|(ty, _range_start, _range_end)| { + .for_each(|ty| { // collect from each `ty` into the `res` result vec let ty = ty.strip_references(); ty.walk(db, |t| { @@ -83,14 +80,12 @@ pub(crate) fn goto_type_definition( push(trait_.into()); } }); - }) // reduce all ranges into a single umbrella span (TODO fishy?) - .map(|(_, range_start, range_end)| (range_start, range_end)) - .reduce(|(start_acc, end_acc), (start_cur, end_cur)| { - (start_acc.min(start_cur), end_acc.max(end_cur)) - }) - .map(|(range_start, range_end)| TextRange::new(range_start, range_end))?; // TODO easy to miss `?` bail - - Some(RangeInfo::new(range, res)) + }); + if res.is_empty() { + None + } else { + Some(RangeInfo::new(range, res)) + } } #[cfg(test)] From c69879423eef75eaafa744ee2b03b5e85d942438 Mon Sep 17 00:00:00 2001 From: Anatol Ulrich Date: Tue, 26 Oct 2021 12:34:40 +0200 Subject: [PATCH 3/3] fix imports --- crates/ide/src/goto_type_definition.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide/src/goto_type_definition.rs b/crates/ide/src/goto_type_definition.rs index 4a999142ec..2976dec379 100644 --- a/crates/ide/src/goto_type_definition.rs +++ b/crates/ide/src/goto_type_definition.rs @@ -1,5 +1,5 @@ use ide_db::{base_db::Upcast, helpers::pick_best_token, RootDatabase}; -use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TextRange, T}; +use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, T}; use crate::{display::TryToNav, FilePosition, NavigationTarget, RangeInfo};