Skip to content

Commit ab44e6d

Browse files
committed
make TryToNav take Semantics instead of RootDatabase
1 parent 091cc8d commit ab44e6d

17 files changed

+203
-124
lines changed

crates/ide/src/call_hierarchy.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub(crate) fn incoming_calls(
6767
let def = ast::Fn::cast(node).and_then(|fn_| sema.to_def(&fn_))?;
6868
// We should return def before check if it is a test, so that we
6969
// will not continue to search for outer fn in nested fns
70-
def.try_to_nav(sema.db).map(|nav| (def, nav))
70+
def.try_to_nav(sema).map(|nav| (def, nav))
7171
});
7272

7373
if let Some((def, nav)) = def_nav {
@@ -122,10 +122,10 @@ pub(crate) fn outgoing_calls(
122122
if exclude_tests && it.is_test(db) {
123123
return None;
124124
}
125-
it.try_to_nav(db)
125+
it.try_to_nav(&sema)
126126
}
127-
hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(db),
128-
hir::CallableKind::TupleStruct(it) => it.try_to_nav(db),
127+
hir::CallableKind::TupleEnumVariant(it) => it.try_to_nav(&sema),
128+
hir::CallableKind::TupleStruct(it) => it.try_to_nav(&sema),
129129
_ => None,
130130
}
131131
.zip(Some(sema.original_range(expr.syntax())))
@@ -136,7 +136,7 @@ pub(crate) fn outgoing_calls(
136136
return None;
137137
}
138138
function
139-
.try_to_nav(db)
139+
.try_to_nav(&sema)
140140
.zip(Some(sema.original_range(expr.name_ref()?.syntax())))
141141
}
142142
}?;

crates/ide/src/doc_links/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn check_doc_links(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
6868
resolve_doc_path_for_def(sema.db, cursor_def, &link, ns, is_inner_attr)
6969
.unwrap_or_else(|| panic!("Failed to resolve {link}"))
7070
});
71-
def.try_to_nav(sema.db).unwrap().into_iter().zip(iter::repeat(link))
71+
def.try_to_nav(sema).unwrap().into_iter().zip(iter::repeat(link))
7272
})
7373
.map(|(nav_target, link)| {
7474
let range =

crates/ide/src/goto_declaration.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ pub(crate) fn goto_declaration(
3838
ast::NameRef(name_ref) => match NameRefClass::classify(&sema, &name_ref)? {
3939
NameRefClass::Definition(it, _) => Some(it),
4040
NameRefClass::FieldShorthand { field_ref, .. } =>
41-
return field_ref.try_to_nav(db),
41+
return field_ref.try_to_nav(&sema),
4242
NameRefClass::ExternCrateShorthand { decl, .. } =>
43-
return decl.try_to_nav(db),
43+
return decl.try_to_nav(&sema),
4444
},
4545
ast::Name(name) => match NameClass::classify(&sema, &name)? {
4646
NameClass::Definition(it) | NameClass::ConstReference(it) => Some(it),
4747
NameClass::PatFieldShorthand { field_ref, .. } =>
48-
return field_ref.try_to_nav(db),
48+
return field_ref.try_to_nav(&sema),
4949
},
5050
_ => None
5151
}
@@ -57,14 +57,14 @@ pub(crate) fn goto_declaration(
5757
Definition::Const(c) => c.as_assoc_item(db),
5858
Definition::TypeAlias(ta) => ta.as_assoc_item(db),
5959
Definition::Function(f) => f.as_assoc_item(db),
60-
Definition::ExternCrateDecl(it) => return it.try_to_nav(db),
60+
Definition::ExternCrateDecl(it) => return it.try_to_nav(&sema),
6161
_ => None,
6262
}?;
6363

6464
let trait_ = assoc.implemented_trait(db)?;
6565
let name = Some(assoc.name(db)?);
6666
let item = trait_.items(db).into_iter().find(|it| it.name(db) == name)?;
67-
item.try_to_nav(db)
67+
item.try_to_nav(&sema)
6868
})
6969
.flatten()
7070
.collect();

crates/ide/src/goto_definition.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn goto_definition(
6262
})?;
6363
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
6464
return doc_comment.get_definition_with_descend_at(sema, offset, |def, _, link_range| {
65-
let nav = def.try_to_nav(db)?;
65+
let nav = def.try_to_nav(sema)?;
6666
Some(RangeInfo::new(link_range, nav.collect()))
6767
});
6868
}
@@ -73,7 +73,7 @@ pub(crate) fn goto_definition(
7373
return Some(RangeInfo::new(
7474
range,
7575
match resolution {
76-
Some(res) => def_to_nav(db, Definition::from(res)),
76+
Some(res) => def_to_nav(sema, Definition::from(res)),
7777
None => vec![],
7878
},
7979
));
@@ -121,7 +121,7 @@ pub(crate) fn goto_definition(
121121
.collect();
122122
}
123123
try_filter_trait_item_definition(sema, &def)
124-
.unwrap_or_else(|| def_to_nav(sema.db, def))
124+
.unwrap_or_else(|| def_to_nav(sema, def))
125125
})
126126
.collect(),
127127
)
@@ -160,7 +160,7 @@ fn find_definition_for_known_blanket_dual_impls(
160160
t_f,
161161
[return_type.type_arguments().next()?],
162162
)
163-
.map(|f| def_to_nav(sema.db, f.into()));
163+
.map(|f| def_to_nav(sema, f.into()));
164164
}
165165
hir::AssocItemContainer::Impl(_) => return None,
166166
};
@@ -201,7 +201,7 @@ fn find_definition_for_known_blanket_dual_impls(
201201
// succeed
202202
let _t = f.as_assoc_item(sema.db)?.implemented_trait(sema.db)?;
203203
let def = Definition::from(f);
204-
Some(def_to_nav(sema.db, def))
204+
Some(def_to_nav(sema, def))
205205
}
206206

207207
fn try_lookup_include_path(
@@ -246,7 +246,7 @@ fn try_lookup_macro_def_in_macro_use(
246246
for mod_def in krate.root_module().declarations(sema.db) {
247247
if let ModuleDef::Macro(mac) = mod_def
248248
&& mac.name(sema.db).as_str() == token.text()
249-
&& let Some(nav) = mac.try_to_nav(sema.db)
249+
&& let Some(nav) = mac.try_to_nav(sema)
250250
{
251251
return Some(nav.call_site);
252252
}
@@ -278,7 +278,7 @@ fn try_filter_trait_item_definition(
278278
.items(db)
279279
.iter()
280280
.filter(|itm| discriminant(*itm) == discriminant_value)
281-
.find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(db)).flatten())
281+
.find_map(|itm| (itm.name(db)? == name).then(|| itm.try_to_nav(sema)).flatten())
282282
.map(|it| it.collect())
283283
}
284284
}
@@ -347,7 +347,7 @@ fn nav_for_exit_points(
347347
match_ast! {
348348
match node {
349349
ast::Fn(fn_) => {
350-
let mut nav = sema.to_def(&fn_)?.try_to_nav(db)?;
350+
let mut nav = sema.to_def(&fn_)?.try_to_nav(sema)?;
351351
// For async token, we navigate to itself, which triggers
352352
// VSCode to find the references
353353
let focus_token = if matches!(token_kind, T![async]) {
@@ -564,8 +564,8 @@ fn nav_for_break_points(
564564
Some(navs)
565565
}
566566

567-
fn def_to_nav(db: &RootDatabase, def: Definition) -> Vec<NavigationTarget> {
568-
def.try_to_nav(db).map(|it| it.collect()).unwrap_or_default()
567+
fn def_to_nav(sema: &Semantics<'_, RootDatabase>, def: Definition) -> Vec<NavigationTarget> {
568+
def.try_to_nav(sema).map(|it| it.collect()).unwrap_or_default()
569569
}
570570

571571
fn expr_to_nav(

crates/ide/src/goto_implementation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn goto_implementation(
8686
fn impls_for_ty(sema: &Semantics<'_, RootDatabase>, ty: hir::Type<'_>) -> Vec<NavigationTarget> {
8787
Impl::all_for_type(sema.db, ty)
8888
.into_iter()
89-
.filter_map(|imp| imp.try_to_nav(sema.db))
89+
.filter_map(|imp| imp.try_to_nav(sema))
9090
.flatten()
9191
.collect()
9292
}
@@ -97,7 +97,7 @@ fn impls_for_trait(
9797
) -> Vec<NavigationTarget> {
9898
Impl::all_for_trait(sema.db, trait_)
9999
.into_iter()
100-
.filter_map(|imp| imp.try_to_nav(sema.db))
100+
.filter_map(|imp| imp.try_to_nav(sema))
101101
.flatten()
102102
.collect()
103103
}
@@ -114,7 +114,7 @@ fn impls_for_trait_item(
114114
let itm_name = itm.name(sema.db)?;
115115
(itm_name == fun_name).then_some(*itm)
116116
})?;
117-
item.try_to_nav(sema.db)
117+
item.try_to_nav(sema)
118118
})
119119
.flatten()
120120
.collect()

crates/ide/src/goto_type_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) fn goto_type_definition(
3030

3131
let mut res = Vec::new();
3232
let mut push = |def: Definition| {
33-
if let Some(navs) = def.try_to_nav(db) {
33+
if let Some(navs) = def.try_to_nav(&sema) {
3434
for nav in navs {
3535
if !res.contains(&nav) {
3636
res.push(nav);

crates/ide/src/highlight_related.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fn highlight_references(
277277
Definition::Module(module) => {
278278
NavigationTarget::from_module_to_decl(sema.db, module)
279279
}
280-
def => match def.try_to_nav(sema.db) {
280+
def => match def.try_to_nav(sema) {
281281
Some(it) => it,
282282
None => continue,
283283
},

crates/ide/src/hover.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ pub enum HoverAction {
8585

8686
impl HoverAction {
8787
fn goto_type_from_targets(
88-
db: &RootDatabase,
88+
sema: &Semantics<'_, RootDatabase>,
8989
targets: Vec<hir::ModuleDef>,
9090
edition: Edition,
9191
) -> Option<Self> {
92+
let db = sema.db;
9293
let targets = targets
9394
.into_iter()
9495
.filter_map(|it| {
@@ -99,7 +100,7 @@ impl HoverAction {
99100
it.name(db).map(|name| name.display(db, edition).to_string()),
100101
edition,
101102
),
102-
nav: it.try_to_nav(db)?.call_site(),
103+
nav: it.try_to_nav(sema)?.call_site(),
103104
})
104105
})
105106
.collect::<Vec<_>>();
@@ -467,10 +468,10 @@ pub(crate) fn hover_for_definition(
467468
HoverResult {
468469
markup: render::process_markup(sema.db, def, &markup, range_map, config),
469470
actions: [
470-
show_fn_references_action(sema.db, def),
471-
show_implementations_action(sema.db, def),
471+
show_fn_references_action(sema, def),
472+
show_implementations_action(sema, def),
472473
runnable_action(sema, def, file_id),
473-
goto_type_action_for_def(sema.db, def, &notable_traits, subst_types, edition),
474+
goto_type_action_for_def(sema, def, &notable_traits, subst_types, edition),
474475
]
475476
.into_iter()
476477
.flatten()
@@ -505,7 +506,10 @@ fn notable_traits<'db>(
505506
.collect::<Vec<_>>()
506507
}
507508

508-
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
509+
fn show_implementations_action(
510+
sema: &Semantics<'_, RootDatabase>,
511+
def: Definition,
512+
) -> Option<HoverAction> {
509513
fn to_action(nav_target: NavigationTarget) -> HoverAction {
510514
HoverAction::Implementation(FilePosition {
511515
file_id: nav_target.file_id,
@@ -515,19 +519,22 @@ fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<Hov
515519

516520
let adt = match def {
517521
Definition::Trait(it) => {
518-
return it.try_to_nav(db).map(UpmappingResult::call_site).map(to_action);
522+
return it.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action);
519523
}
520524
Definition::Adt(it) => Some(it),
521-
Definition::SelfType(it) => it.self_ty(db).as_adt(),
525+
Definition::SelfType(it) => it.self_ty(sema.db).as_adt(),
522526
_ => None,
523527
}?;
524-
adt.try_to_nav(db).map(UpmappingResult::call_site).map(to_action)
528+
adt.try_to_nav(sema).map(UpmappingResult::call_site).map(to_action)
525529
}
526530

527-
fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
531+
fn show_fn_references_action(
532+
sema: &Semantics<'_, RootDatabase>,
533+
def: Definition,
534+
) -> Option<HoverAction> {
528535
match def {
529536
Definition::Function(it) => {
530-
it.try_to_nav(db).map(UpmappingResult::call_site).map(|nav_target| {
537+
it.try_to_nav(sema).map(UpmappingResult::call_site).map(|nav_target| {
531538
HoverAction::Reference(FilePosition {
532539
file_id: nav_target.file_id,
533540
offset: nav_target.focus_or_full_range().start(),
@@ -560,12 +567,13 @@ fn runnable_action(
560567
}
561568

562569
fn goto_type_action_for_def(
563-
db: &RootDatabase,
570+
sema: &Semantics<'_, RootDatabase>,
564571
def: Definition,
565572
notable_traits: &[(hir::Trait, Vec<(Option<hir::Type<'_>>, hir::Name)>)],
566573
subst_types: Option<Vec<(hir::Symbol, hir::Type<'_>)>>,
567574
edition: Edition,
568575
) -> Option<HoverAction> {
576+
let db = sema.db;
569577
let mut targets: Vec<hir::ModuleDef> = Vec::new();
570578
let mut push_new_def = |item: hir::ModuleDef| {
571579
if !targets.contains(&item) {
@@ -612,7 +620,7 @@ fn goto_type_action_for_def(
612620
}
613621
}
614622

615-
HoverAction::goto_type_from_targets(db, targets, edition)
623+
HoverAction::goto_type_from_targets(sema, targets, edition)
616624
}
617625

618626
fn walk_and_push_ty(

crates/ide/src/hover/render.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub(super) fn try_expr(
128128
};
129129
walk_and_push_ty(sema.db, &inner_ty, &mut push_new_def);
130130
walk_and_push_ty(sema.db, &body_ty, &mut push_new_def);
131-
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
131+
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
132132
res.actions.push(actions);
133133
}
134134

@@ -210,7 +210,7 @@ pub(super) fn deref_expr(
210210
)
211211
.into()
212212
};
213-
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
213+
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
214214
res.actions.push(actions);
215215
}
216216

@@ -323,7 +323,7 @@ pub(super) fn struct_rest_pat(
323323

324324
Markup::fenced_block(&s)
325325
};
326-
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
326+
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
327327
res.actions.push(actions);
328328
}
329329
res
@@ -1030,7 +1030,7 @@ fn type_info(
10301030
};
10311031
desc.into()
10321032
};
1033-
if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) {
1033+
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
10341034
res.actions.push(actions);
10351035
}
10361036
Some(res)
@@ -1098,7 +1098,7 @@ fn closure_ty(
10981098
format_to!(markup, "{adjusted}\n\n## Captures\n{}", captures_rendered,);
10991099

11001100
let mut res = HoverResult::default();
1101-
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
1101+
if let Some(actions) = HoverAction::goto_type_from_targets(sema, targets, edition) {
11021102
res.actions.push(actions);
11031103
}
11041104
res.markup = markup.into();
@@ -1302,7 +1302,7 @@ fn keyword_hints(
13021302
KeywordHint {
13031303
description,
13041304
keyword_mod,
1305-
actions: HoverAction::goto_type_from_targets(sema.db, targets, edition)
1305+
actions: HoverAction::goto_type_from_targets(sema, targets, edition)
13061306
.into_iter()
13071307
.collect(),
13081308
}

crates/ide/src/inlay_hints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl fmt::Debug for InlayHintLabelPart {
672672

673673
#[derive(Debug)]
674674
struct InlayHintLabelBuilder<'a> {
675-
db: &'a RootDatabase,
675+
sema: &'a Semantics<'a, RootDatabase>,
676676
result: InlayHintLabel,
677677
last_part: String,
678678
resolve: bool,
@@ -694,7 +694,7 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
694694
LazyProperty::Lazy
695695
} else {
696696
LazyProperty::Computed({
697-
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
697+
let Some(location) = ModuleDef::from(def).try_to_nav(self.sema) else { return };
698698
let location = location.call_site();
699699
FileRange { file_id: location.file_id, range: location.focus_or_full_range() }
700700
})
@@ -782,7 +782,7 @@ fn label_of_ty(
782782
}
783783

784784
let mut label_builder = InlayHintLabelBuilder {
785-
db: sema.db,
785+
sema,
786786
last_part: String::new(),
787787
location: None,
788788
result: InlayHintLabel::default(),

0 commit comments

Comments
 (0)