Lazily compute location links in type hints again

This commit is contained in:
Lukas Wirth 2025-01-24 14:45:38 +01:00
parent f5b86e056b
commit 41e6a8747d
3 changed files with 18 additions and 9 deletions

View file

@ -650,7 +650,8 @@ struct InlayHintLabelBuilder<'a> {
db: &'a RootDatabase, db: &'a RootDatabase,
result: InlayHintLabel, result: InlayHintLabel,
last_part: String, last_part: String,
location: Option<FileRange>, resolve: bool,
location: Option<LazyProperty<FileRange>>,
} }
impl fmt::Write for InlayHintLabelBuilder<'_> { impl fmt::Write for InlayHintLabelBuilder<'_> {
@ -663,11 +664,16 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
fn start_location_link(&mut self, def: ModuleDefId) { fn start_location_link(&mut self, def: ModuleDefId) {
never!(self.location.is_some(), "location link is already started"); never!(self.location.is_some(), "location link is already started");
self.make_new_part(); self.make_new_part();
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
let location = location.call_site(); self.location = Some(if self.resolve {
let location = LazyProperty::Lazy
FileRange { file_id: location.file_id, range: location.focus_or_full_range() }; } else {
self.location = Some(location); LazyProperty::Computed({
let Some(location) = ModuleDef::from(def).try_to_nav(self.db) else { return };
let location = location.call_site();
FileRange { file_id: location.file_id, range: location.focus_or_full_range() }
})
});
} }
fn end_location_link(&mut self) { fn end_location_link(&mut self) {
@ -681,7 +687,7 @@ impl InlayHintLabelBuilder<'_> {
if !text.is_empty() { if !text.is_empty() {
self.result.parts.push(InlayHintLabelPart { self.result.parts.push(InlayHintLabelPart {
text, text,
linked_location: self.location.take().map(LazyProperty::Computed), linked_location: self.location.take(),
tooltip: None, tooltip: None,
}); });
} }
@ -753,6 +759,7 @@ fn label_of_ty(
last_part: String::new(), last_part: String::new(),
location: None, location: None,
result: InlayHintLabel::default(), result: InlayHintLabel::default(),
resolve: config.fields_to_resolve.resolve_label_location,
}; };
let _ = rec(sema, famous_defs, config.max_length, ty, &mut label_builder, config, edition); let _ = rec(sema, famous_defs, config.max_length, ty, &mut label_builder, config, edition);
let r = label_builder.finish(); let r = label_builder.finish();

View file

@ -12,7 +12,8 @@ use syntax::{
}; };
use crate::{ use crate::{
inlay_hints::LazyProperty, InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind, inlay_hints::LazyProperty, InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig,
InlayKind,
}; };
pub(super) fn hints( pub(super) fn hints(

View file

@ -91,7 +91,8 @@ pub use crate::{
inlay_hints::{ inlay_hints::{
AdjustmentHints, AdjustmentHintsMode, ClosureReturnTypeHints, DiscriminantHints, AdjustmentHints, AdjustmentHintsMode, ClosureReturnTypeHints, DiscriminantHints,
GenericParameterHints, InlayFieldsToResolve, InlayHint, InlayHintLabel, InlayHintLabelPart, GenericParameterHints, InlayFieldsToResolve, InlayHint, InlayHintLabel, InlayHintLabelPart,
InlayHintPosition, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, LazyProperty InlayHintPosition, InlayHintsConfig, InlayKind, InlayTooltip, LazyProperty,
LifetimeElisionHints,
}, },
join_lines::JoinLinesConfig, join_lines::JoinLinesConfig,
markup::Markup, markup::Markup,