Skip to content

Commit c5d3132

Browse files
committed
refactor: update tooltip resolution and improve truncation handling for inlay hints
1 parent 254f40b commit c5d3132

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

crates/hir-ty/src/display.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ use crate::{
6464
pub trait HirWrite: fmt::Write {
6565
fn start_location_link(&mut self, _location: ModuleDefId) {}
6666
fn end_location_link(&mut self) {}
67-
/// Returns whether a new truncated part is created during this call. If so,
68-
/// the caller should call `end_truncated` at the end. Otherwise, the caller
69-
/// should not call `end_truncated`. This ensures that the truncated part is
70-
/// not nested.
67+
/// Returns whether a new truncated part is created during this call. If so, the caller should
68+
/// call `end_truncated` at the end. This ensures that the truncated part is not nested, i.e.
69+
/// we should not use [`TYPE_HINT_TRUNCATION`] inside a tooltip (a truncated part).
7170
fn start_truncated(&mut self) -> bool {
7271
false
7372
}
@@ -155,10 +154,13 @@ impl HirFormatter<'_> {
155154
self.fmt.end_location_link();
156155
}
157156

158-
fn maybe_truncated<T, F: FnOnce(&mut Self) -> T>(&mut self, f: F) -> T {
159-
let truncated = self.should_truncate() && self.fmt.start_truncated();
160-
let res = f(self);
161-
if truncated {
157+
fn maybe_truncated<F: FnOnce(&mut Self) -> Result<(), HirDisplayError>>(
158+
&mut self,
159+
render_content_to: F,
160+
) -> Result<(), HirDisplayError> {
161+
let start_truncated_part = self.should_truncate() && self.fmt.start_truncated();
162+
let res = render_content_to(self);
163+
if start_truncated_part {
162164
self.fmt.end_truncated();
163165
}
164166
res

crates/ide/src/inlay_hints.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,8 @@ struct InlayHintLabelBuilder<'a> {
670670
db: &'a RootDatabase,
671671
result: InlayHintLabel,
672672
last_part: String,
673-
resolve: bool,
673+
resolve_location: bool,
674+
resolve_tooltip: bool,
674675
location: Option<LazyProperty<FileRange>>,
675676
tooltip: Option<LazyProperty<InlayTooltip>>,
676677
in_truncated_part: bool,
@@ -691,7 +692,7 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
691692
never!(self.location.is_some(), "location link is already started");
692693
self.make_new_part();
693694

694-
self.location = Some(if self.resolve {
695+
self.location = Some(if self.resolve_location {
695696
LazyProperty::Lazy
696697
} else {
697698
LazyProperty::Computed({
@@ -709,6 +710,8 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
709710
self.make_new_part();
710711
}
711712

713+
/// Returns whether a new truncated part is created during this call. If not, it indicates that
714+
/// the last part is a truncated part.
712715
fn start_truncated(&mut self) -> bool {
713716
never!(self.location.is_some(), "location link is already started");
714717
// If currently in the truncated part, do not create a new part and continue writing into
@@ -727,7 +730,7 @@ impl HirWrite for InlayHintLabelBuilder<'_> {
727730
always!(self.in_truncated_part, "truncated is not started");
728731

729732
const HINT_TRUNCATION: &str = "…";
730-
if self.resolve {
733+
if self.resolve_tooltip {
731734
self.last_part = HINT_TRUNCATION.to_owned();
732735
self.tooltip = Some(LazyProperty::Lazy);
733736
} else {
@@ -821,7 +824,8 @@ fn label_of_ty(
821824
tooltip: None,
822825
in_truncated_part: false,
823826
result: InlayHintLabel::default(),
824-
resolve: config.fields_to_resolve.resolve_label_location,
827+
resolve_location: config.fields_to_resolve.resolve_label_location,
828+
resolve_tooltip: config.fields_to_resolve.resolve_label_tooltip,
825829
};
826830
let _ =
827831
rec(sema, famous_defs, config.max_length, ty, &mut label_builder, config, display_target);
@@ -945,15 +949,8 @@ mod tests {
945949
lifetime_elision_hints: LifetimeElisionHints::Always,
946950
..DISABLED_CONFIG
947951
};
948-
pub(super) const TEST_CONFIG_WITH_TRUNCATION: InlayHintsConfig = InlayHintsConfig {
949-
type_hints: true,
950-
parameter_hints: true,
951-
chaining_hints: true,
952-
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
953-
binding_mode_hints: true,
954-
max_length: Some(10),
955-
..DISABLED_CONFIG
956-
};
952+
pub(super) const TEST_CONFIG_WITH_TRUNCATION: InlayHintsConfig =
953+
InlayHintsConfig { max_length: Some(10), ..TEST_CONFIG };
957954

958955
#[track_caller]
959956
pub(super) fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {

0 commit comments

Comments
 (0)