Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5306eb0

Browse files
committed
Auto merge of rust-lang#13947 - WaffleLapkin:adjustment_hint_tooltips, r=Veykril
Add basic tooltips to adjustment hints ![2023-01-16_16-45](https://user-images.githubusercontent.com/38225716/212681383-a60b60bb-a8e7-410d-8b24-f6b72c197311.png) I'm not sure how to make them look nicer, but it's at least something.
2 parents 27c9c2f + 81d7e30 commit 5306eb0

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

crates/ide/src/inlay_hints/adjustment.rs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
//! let _: u32 = /* <never-to-any> */ loop {};
44
//! let _: &u32 = /* &* */ &mut 0;
55
//! ```
6-
use hir::{Adjust, AutoBorrow, Mutability, OverloadedDeref, PointerCast, Safety, Semantics};
6+
use hir::{Adjust, Adjustment, AutoBorrow, HirDisplay, Mutability, PointerCast, Safety, Semantics};
77
use ide_db::RootDatabase;
88

99
use syntax::{
1010
ast::{self, make, AstNode},
1111
ted,
1212
};
1313

14-
use crate::{AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintsConfig, InlayKind};
14+
use crate::{
15+
AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind,
16+
InlayTooltip,
17+
};
1518

1619
pub(super) fn hints(
1720
acc: &mut Vec<InlayHint>,
@@ -61,44 +64,63 @@ pub(super) fn hints(
6164
&mut tmp1
6265
};
6366

64-
for adjustment in iter {
65-
if adjustment.source == adjustment.target {
67+
for Adjustment { source, target, kind } in iter {
68+
if source == target {
6669
continue;
6770
}
6871

6972
// FIXME: Add some nicer tooltips to each of these
70-
let text = match adjustment.kind {
73+
let (text, coercion) = match kind {
7174
Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
72-
"<never-to-any>"
75+
("<never-to-any>", "never to any")
76+
}
77+
Adjust::Deref(_) => ("*", "dereference"),
78+
Adjust::Borrow(AutoBorrow::Ref(Mutability::Shared)) => ("&", "borrow"),
79+
Adjust::Borrow(AutoBorrow::Ref(Mutability::Mut)) => ("&mut ", "unique borrow"),
80+
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Shared)) => {
81+
("&raw const ", "const pointer borrow")
82+
}
83+
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Mut)) => {
84+
("&raw mut ", "mut pointer borrow")
7385
}
74-
Adjust::Deref(None) => "*",
75-
Adjust::Deref(Some(OverloadedDeref(Mutability::Mut))) => "*",
76-
Adjust::Deref(Some(OverloadedDeref(Mutability::Shared))) => "*",
77-
Adjust::Borrow(AutoBorrow::Ref(Mutability::Shared)) => "&",
78-
Adjust::Borrow(AutoBorrow::Ref(Mutability::Mut)) => "&mut ",
79-
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Shared)) => "&raw const ",
80-
Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Mut)) => "&raw mut ",
8186
// some of these could be represented via `as` casts, but that's not too nice and
8287
// handling everything as a prefix expr makes the `(` and `)` insertion easier
8388
Adjust::Pointer(cast) if config.adjustment_hints == AdjustmentHints::Always => {
8489
match cast {
85-
PointerCast::ReifyFnPointer => "<fn-item-to-fn-pointer>",
86-
PointerCast::UnsafeFnPointer => "<safe-fn-pointer-to-unsafe-fn-pointer>",
90+
PointerCast::ReifyFnPointer => {
91+
("<fn-item-to-fn-pointer>", "fn item to fn pointer")
92+
}
93+
PointerCast::UnsafeFnPointer => (
94+
"<safe-fn-pointer-to-unsafe-fn-pointer>",
95+
"safe fn pointer to unsafe fn pointer",
96+
),
8797
PointerCast::ClosureFnPointer(Safety::Unsafe) => {
88-
"<closure-to-unsafe-fn-pointer>"
98+
("<closure-to-unsafe-fn-pointer>", "closure to unsafe fn pointer")
99+
}
100+
PointerCast::ClosureFnPointer(Safety::Safe) => {
101+
("<closure-to-fn-pointer>", "closure to fn pointer")
102+
}
103+
PointerCast::MutToConstPointer => {
104+
("<mut-ptr-to-const-ptr>", "mut ptr to const ptr")
89105
}
90-
PointerCast::ClosureFnPointer(Safety::Safe) => "<closure-to-fn-pointer>",
91-
PointerCast::MutToConstPointer => "<mut-ptr-to-const-ptr>",
92-
PointerCast::ArrayToPointer => "<array-ptr-to-element-ptr>",
93-
PointerCast::Unsize => "<unsize>",
106+
PointerCast::ArrayToPointer => ("<array-ptr-to-element-ptr>", ""),
107+
PointerCast::Unsize => ("<unsize>", "unsize"),
94108
}
95109
}
96110
_ => continue,
97111
};
98112
acc.push(InlayHint {
99113
range: expr.syntax().text_range(),
100114
kind: if postfix { InlayKind::AdjustmentPostfix } else { InlayKind::Adjustment },
101-
label: if postfix { format!(".{}", text.trim_end()).into() } else { text.into() },
115+
label: InlayHintLabel::simple(
116+
if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
117+
Some(InlayTooltip::Markdown(format!(
118+
"`{}` → `{}` ({coercion} coercion)",
119+
source.display(sema.db),
120+
target.display(sema.db),
121+
))),
122+
None,
123+
),
102124
});
103125
}
104126
if !postfix && needs_inner_parens {

0 commit comments

Comments
 (0)