mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-25 11:17:13 +00:00
make TryToNav take Semantics instead of RootDatabase
This commit is contained in:
parent
091cc8dc47
commit
ab44e6d504
@ -67,7 +67,7 @@ pub(crate) fn incoming_calls(
|
||||
let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
|
||||
// We should return def before check if it is a test, so that we
|
||||
// will not continue to search for outer fn in nested fns
|
||||
def.try_to_nav(sema.db).map(|nav| (def, nav))
|
||||
def.try_to_nav(sema).map(|nav| (def, nav))
|
||||
});
|
||||
|
||||
if let Some((def, nav)) = def_nav {
|
||||
@ -122,10 +122,10 @@ pub(crate) fn outgoing_calls(
|
||||
if exclude_tests && it.is_test(db) {
|
||||
return None;
|
||||
}
|
||||
it.try_to_nav(db)
|
||||
it.try_to_nav(&sema)
|
||||
}
|
||||
hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(db),
|
||||
hir::CallableKind::TupleStruct(it) => it.try_to_nav(db),
|
||||
hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(&sema),
|
||||
hir::CallableKind::TupleStruct(it) => it.try_to_nav(&sema),
|
||||
_ => None,
|
||||
}
|
||||
.zip(Some(sema.original_range(expr.syntax())))
|
||||
@ -136,7 +136,7 @@ pub(crate) fn outgoing_calls(
|
||||
return None;
|
||||
}
|
||||
function
|
||||
.try_to_nav(db)
|
||||
.try_to_nav(&sema)
|
||||
.zip(Some(sema.original_range(expr.name_ref()?.syntax())))
|
||||
}
|
||||
}?;
|
||||
|
@ -68,7 +68,7 @@ fn check_doc_links(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
|
||||
resolve_doc_path_for_def(sema.db, cursor_def, &link, ns, is_inner_attr)
|
||||
.unwrap_or_else(|| panic!("Failed to resolve {link}"))
|
||||
});
|
||||
def.try_to_nav(sema.db).unwrap().into_iter().zip(iter::repeat(link))
|
||||
def.try_to_nav(sema).unwrap().into_iter().zip(iter::repeat(link))
|
||||
})
|
||||
.map(|(nav_target, link)| {
|
||||
let range =
|
||||
|
@ -38,14 +38,14 @@ pub(crate) fn goto_declaration(
|
||||
ast::NameRef(name_ref) => match NameRefClass::classify(&sema, &name_ref)? {
|
||||
NameRefClass::Definition(it, _) => Some(it),
|
||||
NameRefClass::FieldShorthand { field_ref, .. } =>
|
||||
return field_ref.try_to_nav(db),
|
||||
return field_ref.try_to_nav(&sema),
|
||||
NameRefClass::ExternCrateShorthand { decl, .. } =>
|
||||
return decl.try_to_nav(db),
|
||||
return decl.try_to_nav(&sema),
|
||||
},
|
||||
ast::Name(name) => match NameClass::classify(&sema, &name)? {
|
||||
NameClass::Definition(it) | NameClass::ConstReference(it) => Some(it),
|
||||
NameClass::PatFieldShorthand { field_ref, .. } =>
|
||||
return field_ref.try_to_nav(db),
|
||||
return field_ref.try_to_nav(&sema),
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
@ -57,14 +57,14 @@ pub(crate) fn goto_declaration(
|
||||
Definition::Const(c) => c.as_assoc_item(db),
|
||||
Definition::TypeAlias(ta) => ta.as_assoc_item(db),
|
||||
Definition::Function(f) => f.as_assoc_item(db),
|
||||
Definition::ExternCrateDecl(it) => return it.try_to_nav(db),
|
||||
Definition::ExternCrateDecl(it) => return it.try_to_nav(&sema),
|
||||
_ => None,
|
||||
}?;
|
||||
|
||||
let trait_ = assoc.implemented_trait(db)?;
|
||||
let name = Some(assoc.name(db)?);
|
||||
let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?;
|
||||
item.try_to_nav(db)
|
||||
item.try_to_nav(&sema)
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
|
@ -62,7 +62,7 @@ pub(crate) fn goto_definition(
|
||||
})?;
|
||||
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
|
||||
return doc_comment.get_definition_with_descend_at(sema, offset, |def, _, link_range| {
|
||||
let nav = def.try_to_nav(db)?;
|
||||
let nav = def.try_to_nav(sema)?;
|
||||
Some(RangeInfo::new(link_range, nav.collect()))
|
||||
});
|
||||
}
|
||||
@ -73,7 +73,7 @@ pub(crate) fn goto_definition(
|
||||
return Some(RangeInfo::new(
|
||||
range,
|
||||
match resolution {
|
||||
Some(res) => def_to_nav(db, Definition::from(res)),
|
||||
Some(res) => def_to_nav(sema, Definition::from(res)),
|
||||
None => vec![],
|
||||
},
|
||||
));
|
||||
@ -121,7 +121,7 @@ pub(crate) fn goto_definition(
|
||||
.collect();
|
||||
}
|
||||
try_filter_trait_item_definition(sema, &def)
|
||||
.unwrap_or_else(|| def_to_nav(sema.db, def))
|
||||
.unwrap_or_else(|| def_to_nav(sema, def))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
@ -160,7 +160,7 @@ fn find_definition_for_known_blanket_dual_impls(
|
||||
t_f,
|
||||
[return_type.type_arguments().next()?],
|
||||
)
|
||||
.map(|f| def_to_nav(sema.db, f.into()));
|
||||
.map(|f| def_to_nav(sema, f.into()));
|
||||
}
|
||||
hir::AssocItemContainer::Impl(_) => return None,
|
||||
};
|
||||
@ -201,7 +201,7 @@ fn find_definition_for_known_blanket_dual_impls(
|
||||
// succeed
|
||||
let _t = f.as_assoc_item(sema.db)?.implemented_trait(sema.db)?;
|
||||
let def = Definition::from(f);
|
||||
Some(def_to_nav(sema.db, def))
|
||||
Some(def_to_nav(sema, def))
|
||||
}
|
||||
|
||||
fn try_lookup_include_path(
|
||||
@ -246,7 +246,7 @@ fn try_lookup_macro_def_in_macro_use(
|
||||
for mod_def in krate.root_module().declarations(sema.db) {
|
||||
if let ModuleDef::Macro(mac) = mod_def
|
||||
&& mac.name(sema.db).as_str() == token.text()
|
||||
&& let Some(nav) = mac.try_to_nav(sema.db)
|
||||
&& let Some(nav) = mac.try_to_nav(sema)
|
||||
{
|
||||
return Some(nav.call_site);
|
||||
}
|
||||
@ -278,7 +278,7 @@ fn try_filter_trait_item_definition(
|
||||
.items(db)
|
||||
.iter()
|
||||
.filter(|itm| discriminant(*itm) == discriminant_value)
|
||||
.find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten())
|
||||
.find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(sema)).flatten())
|
||||
.map(|it| it.collect())
|
||||
}
|
||||
}
|
||||
@ -347,7 +347,7 @@ fn nav_for_exit_points(
|
||||
match_ast! {
|
||||
match node {
|
||||
ast::Fn(fn_) => {
|
||||
let mut nav = sema.to_def(&fn_)?.try_to_nav(db)?;
|
||||
let mut nav = sema.to_def(&fn_)?.try_to_nav(sema)?;
|
||||
// For async token, we navigate to itself, which triggers
|
||||
// VSCode to find the references
|
||||
let focus_token = if matches!(token_kind, T![async]) {
|
||||
@ -564,8 +564,8 @@ fn nav_for_break_points(
|
||||
Some(navs)
|
||||
}
|
||||
|
||||
fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
|
||||
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
|
||||
fn def_to_nav(sema: &Semantics<'_, RootDatabase>, def: Definition) -> Vec<NavigationTarget> {
|
||||
def.try_to_nav(sema).map(|it| it.collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn expr_to_nav(
|
||||
|
@ -86,7 +86,7 @@ pub(crate) fn goto_implementation(
|
||||
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type<'_>) -> Vec<NavigationTarget> {
|
||||
Impl::all_for_type(sema.db, ty)
|
||||
.into_iter()
|
||||
.filter_map(|imp| imp.try_to_nav(sema.db))
|
||||
.filter_map(|imp| imp.try_to_nav(sema))
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
@ -97,7 +97,7 @@ fn impls_for_trait(
|
||||
) -> Vec<NavigationTarget> {
|
||||
Impl::all_for_trait(sema.db, trait_)
|
||||
.into_iter()
|
||||
.filter_map(|imp| imp.try_to_nav(sema.db))
|
||||
.filter_map(|imp| imp.try_to_nav(sema))
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
@ -114,7 +114,7 @@ fn impls_for_trait_item(
|
||||
let itm_name = itm.name(sema.db)?;
|
||||
(itm_name == fun_name).then_some(*itm)
|
||||
})?;
|
||||
item.try_to_nav(sema.db)
|
||||
item.try_to_nav(sema)
|
||||
})
|
||||
.flatten()
|
||||
.collect()
|
||||
|
@ -30,7 +30,7 @@ pub(crate) fn goto_type_definition(
|
||||
|
||||
let mut res = Vec::new();
|
||||
let mut push = |def: Definition| {
|
||||
if let Some(navs) = def.try_to_nav(db) {
|
||||
if let Some(navs) = def.try_to_nav(&sema) {
|
||||
for nav in navs {
|
||||
if !res.contains(&nav) {
|
||||
res.push(nav);
|
||||
|
@ -277,7 +277,7 @@ fn highlight_references(
|
||||
Definition::Module(module) => {
|
||||
NavigationTarget::from_module_to_decl(sema.db, module)
|
||||
}
|
||||
def => match def.try_to_nav(sema.db) {
|
||||
def => match def.try_to_nav(sema) {
|
||||
Some(it) => it,
|
||||
None => continue,
|
||||
},
|
||||
|
@ -85,10 +85,11 @@ pub enum HoverAction {
|
||||
|
||||
impl HoverAction {
|
||||
fn goto_type_from_targets(
|
||||
db: &RootDatabase,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
targets: Vec<hir::ModuleDef>,
|
||||
edition: Edition,
|
||||
) -> Option<Self> {
|
||||
let db = sema.db;
|
||||
let targets = targets
|
||||
.into_iter()
|
||||
.filter_map(|it| {
|
||||
@ -99,7 +100,7 @@ impl HoverAction {
|
||||
it.name(db).map(|name| name.display(db, edition).to_string()),
|
||||
edition,
|
||||
),
|
||||
nav: it.try_to_nav(db)?.call_site(),
|
||||
nav: it.try_to_nav(sema)?.call_site(),
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@ -467,10 +468,10 @@ pub(crate) fn hover_for_definition(
|
||||
HoverResult {
|
||||
markup: render::process_markup(sema.db, def, &markup, range_map, config),
|
||||
actions: [
|
||||
show_fn_references_action(sema.db, def),
|
||||
show_implementations_action(sema.db, def),
|
||||
show_fn_references_action(sema, def),
|
||||
show_implementations_action(sema, def),
|
||||
runnable_action(sema, def, file_id),
|
||||
goto_type_action_for_def(sema.db, def, ¬able_traits, subst_types, edition),
|
||||
goto_type_action_for_def(sema, def, ¬able_traits, subst_types, edition),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
@ -505,7 +506,10 @@ fn notable_traits<'db>(
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
|
||||
fn show_implementations_action(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
def: Definition,
|
||||
) -> Option<HoverAction> {
|
||||
fn to_action(nav_target: NavigationTarget) -> HoverAction {
|
||||
HoverAction::Implementation(FilePosition {
|
||||
file_id: nav_target.file_id,
|
||||
@ -515,19 +519,22 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
|
||||
|
||||
let adt = match def {
|
||||
Definition::Trait(it) => {
|
||||
return it.try_to_nav(db).map(UpmappingResult::call_site).map(to_action);
|
||||
return it.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action);
|
||||
}
|
||||
Definition::Adt(it) => Some(it),
|
||||
Definition::SelfType(it) => it.self_ty(db).as_adt(),
|
||||
Definition::SelfType(it) => it.self_ty(sema.db).as_adt(),
|
||||
_ => None,
|
||||
}?;
|
||||
adt.try_to_nav(db).map(UpmappingResult::call_site).map(to_action)
|
||||
adt.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action)
|
||||
}
|
||||
|
||||
fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
|
||||
fn show_fn_references_action(
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
def: Definition,
|
||||
) -> Option<HoverAction> {
|
||||
match def {
|
||||
Definition::Function(it) => {
|
||||
it.try_to_nav(db).map(UpmappingResult::call_site).map(|nav_target| {
|
||||
it.try_to_nav(sema).map(UpmappingResult::call_site).map(|nav_target| {
|
||||
HoverAction::Reference(FilePosition {
|
||||
file_id: nav_target.file_id,
|
||||
offset: nav_target.focus_or_full_range().start(),
|
||||
@ -560,12 +567,13 @@ fn runnable_action(
|
||||
}
|
||||
|
||||
fn goto_type_action_for_def(
|
||||
db: &RootDatabase,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
def: Definition,
|
||||
notable_traits: &[(hir::Trait, Vec<(Option<hir::Type<'_>>, hir::Name)>)],
|
||||
subst_types: Option<Vec<(hir::Symbol, hir::Type<'_>)>>,
|
||||
edition: Edition,
|
||||
) -> Option<HoverAction> {
|
||||
let db = sema.db;
|
||||
let mut targets: Vec<hir::ModuleDef> = Vec::new();
|
||||
let mut push_new_def = |item: hir::ModuleDef| {
|
||||
if !targets.contains(&item) {
|
||||
@ -612,7 +620,7 @@ fn goto_type_action_for_def(
|
||||
}
|
||||
}
|
||||
|
||||
HoverAction::goto_type_from_targets(db, targets, edition)
|
||||
HoverAction::goto_type_from_targets(sema, targets, edition)
|
||||
}
|
||||
|
||||
fn walk_and_push_ty(
|
||||
|
@ -128,7 +128,7 @@ pub(super) fn try_expr(
|
||||
};
|
||||
walk_and_push_ty(sema.db, &inner_ty, &mut push_new_def);
|
||||
walk_and_push_ty(sema.db, &body_ty, &mut push_new_def);
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
|
||||
res.actions.push(actions);
|
||||
}
|
||||
|
||||
@ -210,7 +210,7 @@ pub(super) fn deref_expr(
|
||||
)
|
||||
.into()
|
||||
};
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
|
||||
res.actions.push(actions);
|
||||
}
|
||||
|
||||
@ -323,7 +323,7 @@ pub(super) fn struct_rest_pat(
|
||||
|
||||
Markup::fenced_block(&s)
|
||||
};
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
|
||||
res.actions.push(actions);
|
||||
}
|
||||
res
|
||||
@ -1030,7 +1030,7 @@ fn type_info(
|
||||
};
|
||||
desc.into()
|
||||
};
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) {
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
|
||||
res.actions.push(actions);
|
||||
}
|
||||
Some(res)
|
||||
@ -1098,7 +1098,7 @@ fn closure_ty(
|
||||
format_to!(markup, "{adjusted}\n\n## Captures\n{}", captures_rendered,);
|
||||
|
||||
let mut res = HoverResult::default();
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
|
||||
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
|
||||
res.actions.push(actions);
|
||||
}
|
||||
res.markup = markup.into();
|
||||
@ -1302,7 +1302,7 @@ fn keyword_hints(
|
||||
KeywordHint {
|
||||
description,
|
||||
keyword_mod,
|
||||
actions: HoverAction::goto_type_from_targets(sema.db, targets, edition)
|
||||
actions: HoverAction::goto_type_from_targets(sema, targets, edition)
|
||||
.into_iter()
|
||||
.collect(),
|
||||
}
|
||||
|
@ -672,7 +672,7 @@ impl fmt::Debug for InlayHintLabelPart {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct InlayHintLabelBuilder<'a> {
|
||||
db: &'a RootDatabase,
|
||||
sema: &'a Semantics<'a, RootDatabase>,
|
||||
result: InlayHintLabel,
|
||||
last_part: String,
|
||||
resolve: bool,
|
||||
@ -694,7 +694,7 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
|
||||
LazyProperty::Lazy
|
||||
} else {
|
||||
LazyProperty::Computed({
|
||||
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
|
||||
let Some(location) = ModuleDef::from(def).try_to_nav(self.sema) else { return };
|
||||
let location = location.call_site();
|
||||
FileRange { file_id: location.file_id, range: location.focus_or_full_range() }
|
||||
})
|
||||
@ -782,7 +782,7 @@ fn label_of_ty(
|
||||
}
|
||||
|
||||
let mut label_builder = InlayHintLabelBuilder {
|
||||
db: sema.db,
|
||||
sema,
|
||||
last_part: String::new(),
|
||||
location: None,
|
||||
result: InlayHintLabel::default(),
|
||||
|
@ -44,7 +44,7 @@ pub(super) fn hints(
|
||||
text: "Sized".to_owned(),
|
||||
linked_location: sized_trait.and_then(|it| {
|
||||
config.lazy_location_opt(|| {
|
||||
it.try_to_nav(sema.db).map(|it| {
|
||||
it.try_to_nav(sema).map(|it| {
|
||||
let n = it.call_site();
|
||||
FileRange {
|
||||
file_id: n.file_id,
|
||||
|
@ -483,7 +483,7 @@ impl Analysis {
|
||||
salsa::attach(&self.db, || {
|
||||
symbols
|
||||
.into_iter()
|
||||
.filter_map(|s| s.try_to_nav(&self.db))
|
||||
.filter_map(|s| s.try_to_nav(&Semantics::new(&self.db)))
|
||||
.take(limit)
|
||||
.map(UpmappingResult::call_site)
|
||||
.collect::<Vec<_>>()
|
||||
|
@ -83,14 +83,20 @@ pub(crate) trait ToNav {
|
||||
}
|
||||
|
||||
pub trait TryToNav {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>>;
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>>;
|
||||
}
|
||||
|
||||
impl<T: TryToNav, U: TryToNav> TryToNav for Either<T, U> {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
Either::Left(it) => it.try_to_nav(db),
|
||||
Either::Right(it) => it.try_to_nav(db),
|
||||
Either::Left(it) => it.try_to_nav(sema),
|
||||
Either::Right(it) => it.try_to_nav(sema),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -185,7 +191,11 @@ impl NavigationTarget {
|
||||
}
|
||||
|
||||
impl TryToNav for FileSymbol {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let edition =
|
||||
self.def.module(db).map(|it| it.krate().edition(db)).unwrap_or(Edition::CURRENT);
|
||||
let display_target = self.def.krate(db).to_display_target(db);
|
||||
@ -244,49 +254,55 @@ impl TryToNav for FileSymbol {
|
||||
}
|
||||
|
||||
impl TryToNav for Definition {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
Definition::Local(it) => Some(it.to_nav(db)),
|
||||
Definition::Label(it) => it.try_to_nav(db),
|
||||
Definition::Module(it) => Some(it.to_nav(db)),
|
||||
Definition::Crate(it) => Some(it.to_nav(db)),
|
||||
Definition::Macro(it) => it.try_to_nav(db),
|
||||
Definition::Field(it) => it.try_to_nav(db),
|
||||
Definition::SelfType(it) => it.try_to_nav(db),
|
||||
Definition::GenericParam(it) => it.try_to_nav(db),
|
||||
Definition::Function(it) => it.try_to_nav(db),
|
||||
Definition::Adt(it) => it.try_to_nav(db),
|
||||
Definition::Variant(it) => it.try_to_nav(db),
|
||||
Definition::Const(it) => it.try_to_nav(db),
|
||||
Definition::Static(it) => it.try_to_nav(db),
|
||||
Definition::Trait(it) => it.try_to_nav(db),
|
||||
Definition::TypeAlias(it) => it.try_to_nav(db),
|
||||
Definition::ExternCrateDecl(it) => it.try_to_nav(db),
|
||||
Definition::InlineAsmOperand(it) => it.try_to_nav(db),
|
||||
Definition::BuiltinType(it) => it.try_to_nav(db),
|
||||
Definition::Local(it) => Some(it.to_nav(sema.db)),
|
||||
Definition::Label(it) => it.try_to_nav(sema),
|
||||
Definition::Module(it) => Some(it.to_nav(sema.db)),
|
||||
Definition::Crate(it) => Some(it.to_nav(sema.db)),
|
||||
Definition::Macro(it) => it.try_to_nav(sema),
|
||||
Definition::Field(it) => it.try_to_nav(sema),
|
||||
Definition::SelfType(it) => it.try_to_nav(sema),
|
||||
Definition::GenericParam(it) => it.try_to_nav(sema),
|
||||
Definition::Function(it) => it.try_to_nav(sema),
|
||||
Definition::Adt(it) => it.try_to_nav(sema),
|
||||
Definition::Variant(it) => it.try_to_nav(sema),
|
||||
Definition::Const(it) => it.try_to_nav(sema),
|
||||
Definition::Static(it) => it.try_to_nav(sema),
|
||||
Definition::Trait(it) => it.try_to_nav(sema),
|
||||
Definition::TypeAlias(it) => it.try_to_nav(sema),
|
||||
Definition::ExternCrateDecl(it) => it.try_to_nav(sema),
|
||||
Definition::InlineAsmOperand(it) => it.try_to_nav(sema),
|
||||
Definition::BuiltinType(it) => it.try_to_nav(sema),
|
||||
Definition::BuiltinLifetime(_)
|
||||
| Definition::TupleField(_)
|
||||
| Definition::ToolModule(_)
|
||||
| Definition::InlineAsmRegOrRegClass(_)
|
||||
| Definition::BuiltinAttr(_) => None,
|
||||
// FIXME: The focus range should be set to the helper declaration
|
||||
Definition::DeriveHelper(it) => it.derive().try_to_nav(db),
|
||||
Definition::DeriveHelper(it) => it.derive().try_to_nav(sema),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryToNav for hir::ModuleDef {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
hir::ModuleDef::Module(it) => Some(it.to_nav(db)),
|
||||
hir::ModuleDef::Function(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Adt(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Variant(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Const(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Static(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Trait(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::TypeAlias(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Macro(it) => it.try_to_nav(db),
|
||||
hir::ModuleDef::Module(it) => Some(it.to_nav(sema.db)),
|
||||
hir::ModuleDef::Function(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Adt(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Variant(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Const(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Static(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Trait(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::TypeAlias(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::Macro(it) => it.try_to_nav(sema),
|
||||
hir::ModuleDef::BuiltinType(_) => None,
|
||||
}
|
||||
}
|
||||
@ -369,7 +385,11 @@ where
|
||||
D: HasSource + ToNavFromAst + Copy + HasDocs + HirDisplay + HasCrate,
|
||||
D::Ast: ast::HasName,
|
||||
{
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let src = self.source(db)?;
|
||||
Some(
|
||||
NavigationTarget::from_named(
|
||||
@ -423,7 +443,11 @@ impl ToNav for hir::Crate {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::Impl {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = self.source(db)?;
|
||||
let derive_path = self.as_builtin_derive_path(db);
|
||||
|
||||
@ -447,7 +471,11 @@ impl TryToNav for hir::Impl {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::ExternCrateDecl {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let src = self.source(db)?;
|
||||
let InFile { file_id, value } = src;
|
||||
let focus = value
|
||||
@ -479,7 +507,11 @@ impl TryToNav for hir::ExternCrateDecl {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::Field {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let src = self.source(db)?;
|
||||
let krate = self.parent_def(db).module(db).krate();
|
||||
|
||||
@ -512,7 +544,11 @@ impl TryToNav for hir::Field {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::Macro {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let src = self.source(db)?;
|
||||
let name_owner: &dyn ast::HasName = match &src.value {
|
||||
Either::Left(it) => it,
|
||||
@ -533,31 +569,40 @@ impl TryToNav for hir::Macro {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::Adt {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
hir::Adt::Struct(it) => it.try_to_nav(db),
|
||||
hir::Adt::Union(it) => it.try_to_nav(db),
|
||||
hir::Adt::Enum(it) => it.try_to_nav(db),
|
||||
hir::Adt::Struct(it) => it.try_to_nav(sema),
|
||||
hir::Adt::Union(it) => it.try_to_nav(sema),
|
||||
hir::Adt::Enum(it) => it.try_to_nav(sema),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryToNav for hir::AssocItem {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
AssocItem::Function(it) => it.try_to_nav(db),
|
||||
AssocItem::Const(it) => it.try_to_nav(db),
|
||||
AssocItem::TypeAlias(it) => it.try_to_nav(db),
|
||||
AssocItem::Function(it) => it.try_to_nav(sema),
|
||||
AssocItem::Const(it) => it.try_to_nav(sema),
|
||||
AssocItem::TypeAlias(it) => it.try_to_nav(sema),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryToNav for hir::GenericParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
match self {
|
||||
hir::GenericParam::TypeParam(it) => it.try_to_nav(db),
|
||||
hir::GenericParam::ConstParam(it) => it.try_to_nav(db),
|
||||
hir::GenericParam::LifetimeParam(it) => it.try_to_nav(db),
|
||||
hir::GenericParam::TypeParam(it) => it.try_to_nav(sema),
|
||||
hir::GenericParam::ConstParam(it) => it.try_to_nav(sema),
|
||||
hir::GenericParam::LifetimeParam(it) => it.try_to_nav(sema),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -606,7 +651,11 @@ impl ToNav for hir::Local {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::Label {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = self.source(db)?;
|
||||
// Labels can't be keywords, so no escaping needed.
|
||||
let name = self.name(db).display_no_db(Edition::Edition2015).to_smolstr();
|
||||
@ -628,7 +677,11 @@ impl TryToNav for hir::Label {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::TypeParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = self.merge().source(db)?;
|
||||
let edition = self.module(db).krate().edition(db);
|
||||
let name = self.name(db).display_no_db(edition).to_smolstr();
|
||||
@ -665,13 +718,20 @@ impl TryToNav for hir::TypeParam {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::TypeOrConstParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
self.split(db).try_to_nav(db)
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
self.split(sema.db).try_to_nav(sema)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryToNav for hir::LifetimeParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = self.source(db)?;
|
||||
// Lifetimes cannot be keywords, so not escaping needed.
|
||||
let name = self.name(db).display_no_db(Edition::Edition2015).to_smolstr();
|
||||
@ -693,7 +753,11 @@ impl TryToNav for hir::LifetimeParam {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::ConstParam {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = self.merge().source(db)?;
|
||||
let edition = self.module(db).krate().edition(db);
|
||||
let name = self.name(db).display_no_db(edition).to_smolstr();
|
||||
@ -723,7 +787,11 @@ impl TryToNav for hir::ConstParam {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::InlineAsmOperand {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let InFile { file_id, value } = &self.source(db)?;
|
||||
let file_id = *file_id;
|
||||
Some(orig_range_with_focus(db, file_id, value.syntax(), value.name()).map(
|
||||
@ -748,9 +816,11 @@ impl TryToNav for hir::InlineAsmOperand {
|
||||
}
|
||||
|
||||
impl TryToNav for hir::BuiltinType {
|
||||
fn try_to_nav(&self, db: &RootDatabase) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let sema = Semantics::new(db);
|
||||
|
||||
fn try_to_nav(
|
||||
&self,
|
||||
sema: &Semantics<'_, RootDatabase>,
|
||||
) -> Option<UpmappingResult<NavigationTarget>> {
|
||||
let db = sema.db;
|
||||
let krate = db
|
||||
.all_crates()
|
||||
.iter()
|
||||
@ -759,7 +829,7 @@ impl TryToNav for hir::BuiltinType {
|
||||
.map(Crate::from)?;
|
||||
let edition = krate.edition(db);
|
||||
|
||||
let fd = FamousDefs(&sema, krate);
|
||||
let fd = FamousDefs(sema, krate);
|
||||
let primitive_mod = format!("prim_{}", self.name().display(fd.0.db, edition));
|
||||
let doc_owner = find_std_module(&fd, &primitive_mod, edition)?;
|
||||
|
||||
|
@ -138,7 +138,7 @@ pub(crate) fn find_all_refs(
|
||||
Definition::Module(module) => {
|
||||
Some(NavigationTarget::from_module_to_decl(sema.db, module))
|
||||
}
|
||||
def => def.try_to_nav(sema.db),
|
||||
def => def.try_to_nav(sema),
|
||||
}
|
||||
.map(|nav| {
|
||||
let (nav, extra_ref) = match nav.def_site {
|
||||
|
@ -158,15 +158,15 @@ pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||
Definition::SelfType(impl_) => runnable_impl(&sema, &impl_),
|
||||
_ => None,
|
||||
};
|
||||
add_opt(runnable.or_else(|| module_def_doctest(sema.db, def)), Some(def));
|
||||
add_opt(runnable.or_else(|| module_def_doctest(&sema, def)), Some(def));
|
||||
if let Definition::SelfType(impl_) = def {
|
||||
impl_.items(db).into_iter().for_each(|assoc| {
|
||||
let runnable = match assoc {
|
||||
hir::AssocItem::Function(it) => {
|
||||
runnable_fn(&sema, it).or_else(|| module_def_doctest(sema.db, it.into()))
|
||||
runnable_fn(&sema, it).or_else(|| module_def_doctest(&sema, it.into()))
|
||||
}
|
||||
hir::AssocItem::Const(it) => module_def_doctest(sema.db, it.into()),
|
||||
hir::AssocItem::TypeAlias(it) => module_def_doctest(sema.db, it.into()),
|
||||
hir::AssocItem::Const(it) => module_def_doctest(&sema, it.into()),
|
||||
hir::AssocItem::TypeAlias(it) => module_def_doctest(&sema, it.into()),
|
||||
};
|
||||
add_opt(runnable, Some(assoc.into()))
|
||||
});
|
||||
@ -410,7 +410,7 @@ pub(crate) fn runnable_impl(
|
||||
return None;
|
||||
}
|
||||
let cfg = attrs.cfg();
|
||||
let nav = def.try_to_nav(sema.db)?.call_site();
|
||||
let nav = def.try_to_nav(sema)?.call_site();
|
||||
let ty = def.self_ty(sema.db);
|
||||
let adt_name = ty.as_adt()?.name(sema.db);
|
||||
let mut ty_args = ty.generic_parameters(sema.db, display_target).peekable();
|
||||
@ -486,7 +486,8 @@ fn runnable_mod_outline_definition(
|
||||
})
|
||||
}
|
||||
|
||||
fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
|
||||
fn module_def_doctest(sema: &Semantics<'_, RootDatabase>, def: Definition) -> Option<Runnable> {
|
||||
let db = sema.db;
|
||||
let attrs = match def {
|
||||
Definition::Module(it) => it.attrs(db),
|
||||
Definition::Function(it) => it.attrs(db),
|
||||
@ -540,7 +541,7 @@ fn module_def_doctest(db: &RootDatabase, def: Definition) -> Option<Runnable> {
|
||||
|
||||
let mut nav = match def {
|
||||
Definition::Module(def) => NavigationTarget::from_module_to_decl(db, def),
|
||||
def => def.try_to_nav(db)?,
|
||||
def => def.try_to_nav(sema)?,
|
||||
}
|
||||
.call_site();
|
||||
nav.focus_range = None;
|
||||
|
@ -243,7 +243,7 @@ impl StaticIndex<'_> {
|
||||
edition,
|
||||
display_target,
|
||||
)),
|
||||
definition: def.try_to_nav(self.db).map(UpmappingResult::call_site).map(
|
||||
definition: def.try_to_nav(&sema).map(UpmappingResult::call_site).map(
|
||||
|it| FileRange { file_id: it.file_id, range: it.focus_or_full_range() },
|
||||
),
|
||||
references: vec![],
|
||||
@ -261,7 +261,7 @@ impl StaticIndex<'_> {
|
||||
let token = self.tokens.get_mut(id).unwrap();
|
||||
token.references.push(ReferenceData {
|
||||
range: FileRange { range, file_id },
|
||||
is_definition: match def.try_to_nav(self.db).map(UpmappingResult::call_site) {
|
||||
is_definition: match def.try_to_nav(&sema).map(UpmappingResult::call_site) {
|
||||
Some(it) => it.file_id == file_id && it.focus_or_full_range() == range,
|
||||
None => false,
|
||||
},
|
||||
|
@ -94,7 +94,7 @@ fn discover_tests_in_module(
|
||||
if !f.is_test(db) {
|
||||
continue;
|
||||
}
|
||||
let nav = f.try_to_nav(db).map(|r| r.call_site);
|
||||
let nav = f.try_to_nav(&sema).map(|r| r.call_site);
|
||||
let fn_name = f.name(db).as_str().to_owned();
|
||||
r.push(TestItem {
|
||||
id: format!("{prefix_id}::{fn_name}"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user