Skip to content

Commit 7aad1a2

Browse files
committed
Merge closure capture inlay hints into one
1 parent 5482247 commit 7aad1a2

File tree

2 files changed

+41
-90
lines changed

2 files changed

+41
-90
lines changed

src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ impl InlayHintLabel {
475475
}
476476
}
477477

478+
pub fn append_part(&mut self, part: InlayHintLabelPart) {
479+
if part.linked_location.is_none() && part.tooltip.is_none() {
480+
if let Some(InlayHintLabelPart { text, linked_location: None, tooltip: None }) =
481+
self.parts.last_mut()
482+
{
483+
text.push_str(&part.text);
484+
return;
485+
}
486+
}
487+
self.parts.push(part);
488+
}
489+
478490
pub fn needs_resolve(&self) -> bool {
479491
self.parts.iter().any(|part| part.linked_location.is_some() || part.tooltip.is_some())
480492
}

src/tools/rust-analyzer/crates/ide/src/inlay_hints/closure_captures.rs

Lines changed: 29 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use stdx::{never, TupleExt};
77
use syntax::ast::{self, AstNode};
88
use text_edit::{TextRange, TextSize};
99

10-
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
10+
use crate::{
11+
InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition, InlayHintsConfig, InlayKind,
12+
};
1113

1214
pub(super) fn hints(
1315
acc: &mut Vec<InlayHint>,
@@ -27,34 +29,27 @@ pub(super) fn hints(
2729
return None;
2830
}
2931

30-
let move_kw_range = match closure.move_token() {
31-
Some(t) => t.text_range(),
32+
let (range, label) = match closure.move_token() {
33+
Some(t) => (t.text_range(), InlayHintLabel::default()),
3234
None => {
33-
let range = closure.syntax().first_token()?.prev_token()?.text_range();
34-
let range = TextRange::new(range.end() - TextSize::from(1), range.end());
35-
acc.push(InlayHint {
36-
range,
37-
kind: InlayKind::ClosureCapture,
38-
label: InlayHintLabel::from("move"),
39-
text_edit: None,
40-
position: InlayHintPosition::After,
41-
pad_left: false,
42-
pad_right: false,
43-
resolve_parent: Some(closure.syntax().text_range()),
44-
});
45-
range
35+
let prev_token = closure.syntax().first_token()?.prev_token()?.text_range();
36+
(
37+
TextRange::new(prev_token.end() - TextSize::from(1), prev_token.end()),
38+
InlayHintLabel::from("move"),
39+
)
4640
}
4741
};
48-
acc.push(InlayHint {
49-
range: move_kw_range,
42+
let mut hint = InlayHint {
43+
range,
5044
kind: InlayKind::ClosureCapture,
51-
label: InlayHintLabel::from("("),
45+
label,
5246
text_edit: None,
5347
position: InlayHintPosition::After,
5448
pad_left: false,
55-
pad_right: false,
56-
resolve_parent: None,
57-
});
49+
pad_right: true,
50+
resolve_parent: Some(closure.syntax().text_range()),
51+
};
52+
hint.label.append_str("(");
5853
let last = captures.len() - 1;
5954
for (idx, capture) in captures.into_iter().enumerate() {
6055
let local = capture.local();
@@ -76,48 +71,20 @@ pub(super) fn hints(
7671
if never!(label.is_empty()) {
7772
continue;
7873
}
79-
let label = InlayHintLabel::simple(
80-
label,
81-
None,
82-
source.name().and_then(|name| {
74+
hint.label.append_part(InlayHintLabelPart {
75+
text: label,
76+
linked_location: source.name().and_then(|name| {
8377
name.syntax().original_file_range_opt(sema.db).map(TupleExt::head).map(Into::into)
8478
}),
85-
);
86-
acc.push(InlayHint {
87-
range: move_kw_range,
88-
kind: InlayKind::ClosureCapture,
89-
label,
90-
text_edit: None,
91-
position: InlayHintPosition::After,
92-
pad_left: false,
93-
pad_right: false,
94-
resolve_parent: Some(closure.syntax().text_range()),
79+
tooltip: None,
9580
});
9681

9782
if idx != last {
98-
acc.push(InlayHint {
99-
range: move_kw_range,
100-
kind: InlayKind::ClosureCapture,
101-
label: InlayHintLabel::from(", "),
102-
text_edit: None,
103-
position: InlayHintPosition::After,
104-
pad_left: false,
105-
pad_right: false,
106-
resolve_parent: None,
107-
});
83+
hint.label.append_str(", ");
10884
}
10985
}
110-
acc.push(InlayHint {
111-
range: move_kw_range,
112-
kind: InlayKind::ClosureCapture,
113-
label: InlayHintLabel::from(")"),
114-
text_edit: None,
115-
position: InlayHintPosition::After,
116-
pad_left: false,
117-
pad_right: true,
118-
resolve_parent: None,
119-
});
120-
86+
hint.label.append_str(")");
87+
acc.push(hint);
12188
Some(())
12289
}
12390

@@ -147,51 +114,25 @@ fn main() {
147114
let mut baz = NonCopy;
148115
let qux = &mut NonCopy;
149116
|| {
150-
// ^ move
151-
// ^ (
152-
// ^ &foo
153-
// ^ , $
154-
// ^ bar
155-
// ^ , $
156-
// ^ baz
157-
// ^ , $
158-
// ^ qux
159-
// ^ )
117+
// ^ move(&foo, bar, baz, qux)
160118
foo;
161119
bar;
162120
baz;
163121
qux;
164122
};
165123
|| {
166-
// ^ move
167-
// ^ (
168-
// ^ &foo
169-
// ^ , $
170-
// ^ &bar
171-
// ^ , $
172-
// ^ &baz
173-
// ^ , $
174-
// ^ &qux
175-
// ^ )
124+
// ^ move(&foo, &bar, &baz, &qux)
176125
&foo;
177126
&bar;
178127
&baz;
179128
&qux;
180129
};
181130
|| {
182-
// ^ move
183-
// ^ (
184-
// ^ &mut baz
185-
// ^ )
131+
// ^ move(&mut baz)
186132
&mut baz;
187133
};
188134
|| {
189-
// ^ move
190-
// ^ (
191-
// ^ &mut baz
192-
// ^ , $
193-
// ^ &mut *qux
194-
// ^ )
135+
// ^ move(&mut baz, &mut *qux)
195136
baz = NonCopy;
196137
*qux = NonCopy;
197138
};
@@ -209,9 +150,7 @@ fn main() {
209150
fn main() {
210151
let foo = u32;
211152
move || {
212-
// ^^^^ (
213-
// ^^^^ foo
214-
// ^^^^ )
153+
// ^^^^ (foo)
215154
foo;
216155
};
217156
}

0 commit comments

Comments
 (0)