Skip to content

feat: show full type in tooltips for hints #19640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions crates/ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,17 @@ pub enum InlayTooltip {
Markdown(String),
}

#[derive(Default, Hash)]
#[derive(Default)]
pub struct InlayHintLabel {
pub parts: SmallVec<[InlayHintLabelPart; 1]>,
pub tooltip: Option<LazyProperty<String>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed here, we should instead record the hover tooltip for the ... parts (to make that work, we probably need to add a function to HirWrite that is called when the truncation string gets emitted).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, should we concatenate the types at the end? 👀 I tried adding a tooltip only for ..., but it was quite inconvenient. Displaying the tooltip for the entire type directly is a better choice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature itself shouldn't require changes to our inlay hint structure here (and thus also no changes in to_proto). Associating a tooltip with all parts doesnt make sense when every part can track tooltips themselves.

I would expect that if HirWrite had a function like fn start_truncated(&mut self) (and end) that gets called when the formatting infra emits a ... we could have that implemented as:

    fn start_truncated(&mut self) {
        self.make_new_part();
        // render full type into the new part tooltip here / (lazily)
    }

    fn end_truncated(&mut self) {
        self.make_new_part();
    }

and probably needs InlayHintLabelBuilder to get the full type as an additional field or so

}

impl std::hash::Hash for InlayHintLabel {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.parts.hash(state);
self.tooltip.is_some().hash(state);
}
}

impl InlayHintLabel {
Expand All @@ -537,6 +545,7 @@ impl InlayHintLabel {
) -> InlayHintLabel {
InlayHintLabel {
parts: smallvec![InlayHintLabelPart { text: s.into(), linked_location, tooltip }],
tooltip: None,
}
}

Expand Down Expand Up @@ -582,6 +591,7 @@ impl From<String> for InlayHintLabel {
fn from(s: String) -> Self {
Self {
parts: smallvec![InlayHintLabelPart { text: s, linked_location: None, tooltip: None }],
tooltip: None,
}
}
}
Expand All @@ -592,8 +602,9 @@ impl From<&str> for InlayHintLabel {
parts: smallvec![InlayHintLabelPart {
text: s.into(),
linked_location: None,
tooltip: None
tooltip: None,
}],
tooltip: None,
}
}
}
Expand All @@ -606,7 +617,10 @@ impl fmt::Display for InlayHintLabel {

impl fmt::Debug for InlayHintLabel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(&self.parts).finish()
f.debug_struct("InlayHintLabel")
.field("parts", &self.parts)
.field("tooltip", &self.tooltip)
.finish()
}
}

Expand Down Expand Up @@ -703,6 +717,13 @@ impl InlayHintLabelBuilder<'_> {
}
}

fn write_to_label_and_tooltip(&mut self, s: &str) -> fmt::Result {
if let Some(LazyProperty::Computed(ref mut tooltip)) = self.result.tooltip {
tooltip.push_str(s);
}
self.write_str(s)
}

fn finish(mut self) -> InlayHintLabel {
self.make_new_part();
self.result
Expand Down Expand Up @@ -744,31 +765,44 @@ fn label_of_ty(
)
});

label_builder.write_str(LABEL_START)?;
label_builder.write_to_label_and_tooltip(LABEL_START)?;
label_builder.start_location_link(ModuleDef::from(iter_trait).into());
label_builder.write_str(LABEL_ITERATOR)?;
label_builder.write_to_label_and_tooltip(LABEL_ITERATOR)?;
label_builder.end_location_link();
label_builder.write_str(LABEL_MIDDLE)?;
label_builder.write_to_label_and_tooltip(LABEL_MIDDLE)?;
label_builder.start_location_link(ModuleDef::from(item).into());
label_builder.write_str(LABEL_ITEM)?;
label_builder.write_to_label_and_tooltip(LABEL_ITEM)?;
label_builder.end_location_link();
label_builder.write_str(LABEL_MIDDLE2)?;
label_builder.write_to_label_and_tooltip(LABEL_MIDDLE2)?;
rec(sema, famous_defs, max_length, &ty, label_builder, config, display_target)?;
label_builder.write_str(LABEL_END)?;
label_builder.write_to_label_and_tooltip(LABEL_END)?;
Ok(())
}
None => ty
.display_truncated(sema.db, max_length, display_target)
.with_closure_style(config.closure_style)
.write_to(label_builder),
None => {
if let Some(LazyProperty::Computed(ref mut tooltip)) = label_builder.result.tooltip
{
ty.display(sema.db, display_target)
.with_closure_style(config.closure_style)
.write_to(tooltip)?;
}

ty.display_truncated(sema.db, max_length, display_target)
.with_closure_style(config.closure_style)
.write_to(label_builder)
}
}
}

let tooltip = if config.fields_to_resolve.resolve_label_tooltip {
Some(LazyProperty::Lazy)
} else {
Some(LazyProperty::Computed(String::new()))
};
let mut label_builder = InlayHintLabelBuilder {
db: sema.db,
last_part: String::new(),
location: None,
result: InlayHintLabel::default(),
result: InlayHintLabel { tooltip, ..Default::default() },
resolve: config.fields_to_resolve.resolve_label_location,
};
let _ =
Expand Down Expand Up @@ -966,6 +1000,22 @@ mod tests {
assert!(edits.is_empty(), "unexpected edits: {edits:?}");
}

#[track_caller]
pub(super) fn check_tooltip(
config: InlayHintsConfig,
#[rust_analyzer::rust_fixture] ra_fixture: &str,
expect: Expect,
) {
let (analysis, file_id) = fixture::file(ra_fixture);
let inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();

let tooltips = inlay_hints
.into_iter()
.filter_map(|hint| hint.label.tooltip?.computed())
.collect::<Vec<_>>();
expect.assert_debug_eq(&tooltips);
}

#[test]
fn hints_disabled() {
check_with_config(
Expand Down
49 changes: 48 additions & 1 deletion crates/ide/src/inlay_hints/bind_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,23 @@ mod tests {
use crate::{ClosureReturnTypeHints, fixture, inlay_hints::InlayHintsConfig};

use crate::inlay_hints::tests::{
DISABLED_CONFIG, TEST_CONFIG, check, check_edit, check_no_edit, check_with_config,
DISABLED_CONFIG, TEST_CONFIG, check, check_edit, check_no_edit, check_tooltip,
check_with_config,
};

#[track_caller]
fn check_types(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_with_config(InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG }, ra_fixture);
}

#[track_caller]
fn check_types_tooltip(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
expect: expect_test::Expect,
) {
check_tooltip(InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG }, ra_fixture, expect);
}

#[test]
fn type_hints_only() {
check_types(
Expand Down Expand Up @@ -1256,4 +1265,42 @@ where
"#,
);
}

#[test]
fn tooltip_for_bind_pat() {
check_types_tooltip(
r#"
struct Struct<T, T2> {
a: T,
b: T2,
}

pub fn main() {
let x = Struct { a: Struct { a: Struct { a: 1, b: 2 }, b: 2 }, b: Struct { a: 1, b: 2 } };
}
"#,
expect![[r#"
[
"Struct<Struct<Struct<i32, i32>, i32>, Struct<i32, i32>>",
]
"#]],
);
}

#[test]
fn tooltip_for_bind_pat_with_type_alias() {
check_types_tooltip(
r#"
//- minicore: option
pub fn main() {
let x = Some(1);
}
"#,
expect![[r#"
[
"Option<i32>",
]
"#]],
);
}
}
35 changes: 19 additions & 16 deletions crates/ide/src/inlay_hints/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,26 @@ fn foo<T>() {}
[
(
7..8,
[
": ",
InlayHintLabelPart {
text: "Sized",
linked_location: Some(
Computed(
FileRangeWrapper {
file_id: FileId(
1,
),
range: 135..140,
},
InlayHintLabel {
parts: [
": ",
InlayHintLabelPart {
text: "Sized",
linked_location: Some(
Computed(
FileRangeWrapper {
file_id: FileId(
1,
),
range: 135..140,
},
),
),
),
tooltip: "",
},
],
tooltip: "",
},
],
tooltip: None,
},
),
]
"#]],
Expand Down
Loading