Skip to content

Commit 8364ef2

Browse files
authored
Merge pull request #18923 from Veykril/push-oovkowowotqx
internal: Compute inlay hint tooltips lazily
2 parents 69ab0cf + f66bbbf commit 8364ef2

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

crates/ide/src/inlay_hints.rs

+32-5
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,25 @@ impl InlayHintsConfig {
307307
Lazy::Computed(edit)
308308
}
309309
}
310+
311+
fn lazy_tooltip(&self, finish: impl FnOnce() -> InlayTooltip) -> Lazy<InlayTooltip> {
312+
if self.fields_to_resolve.resolve_hint_tooltip
313+
&& self.fields_to_resolve.resolve_label_tooltip
314+
{
315+
Lazy::Lazy
316+
} else {
317+
let tooltip = finish();
318+
never!(
319+
match &tooltip {
320+
InlayTooltip::String(s) => s,
321+
InlayTooltip::Markdown(s) => s,
322+
}
323+
.is_empty(),
324+
"inlay hint produced an empty tooltip"
325+
);
326+
Lazy::Computed(tooltip)
327+
}
328+
}
310329
}
311330

312331
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -486,7 +505,7 @@ pub struct InlayHintLabel {
486505
impl InlayHintLabel {
487506
pub fn simple(
488507
s: impl Into<String>,
489-
tooltip: Option<InlayTooltip>,
508+
tooltip: Option<Lazy<InlayTooltip>>,
490509
linked_location: Option<FileRange>,
491510
) -> InlayHintLabel {
492511
InlayHintLabel {
@@ -564,7 +583,6 @@ impl fmt::Debug for InlayHintLabel {
564583
}
565584
}
566585

567-
#[derive(Hash)]
568586
pub struct InlayHintLabelPart {
569587
pub text: String,
570588
/// Source location represented by this label part. The client will use this to fetch the part's
@@ -575,21 +593,30 @@ pub struct InlayHintLabelPart {
575593
pub linked_location: Option<FileRange>,
576594
/// The tooltip to show when hovering over the inlay hint, this may invoke other actions like
577595
/// hover requests to show.
578-
pub tooltip: Option<InlayTooltip>,
596+
pub tooltip: Option<Lazy<InlayTooltip>>,
597+
}
598+
599+
impl std::hash::Hash for InlayHintLabelPart {
600+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
601+
self.text.hash(state);
602+
self.linked_location.hash(state);
603+
self.tooltip.is_some().hash(state);
604+
}
579605
}
580606

581607
impl fmt::Debug for InlayHintLabelPart {
582608
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
583609
match self {
584-
Self { text, linked_location: None, tooltip: None } => text.fmt(f),
610+
Self { text, linked_location: None, tooltip: None | Some(Lazy::Lazy) } => text.fmt(f),
585611
Self { text, linked_location, tooltip } => f
586612
.debug_struct("InlayHintLabelPart")
587613
.field("text", text)
588614
.field("linked_location", linked_location)
589615
.field(
590616
"tooltip",
591617
&tooltip.as_ref().map_or("", |it| match it {
592-
InlayTooltip::String(it) | InlayTooltip::Markdown(it) => it,
618+
Lazy::Computed(InlayTooltip::String(it) | InlayTooltip::Markdown(it)) => it,
619+
Lazy::Lazy => "",
593620
}),
594621
)
595622
.finish(),

crates/ide/src/inlay_hints/adjustment.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ pub(super) fn hints(
162162
let label = InlayHintLabelPart {
163163
text: if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
164164
linked_location: None,
165-
tooltip: Some(InlayTooltip::Markdown(format!(
166-
"`{}` → `{}` ({coercion} coercion)",
167-
source.display(sema.db, file_id.edition()),
168-
target.display(sema.db, file_id.edition()),
169-
))),
165+
tooltip: Some(config.lazy_tooltip(|| {
166+
InlayTooltip::Markdown(format!(
167+
"`{}` → `{}` ({coercion} coercion)",
168+
source.display(sema.db, file_id.edition()),
169+
target.display(sema.db, file_id.edition()),
170+
))
171+
})),
170172
};
171173
if postfix { &mut post } else { &mut pre }.label.append_part(label);
172174
}

crates/ide/src/inlay_hints/discriminant.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ fn variant_hints(
7676
}
7777
Err(_) => format!("{eq_} ?"),
7878
},
79-
Some(InlayTooltip::String(match &d {
80-
Ok(_) => "enum variant discriminant".into(),
81-
Err(e) => format!("{e:?}"),
79+
Some(config.lazy_tooltip(|| {
80+
InlayTooltip::String(match &d {
81+
Ok(_) => "enum variant discriminant".into(),
82+
Err(e) => format!("{e:?}"),
83+
})
8284
})),
8385
None,
8486
);

crates/ide/src/inlay_hints/implicit_drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub(super) fn hints(
108108
}
109109
let mut label = InlayHintLabel::simple(
110110
name,
111-
Some(crate::InlayTooltip::String("moz".into())),
111+
Some(config.lazy_tooltip(|| crate::InlayTooltip::String("moz".into()))),
112112
binding_source,
113113
);
114114
label.prepend_str("drop(");

crates/rust-analyzer/src/lsp/to_proto.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ fn inlay_hint_label(
641641
*something_to_resolve |= tooltip.is_some();
642642
None
643643
} else {
644-
match tooltip {
644+
match tooltip.and_then(|it| it.computed()) {
645645
Some(ide::InlayTooltip::String(s)) => {
646646
Some(lsp_types::InlayHintTooltip::String(s))
647647
}
@@ -665,7 +665,7 @@ fn inlay_hint_label(
665665
*something_to_resolve |= part.tooltip.is_some();
666666
None
667667
} else {
668-
match part.tooltip {
668+
match part.tooltip.and_then(|it| it.computed()) {
669669
Some(ide::InlayTooltip::String(s)) => {
670670
Some(lsp_types::InlayHintLabelPartTooltip::String(s))
671671
}

0 commit comments

Comments
 (0)