about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-10-22 16:27:57 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-10-22 16:29:15 +0200
commit7aad1a2c4e16155435b3e545aec661c4279f7831 (patch)
treefcbaa0c7fc85a1acc0512caa2d0b312c3a8a5642
parent548224782c7b4cc8409d037462b4d0e758a9389b (diff)
downloadrust-7aad1a2c4e16155435b3e545aec661c4279f7831.tar.gz
rust-7aad1a2c4e16155435b3e545aec661c4279f7831.zip
Merge closure capture inlay hints into one
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs12
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs119
2 files changed, 41 insertions, 90 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs
index 97e712356b5..fcf262877dd 100644
--- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs
@@ -475,6 +475,18 @@ impl InlayHintLabel {
         }
     }
 
+    pub fn append_part(&mut self, part: InlayHintLabelPart) {
+        if part.linked_location.is_none() && part.tooltip.is_none() {
+            if let Some(InlayHintLabelPart { text, linked_location: None, tooltip: None }) =
+                self.parts.last_mut()
+            {
+                text.push_str(&part.text);
+                return;
+            }
+        }
+        self.parts.push(part);
+    }
+
     pub fn needs_resolve(&self) -> bool {
         self.parts.iter().any(|part| part.linked_location.is_some() || part.tooltip.is_some())
     }
diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs
index f399bd01d07..628ddc6154c 100644
--- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs
@@ -7,7 +7,9 @@ use stdx::{never, TupleExt};
 use syntax::ast::{self, AstNode};
 use text_edit::{TextRange, TextSize};
 
-use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
+use crate::{
+    InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition, InlayHintsConfig, InlayKind,
+};
 
 pub(super) fn hints(
     acc: &mut Vec<InlayHint>,
@@ -27,34 +29,27 @@ pub(super) fn hints(
         return None;
     }
 
-    let move_kw_range = match closure.move_token() {
-        Some(t) => t.text_range(),
+    let (range, label) = match closure.move_token() {
+        Some(t) => (t.text_range(), InlayHintLabel::default()),
         None => {
-            let range = closure.syntax().first_token()?.prev_token()?.text_range();
-            let range = TextRange::new(range.end() - TextSize::from(1), range.end());
-            acc.push(InlayHint {
-                range,
-                kind: InlayKind::ClosureCapture,
-                label: InlayHintLabel::from("move"),
-                text_edit: None,
-                position: InlayHintPosition::After,
-                pad_left: false,
-                pad_right: false,
-                resolve_parent: Some(closure.syntax().text_range()),
-            });
-            range
+            let prev_token = closure.syntax().first_token()?.prev_token()?.text_range();
+            (
+                TextRange::new(prev_token.end() - TextSize::from(1), prev_token.end()),
+                InlayHintLabel::from("move"),
+            )
         }
     };
-    acc.push(InlayHint {
-        range: move_kw_range,
+    let mut hint = InlayHint {
+        range,
         kind: InlayKind::ClosureCapture,
-        label: InlayHintLabel::from("("),
+        label,
         text_edit: None,
         position: InlayHintPosition::After,
         pad_left: false,
-        pad_right: false,
-        resolve_parent: None,
-    });
+        pad_right: true,
+        resolve_parent: Some(closure.syntax().text_range()),
+    };
+    hint.label.append_str("(");
     let last = captures.len() - 1;
     for (idx, capture) in captures.into_iter().enumerate() {
         let local = capture.local();
@@ -76,48 +71,20 @@ pub(super) fn hints(
         if never!(label.is_empty()) {
             continue;
         }
-        let label = InlayHintLabel::simple(
-            label,
-            None,
-            source.name().and_then(|name| {
+        hint.label.append_part(InlayHintLabelPart {
+            text: label,
+            linked_location: source.name().and_then(|name| {
                 name.syntax().original_file_range_opt(sema.db).map(TupleExt::head).map(Into::into)
             }),
-        );
-        acc.push(InlayHint {
-            range: move_kw_range,
-            kind: InlayKind::ClosureCapture,
-            label,
-            text_edit: None,
-            position: InlayHintPosition::After,
-            pad_left: false,
-            pad_right: false,
-            resolve_parent: Some(closure.syntax().text_range()),
+            tooltip: None,
         });
 
         if idx != last {
-            acc.push(InlayHint {
-                range: move_kw_range,
-                kind: InlayKind::ClosureCapture,
-                label: InlayHintLabel::from(", "),
-                text_edit: None,
-                position: InlayHintPosition::After,
-                pad_left: false,
-                pad_right: false,
-                resolve_parent: None,
-            });
+            hint.label.append_str(", ");
         }
     }
-    acc.push(InlayHint {
-        range: move_kw_range,
-        kind: InlayKind::ClosureCapture,
-        label: InlayHintLabel::from(")"),
-        text_edit: None,
-        position: InlayHintPosition::After,
-        pad_left: false,
-        pad_right: true,
-        resolve_parent: None,
-    });
-
+    hint.label.append_str(")");
+    acc.push(hint);
     Some(())
 }
 
@@ -147,51 +114,25 @@ fn main() {
     let mut baz = NonCopy;
     let qux = &mut NonCopy;
     || {
-// ^ move
-// ^ (
-// ^ &foo
-// ^ , $
-// ^ bar
-// ^ , $
-// ^ baz
-// ^ , $
-// ^ qux
-// ^ )
+// ^ move(&foo, bar, baz, qux)
         foo;
         bar;
         baz;
         qux;
     };
     || {
-// ^ move
-// ^ (
-// ^ &foo
-// ^ , $
-// ^ &bar
-// ^ , $
-// ^ &baz
-// ^ , $
-// ^ &qux
-// ^ )
+// ^ move(&foo, &bar, &baz, &qux)
         &foo;
         &bar;
         &baz;
         &qux;
     };
     || {
-// ^ move
-// ^ (
-// ^ &mut baz
-// ^ )
+// ^ move(&mut baz)
         &mut baz;
     };
     || {
-// ^ move
-// ^ (
-// ^ &mut baz
-// ^ , $
-// ^ &mut *qux
-// ^ )
+// ^ move(&mut baz, &mut *qux)
         baz = NonCopy;
         *qux = NonCopy;
     };
@@ -209,9 +150,7 @@ fn main() {
 fn main() {
     let foo = u32;
     move || {
-//  ^^^^ (
-//  ^^^^ foo
-//  ^^^^ )
+//  ^^^^ (foo)
         foo;
     };
 }