mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
internal: move NavigationTarget::from_expr to goto_definition
This commit is contained in:
parent
1b59cf2d52
commit
f027a46d94
@ -1,20 +1,23 @@
|
|||||||
use std::{iter, mem::discriminant};
|
use std::{iter, mem::discriminant};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
doc_links::token_as_doc_comment, navigation_target::ToNav, FilePosition, NavigationTarget,
|
doc_links::token_as_doc_comment,
|
||||||
RangeInfo, TryToNav,
|
navigation_target::{self, ToNav},
|
||||||
|
FilePosition, NavigationTarget, RangeInfo, TryToNav, UpmappingResult,
|
||||||
};
|
};
|
||||||
use hir::{
|
use hir::{
|
||||||
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef, Semantics
|
AsAssocItem, AssocItem, DescendPreference, FileRange, InFile, MacroFileIdExt, ModuleDef,
|
||||||
|
Semantics,
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{AnchoredPath, FileLoader},
|
base_db::{AnchoredPath, FileLoader},
|
||||||
defs::{Definition, IdentClass},
|
defs::{Definition, IdentClass},
|
||||||
helpers::pick_best_token,
|
helpers::pick_best_token,
|
||||||
FileId, RootDatabase,
|
RootDatabase, SymbolKind,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use span::FileId;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, HasLoopBody},
|
ast::{self, HasLoopBody},
|
||||||
match_ast, AstNode, AstToken,
|
match_ast, AstNode, AstToken,
|
||||||
@ -299,19 +302,19 @@ fn nav_for_exit_points(
|
|||||||
ast::ClosureExpr(c) => {
|
ast::ClosureExpr(c) => {
|
||||||
let pipe_tok = c.param_list().and_then(|it| it.pipe_token())?.text_range();
|
let pipe_tok = c.param_list().and_then(|it| it.pipe_token())?.text_range();
|
||||||
let closure_in_file = InFile::new(file_id, c.into());
|
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) => {
|
ast::BlockExpr(blk) => {
|
||||||
match blk.modifier() {
|
match blk.modifier() {
|
||||||
Some(ast::BlockModifier::Async(_)) => {
|
Some(ast::BlockModifier::Async(_)) => {
|
||||||
let async_tok = blk.async_token()?.text_range();
|
let async_tok = blk.async_token()?.text_range();
|
||||||
let blk_in_file = InFile::new(file_id, blk.into());
|
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] => {
|
Some(ast::BlockModifier::Try(_)) if token_kind != T![return] => {
|
||||||
let try_tok = blk.try_token()?.text_range();
|
let try_tok = blk.try_token()?.text_range();
|
||||||
let blk_in_file = InFile::new(file_id, blk.into());
|
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,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -390,7 +393,7 @@ fn nav_for_break_points(
|
|||||||
ast::Expr::BlockExpr(blk) => blk.label().unwrap().syntax().text_range(),
|
ast::Expr::BlockExpr(blk) => blk.label().unwrap().syntax().text_range(),
|
||||||
_ => return None,
|
_ => 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)
|
Some(nav)
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
@ -403,6 +406,20 @@ fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
|
|||||||
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
|
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn expr_to_nav(
|
||||||
|
db: &RootDatabase,
|
||||||
|
InFile { file_id, value }: InFile<ast::Expr>,
|
||||||
|
focus_range: Option<TextRange>,
|
||||||
|
) -> UpmappingResult<NavigationTarget> {
|
||||||
|
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, "<expr>".into(), focus_range, range, kind)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ide_db::FileRange;
|
use ide_db::FileRange;
|
||||||
|
@ -2,10 +2,14 @@ use std::iter;
|
|||||||
|
|
||||||
use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics};
|
use hir::{db, DescendPreference, FilePosition, FileRange, HirFileId, InFile, Semantics};
|
||||||
use ide_db::{
|
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,
|
eq_label_lt, for_each_tail_expr, full_path_of_name_ref, is_closure_or_blk_with_modif,
|
||||||
preorder_expr_with_ctx_checker,
|
preorder_expr_with_ctx_checker,
|
||||||
}, FxHashMap, FxHashSet, RootDatabase
|
},
|
||||||
|
FxHashMap, FxHashSet, RootDatabase,
|
||||||
};
|
};
|
||||||
use span::EditionedFileId;
|
use span::EditionedFileId;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -152,22 +152,7 @@ impl NavigationTarget {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_expr(
|
pub(crate) fn from_syntax(
|
||||||
db: &RootDatabase,
|
|
||||||
InFile { file_id, value }: InFile<ast::Expr>,
|
|
||||||
focus_range: Option<TextRange>,
|
|
||||||
) -> UpmappingResult<NavigationTarget> {
|
|
||||||
let name: SmolStr = "<expr>".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(
|
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
name: SmolStr,
|
name: SmolStr,
|
||||||
focus_range: Option<TextRange>,
|
focus_range: Option<TextRange>,
|
||||||
@ -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,
|
db: &RootDatabase,
|
||||||
hir_file: HirFileId,
|
hir_file: HirFileId,
|
||||||
value: TextRange,
|
value: TextRange,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user