about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-01-16 13:11:16 +0000
committerbors <bors@rust-lang.org>2023-01-16 13:11:16 +0000
commit5306eb06cc2de0566ca789ee3863b78059ee2376 (patch)
tree1a4e58f74d2adf2b965c7febdb726e2282a57abb
parent27c9c2fcaf57907862419751e11dbb600bfc77de (diff)
parent81d7e302edcff20ba7ec8f3383f86cb513e0aceb (diff)
downloadrust-5306eb06cc2de0566ca789ee3863b78059ee2376.tar.gz
rust-5306eb06cc2de0566ca789ee3863b78059ee2376.zip
Auto merge of #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.
-rw-r--r--crates/ide/src/inlay_hints/adjustment.rs64
1 files changed, 43 insertions, 21 deletions
diff --git a/crates/ide/src/inlay_hints/adjustment.rs b/crates/ide/src/inlay_hints/adjustment.rs
index 581813f3b77..45e85a338a4 100644
--- a/crates/ide/src/inlay_hints/adjustment.rs
+++ b/crates/ide/src/inlay_hints/adjustment.rs
@@ -3,7 +3,7 @@
 //! let _: u32  = /* <never-to-any> */ loop {};
 //! let _: &u32 = /* &* */ &mut 0;
 //! ```
-use hir::{Adjust, AutoBorrow, Mutability, OverloadedDeref, PointerCast, Safety, Semantics};
+use hir::{Adjust, Adjustment, AutoBorrow, HirDisplay, Mutability, PointerCast, Safety, Semantics};
 use ide_db::RootDatabase;
 
 use syntax::{
@@ -11,7 +11,10 @@ use syntax::{
     ted,
 };
 
-use crate::{AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintsConfig, InlayKind};
+use crate::{
+    AdjustmentHints, AdjustmentHintsMode, InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind,
+    InlayTooltip,
+};
 
 pub(super) fn hints(
     acc: &mut Vec<InlayHint>,
@@ -61,36 +64,47 @@ pub(super) fn hints(
         &mut tmp1
     };
 
-    for adjustment in iter {
-        if adjustment.source == adjustment.target {
+    for Adjustment { source, target, kind } in iter {
+        if source == target {
             continue;
         }
 
         // FIXME: Add some nicer tooltips to each of these
-        let text = match adjustment.kind {
+        let (text, coercion) = match kind {
             Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
-                "<never-to-any>"
+                ("<never-to-any>", "never to any")
+            }
+            Adjust::Deref(_) => ("*", "dereference"),
+            Adjust::Borrow(AutoBorrow::Ref(Mutability::Shared)) => ("&", "borrow"),
+            Adjust::Borrow(AutoBorrow::Ref(Mutability::Mut)) => ("&mut ", "unique borrow"),
+            Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Shared)) => {
+                ("&raw const ", "const pointer borrow")
+            }
+            Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Mut)) => {
+                ("&raw mut ", "mut pointer borrow")
             }
-            Adjust::Deref(None) => "*",
-            Adjust::Deref(Some(OverloadedDeref(Mutability::Mut))) => "*",
-            Adjust::Deref(Some(OverloadedDeref(Mutability::Shared))) => "*",
-            Adjust::Borrow(AutoBorrow::Ref(Mutability::Shared)) => "&",
-            Adjust::Borrow(AutoBorrow::Ref(Mutability::Mut)) => "&mut ",
-            Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Shared)) => "&raw const ",
-            Adjust::Borrow(AutoBorrow::RawPtr(Mutability::Mut)) => "&raw mut ",
             // some of these could be represented via `as` casts, but that's not too nice and
             // handling everything as a prefix expr makes the `(` and `)` insertion easier
             Adjust::Pointer(cast) if config.adjustment_hints == AdjustmentHints::Always => {
                 match cast {
-                    PointerCast::ReifyFnPointer => "<fn-item-to-fn-pointer>",
-                    PointerCast::UnsafeFnPointer => "<safe-fn-pointer-to-unsafe-fn-pointer>",
+                    PointerCast::ReifyFnPointer => {
+                        ("<fn-item-to-fn-pointer>", "fn item to fn pointer")
+                    }
+                    PointerCast::UnsafeFnPointer => (
+                        "<safe-fn-pointer-to-unsafe-fn-pointer>",
+                        "safe fn pointer to unsafe fn pointer",
+                    ),
                     PointerCast::ClosureFnPointer(Safety::Unsafe) => {
-                        "<closure-to-unsafe-fn-pointer>"
+                        ("<closure-to-unsafe-fn-pointer>", "closure to unsafe fn pointer")
+                    }
+                    PointerCast::ClosureFnPointer(Safety::Safe) => {
+                        ("<closure-to-fn-pointer>", "closure to fn pointer")
+                    }
+                    PointerCast::MutToConstPointer => {
+                        ("<mut-ptr-to-const-ptr>", "mut ptr to const ptr")
                     }
-                    PointerCast::ClosureFnPointer(Safety::Safe) => "<closure-to-fn-pointer>",
-                    PointerCast::MutToConstPointer => "<mut-ptr-to-const-ptr>",
-                    PointerCast::ArrayToPointer => "<array-ptr-to-element-ptr>",
-                    PointerCast::Unsize => "<unsize>",
+                    PointerCast::ArrayToPointer => ("<array-ptr-to-element-ptr>", ""),
+                    PointerCast::Unsize => ("<unsize>", "unsize"),
                 }
             }
             _ => continue,
@@ -98,7 +112,15 @@ pub(super) fn hints(
         acc.push(InlayHint {
             range: expr.syntax().text_range(),
             kind: if postfix { InlayKind::AdjustmentPostfix } else { InlayKind::Adjustment },
-            label: if postfix { format!(".{}", text.trim_end()).into() } else { text.into() },
+            label: InlayHintLabel::simple(
+                if postfix { format!(".{}", text.trim_end()) } else { text.to_owned() },
+                Some(InlayTooltip::Markdown(format!(
+                    "`{}` → `{}` ({coercion} coercion)",
+                    source.display(sema.db),
+                    target.display(sema.db),
+                ))),
+                None,
+            ),
         });
     }
     if !postfix && needs_inner_parens {