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 { pub fn needs_resolve(&self) -> bool {
self.parts.iter().any(|part| part.linked_location.is_some() || part.tooltip.is_some()) 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 syntax::ast::{self, AstNode};
use text_edit::{TextRange, TextSize}; use text_edit::{TextRange, TextSize};
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind}; use crate::{
InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition, InlayHintsConfig, InlayKind,
};
pub(super) fn hints( pub(super) fn hints(
acc: &mut Vec<InlayHint>, acc: &mut Vec<InlayHint>,
@ -27,34 +29,27 @@ pub(super) fn hints(
return None; return None;
} }
let move_kw_range = match closure.move_token() { let (range, label) = match closure.move_token() {
Some(t) => t.text_range(), Some(t) => (t.text_range(), InlayHintLabel::default()),
None => { None => {
let range = closure.syntax().first_token()?.prev_token()?.text_range(); let prev_token = closure.syntax().first_token()?.prev_token()?.text_range();
let range = TextRange::new(range.end() - TextSize::from(1), range.end()); (
acc.push(InlayHint { TextRange::new(prev_token.end() - TextSize::from(1), prev_token.end()),
range, InlayHintLabel::from("move"),
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
} }
}; };
acc.push(InlayHint { let mut hint = InlayHint {
range: move_kw_range, range,
kind: InlayKind::ClosureCapture, kind: InlayKind::ClosureCapture,
label: InlayHintLabel::from("("), label,
text_edit: None, text_edit: None,
position: InlayHintPosition::After, position: InlayHintPosition::After,
pad_left: false, pad_left: false,
pad_right: false, pad_right: true,
resolve_parent: None, resolve_parent: Some(closure.syntax().text_range()),
}); };
hint.label.append_str("(");
let last = captures.len() - 1; let last = captures.len() - 1;
for (idx, capture) in captures.into_iter().enumerate() { for (idx, capture) in captures.into_iter().enumerate() {
let local = capture.local(); let local = capture.local();
@ -76,48 +71,20 @@ pub(super) fn hints(
if never!(label.is_empty()) { if never!(label.is_empty()) {
continue; continue;
} }
let label = InlayHintLabel::simple( hint.label.append_part(InlayHintLabelPart {
label, text: label,
None, linked_location: source.name().and_then(|name| {
source.name().and_then(|name| {
name.syntax().original_file_range_opt(sema.db).map(TupleExt::head).map(Into::into) name.syntax().original_file_range_opt(sema.db).map(TupleExt::head).map(Into::into)
}), }),
); tooltip: None,
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()),
}); });
if idx != last { if idx != last {
acc.push(InlayHint { hint.label.append_str(", ");
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,
});
} }
} }
acc.push(InlayHint { hint.label.append_str(")");
range: move_kw_range, acc.push(hint);
kind: InlayKind::ClosureCapture,
label: InlayHintLabel::from(")"),
text_edit: None,
position: InlayHintPosition::After,
pad_left: false,
pad_right: true,
resolve_parent: None,
});
Some(()) Some(())
} }
@ -147,51 +114,25 @@ fn main() {
let mut baz = NonCopy; let mut baz = NonCopy;
let qux = &mut NonCopy; let qux = &mut NonCopy;
|| { || {
// ^ move // ^ move(&foo, bar, baz, qux)
// ^ (
// ^ &foo
// ^ , $
// ^ bar
// ^ , $
// ^ baz
// ^ , $
// ^ qux
// ^ )
foo; foo;
bar; bar;
baz; baz;
qux; qux;
}; };
|| { || {
// ^ move // ^ move(&foo, &bar, &baz, &qux)
// ^ (
// ^ &foo
// ^ , $
// ^ &bar
// ^ , $
// ^ &baz
// ^ , $
// ^ &qux
// ^ )
&foo; &foo;
&bar; &bar;
&baz; &baz;
&qux; &qux;
}; };
|| { || {
// ^ move // ^ move(&mut baz)
// ^ (
// ^ &mut baz
// ^ )
&mut baz; &mut baz;
}; };
|| { || {
// ^ move // ^ move(&mut baz, &mut *qux)
// ^ (
// ^ &mut baz
// ^ , $
// ^ &mut *qux
// ^ )
baz = NonCopy; baz = NonCopy;
*qux = NonCopy; *qux = NonCopy;
}; };
@ -209,9 +150,7 @@ fn main() {
fn main() { fn main() {
let foo = u32; let foo = u32;
move || { move || {
// ^^^^ ( // ^^^^ (foo)
// ^^^^ foo
// ^^^^ )
foo; foo;
}; };
} }