diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index ad05028ccf..d0701a45b1 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -1,20 +1,23 @@ use std::{iter, mem::discriminant}; use crate::{ - doc_links::token_as_doc_comment, navigation_target::ToNav, FilePosition, NavigationTarget, - RangeInfo, TryToNav, + doc_links::token_as_doc_comment, + navigation_target::{self, ToNav}, + FilePosition, NavigationTarget, RangeInfo, TryToNav, UpmappingResult, }; use hir::{ - AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef, Semantics + AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef, + Semantics, }; use ide_db::{ base_db::{AnchoredPath, FileLoader}, defs::{Definition, IdentClass}, helpers::pick_best_token, - FileId, RootDatabase, + RootDatabase, SymbolKind, }; use itertools::Itertools; +use span::FileId; use syntax::{ ast::{self, HasLoopBody}, match_ast, AstNode, AstToken, @@ -299,19 +302,19 @@ fn nav_for_exit_points( ast::ClosureExpr(c) => { let pipe_tok = c.param_list().and_then(|it| it.pipe_token())?.text_range(); let closure_in_file = InFile::new(file_id, c.into()); - Some(NavigationTarget::from_expr(db, closure_in_file, Some(pipe_tok))) + Some(expr_to_nav(db, closure_in_file, Some(pipe_tok))) }, ast::BlockExpr(blk) => { match blk.modifier() { Some(ast::BlockModifier::Async(_)) => { let async_tok = blk.async_token()?.text_range(); let blk_in_file = InFile::new(file_id, blk.into()); - Some(NavigationTarget::from_expr(db, blk_in_file, Some(async_tok))) + Some(expr_to_nav(db, blk_in_file, Some(async_tok))) }, Some(ast::BlockModifier::Try(_)) if token_kind != T![return] => { let try_tok = blk.try_token()?.text_range(); let blk_in_file = InFile::new(file_id, blk.into()); - Some(NavigationTarget::from_expr(db, blk_in_file, Some(try_tok))) + Some(expr_to_nav(db, blk_in_file, Some(try_tok))) }, _ => None, } @@ -390,7 +393,7 @@ fn nav_for_break_points( ast::Expr::BlockExpr(blk) => blk.label().unwrap().syntax().text_range(), _ => return None, }; - let nav = NavigationTarget::from_expr(db, expr_in_file, Some(focus_range)); + let nav = expr_to_nav(db, expr_in_file, Some(focus_range)); Some(nav) }) .flatten() @@ -403,6 +406,20 @@ fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec { def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default() } +fn expr_to_nav( + db: &RootDatabase, + InFile { file_id, value }: InFile, + focus_range: Option, +) -> UpmappingResult { + let kind = SymbolKind::Label; + + let value_range = value.syntax().text_range(); + let navs = navigation_target::orig_range_with_focus_r(db, file_id, value_range, focus_range); + navs.map(|(hir::FileRangeWrapper { file_id, range }, focus_range)| { + NavigationTarget::from_syntax(file_id, "".into(), focus_range, range, kind) + }) +} + #[cfg(test)] mod tests { use ide_db::FileRange; diff --git a/crates/ide/src/highlight_related.rs b/crates/ide/src/highlight_related.rs index d11a6be254..8fcd38b4e3 100644 --- a/crates/ide/src/highlight_related.rs +++ b/crates/ide/src/highlight_related.rs @@ -2,10 +2,14 @@ use std::iter; use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics}; use ide_db::{ - defs::{Definition, IdentClass}, helpers::pick_best_token, search::{FileReference, ReferenceCategory, SearchScope}, syntax_helpers::node_ext::{ + defs::{Definition, IdentClass}, + helpers::pick_best_token, + search::{FileReference, ReferenceCategory, SearchScope}, + syntax_helpers::node_ext::{ eq_label_lt, for_each_tail_expr, full_path_of_name_ref, is_closure_or_blk_with_modif, preorder_expr_with_ctx_checker, - }, FxHashMap, FxHashSet, RootDatabase + }, + FxHashMap, FxHashSet, RootDatabase, }; use span::EditionedFileId; use syntax::{ diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs index 3eb2651314..066141d36f 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -152,22 +152,7 @@ impl NavigationTarget { ) } - pub(crate) fn from_expr( - db: &RootDatabase, - InFile { file_id, value }: InFile, - focus_range: Option, - ) -> UpmappingResult { - let name: SmolStr = "".into(); - let kind = SymbolKind::Label; - - orig_range_with_focus_r(db, file_id, value.syntax().text_range(), focus_range).map( - |(FileRange { file_id, range: full_range }, focus_range)| { - NavigationTarget::from_syntax(file_id, name.clone(), focus_range, full_range, kind) - }, - ) - } - - fn from_syntax( + pub(crate) fn from_syntax( file_id: FileId, name: SmolStr, focus_range: Option, @@ -747,7 +732,7 @@ fn orig_range_with_focus( ) } -fn orig_range_with_focus_r( +pub(crate) fn orig_range_with_focus_r( db: &RootDatabase, hir_file: HirFileId, value: TextRange,