Merge closure capture inlay hints into one

This commit is contained in:
Lukas Wirth 2024-10-22 16:27:57 +02:00
parent 1b00f0857b
commit 13acfbfae4
2 changed files with 41 additions and 90 deletions

View file

@ -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())
}

View file

@ -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;
};
}