Skip to content

Commit 284e0cd

Browse files
committed
Make InlayHint::linked_location computation lazy
1 parent f0f7204 commit 284e0cd

File tree

9 files changed

+285
-204
lines changed

9 files changed

+285
-204
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub struct InlayHintsConfig {
300300
pub closing_brace_hints_min_lines: Option<usize>,
301301
pub fields_to_resolve: InlayFieldsToResolve,
302302
}
303+
303304
impl InlayHintsConfig {
304305
fn lazy_text_edit(&self, finish: impl FnOnce() -> TextEdit) -> Lazy<TextEdit> {
305306
if self.fields_to_resolve.resolve_text_edits {
@@ -329,6 +330,19 @@ impl InlayHintsConfig {
329330
Lazy::Computed(tooltip)
330331
}
331332
}
333+
334+
/// This always reports a resolvable location, so only use this when it is very likely for a
335+
/// location link to actually resolve but where computing `finish` would be costly.
336+
fn lazy_location_opt(
337+
&self,
338+
finish: impl FnOnce() -> Option<FileRange>,
339+
) -> Option<Lazy<FileRange>> {
340+
if self.fields_to_resolve.resolve_label_location {
341+
Some(Lazy::Lazy)
342+
} else {
343+
finish().map(Lazy::Computed)
344+
}
345+
}
332346
}
333347

334348
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -509,7 +523,7 @@ impl InlayHintLabel {
509523
pub fn simple(
510524
s: impl Into<String>,
511525
tooltip: Option<Lazy<InlayTooltip>>,
512-
linked_location: Option<FileRange>,
526+
linked_location: Option<Lazy<FileRange>>,
513527
) -> InlayHintLabel {
514528
InlayHintLabel {
515529
parts: smallvec![InlayHintLabelPart { text: s.into(), linked_location, tooltip }],
@@ -593,7 +607,7 @@ pub struct InlayHintLabelPart {
593607
/// refers to (not necessarily the location itself).
594608
/// When setting this, no tooltip must be set on the containing hint, or VS Code will display
595609
/// them both.
596-
pub linked_location: Option<FileRange>,
610+
pub linked_location: Option<Lazy<FileRange>>,
597611
/// The tooltip to show when hovering over the inlay hint, this may invoke other actions like
598612
/// hover requests to show.
599613
pub tooltip: Option<Lazy<InlayTooltip>>,
@@ -602,7 +616,7 @@ pub struct InlayHintLabelPart {
602616
impl std::hash::Hash for InlayHintLabelPart {
603617
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
604618
self.text.hash(state);
605-
self.linked_location.hash(state);
619+
self.linked_location.is_some().hash(state);
606620
self.tooltip.is_some().hash(state);
607621
}
608622
}
@@ -663,7 +677,7 @@ impl InlayHintLabelBuilder<'_> {
663677
if !text.is_empty() {
664678
self.result.parts.push(InlayHintLabelPart {
665679
text,
666-
linked_location: self.location.take(),
680+
linked_location: self.location.take().map(Lazy::Computed),
667681
tooltip: None,
668682
});
669683
}

src/tools/rust-analyzer/crates/ide/src/inlay_hints/bounds.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ pub(super) fn hints(
2222
return None;
2323
}
2424

25-
let linked_location =
26-
famous_defs.core_marker_Sized().and_then(|it| it.try_to_nav(sema.db)).map(|it| {
27-
let n = it.call_site();
28-
FileRange { file_id: n.file_id, range: n.focus_or_full_range() }
29-
});
25+
let sized_trait = famous_defs.core_marker_Sized();
3026

3127
for param in params.type_or_const_params() {
3228
match param {
@@ -48,7 +44,17 @@ pub(super) fn hints(
4844
}
4945
hint.parts.push(InlayHintLabelPart {
5046
text: "Sized".to_owned(),
51-
linked_location,
47+
linked_location: sized_trait.and_then(|it| {
48+
config.lazy_location_opt(|| {
49+
it.try_to_nav(sema.db).map(|it| {
50+
let n = it.call_site();
51+
FileRange {
52+
file_id: n.file_id,
53+
range: n.focus_or_full_range(),
54+
}
55+
})
56+
})
57+
}),
5258
tooltip: None,
5359
});
5460
if has_bounds {
@@ -134,12 +140,14 @@ fn foo<T>() {}
134140
InlayHintLabelPart {
135141
text: "Sized",
136142
linked_location: Some(
137-
FileRangeWrapper {
138-
file_id: FileId(
139-
1,
140-
),
141-
range: 135..140,
142-
},
143+
Computed(
144+
FileRangeWrapper {
145+
file_id: FileId(
146+
1,
147+
),
148+
range: 135..140,
149+
},
150+
),
143151
),
144152
tooltip: "",
145153
},

0 commit comments

Comments
 (0)