Skip to content

Commit e49887e

Browse files
committed
internal: move NavigationTarget::from_expr to goto_definition
1 parent b70fab2 commit e49887e

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
use std::{iter, mem::discriminant};
22

33
use crate::{
4-
doc_links::token_as_doc_comment, navigation_target::ToNav, FilePosition, NavigationTarget,
5-
RangeInfo, TryToNav,
4+
doc_links::token_as_doc_comment,
5+
navigation_target::{self, ToNav},
6+
FilePosition, NavigationTarget, RangeInfo, TryToNav, UpmappingResult,
67
};
78
use hir::{
8-
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef, Semantics
9+
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef,
10+
Semantics,
911
};
1012
use ide_db::{
1113
base_db::{AnchoredPath, FileLoader},
1214
defs::{Definition, IdentClass},
1315
helpers::pick_best_token,
14-
FileId, RootDatabase,
16+
RootDatabase, SymbolKind,
1517
};
1618
use itertools::Itertools;
1719

20+
use span::FileId;
1821
use syntax::{
1922
ast::{self, HasLoopBody},
2023
match_ast, AstNode, AstToken,
@@ -299,19 +302,19 @@ fn nav_for_exit_points(
299302
ast::ClosureExpr(c) => {
300303
let pipe_tok = c.param_list().and_then(|it| it.pipe_token())?.text_range();
301304
let closure_in_file = InFile::new(file_id, c.into());
302-
Some(NavigationTarget::from_expr(db, closure_in_file, Some(pipe_tok)))
305+
Some(expr_to_nav(db, closure_in_file, Some(pipe_tok)))
303306
},
304307
ast::BlockExpr(blk) => {
305308
match blk.modifier() {
306309
Some(ast::BlockModifier::Async(_)) => {
307310
let async_tok = blk.async_token()?.text_range();
308311
let blk_in_file = InFile::new(file_id, blk.into());
309-
Some(NavigationTarget::from_expr(db, blk_in_file, Some(async_tok)))
312+
Some(expr_to_nav(db, blk_in_file, Some(async_tok)))
310313
},
311314
Some(ast::BlockModifier::Try(_)) if token_kind != T![return] => {
312315
let try_tok = blk.try_token()?.text_range();
313316
let blk_in_file = InFile::new(file_id, blk.into());
314-
Some(NavigationTarget::from_expr(db, blk_in_file, Some(try_tok)))
317+
Some(expr_to_nav(db, blk_in_file, Some(try_tok)))
315318
},
316319
_ => None,
317320
}
@@ -390,7 +393,7 @@ fn nav_for_break_points(
390393
ast::Expr::BlockExpr(blk) => blk.label().unwrap().syntax().text_range(),
391394
_ => return None,
392395
};
393-
let nav = NavigationTarget::from_expr(db, expr_in_file, Some(focus_range));
396+
let nav = expr_to_nav(db, expr_in_file, Some(focus_range));
394397
Some(nav)
395398
})
396399
.flatten()
@@ -403,6 +406,20 @@ fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
403406
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
404407
}
405408

409+
fn expr_to_nav(
410+
db: &RootDatabase,
411+
InFile { file_id, value }: InFile<ast::Expr>,
412+
focus_range: Option<TextRange>,
413+
) -> UpmappingResult<NavigationTarget> {
414+
let kind = SymbolKind::Label;
415+
416+
let value_range = value.syntax().text_range();
417+
let navs = navigation_target::orig_range_with_focus_r(db, file_id, value_range, focus_range);
418+
navs.map(|(hir::FileRangeWrapper { file_id, range }, focus_range)| {
419+
NavigationTarget::from_syntax(file_id, "<expr>".into(), focus_range, range, kind)
420+
})
421+
}
422+
406423
#[cfg(test)]
407424
mod tests {
408425
use ide_db::FileRange;

src/tools/rust-analyzer/crates/ide/src/highlight_related.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ use std::iter;
22

33
use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics};
44
use ide_db::{
5-
defs::{Definition, IdentClass}, helpers::pick_best_token, search::{FileReference, ReferenceCategory, SearchScope}, syntax_helpers::node_ext::{
5+
defs::{Definition, IdentClass},
6+
helpers::pick_best_token,
7+
search::{FileReference, ReferenceCategory, SearchScope},
8+
syntax_helpers::node_ext::{
69
eq_label_lt, for_each_tail_expr, full_path_of_name_ref, is_closure_or_blk_with_modif,
710
preorder_expr_with_ctx_checker,
8-
}, FxHashMap, FxHashSet, RootDatabase
11+
},
12+
FxHashMap, FxHashSet, RootDatabase,
913
};
1014
use span::EditionedFileId;
1115
use syntax::{

src/tools/rust-analyzer/crates/ide/src/navigation_target.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,7 @@ impl NavigationTarget {
152152
)
153153
}
154154

155-
pub(crate) fn from_expr(
156-
db: &RootDatabase,
157-
InFile { file_id, value }: InFile<ast::Expr>,
158-
focus_range: Option<TextRange>,
159-
) -> UpmappingResult<NavigationTarget> {
160-
let name: SmolStr = "<expr>".into();
161-
let kind = SymbolKind::Label;
162-
163-
orig_range_with_focus_r(db, file_id, value.syntax().text_range(), focus_range).map(
164-
|(FileRange { file_id, range: full_range }, focus_range)| {
165-
NavigationTarget::from_syntax(file_id, name.clone(), focus_range, full_range, kind)
166-
},
167-
)
168-
}
169-
170-
fn from_syntax(
155+
pub(crate) fn from_syntax(
171156
file_id: FileId,
172157
name: SmolStr,
173158
focus_range: Option<TextRange>,
@@ -747,7 +732,7 @@ fn orig_range_with_focus(
747732
)
748733
}
749734

750-
fn orig_range_with_focus_r(
735+
pub(crate) fn orig_range_with_focus_r(
751736
db: &RootDatabase,
752737
hir_file: HirFileId,
753738
value: TextRange,

0 commit comments

Comments
 (0)