Add text edit to binding mode hints

This commit is contained in:
Lukas Wirth 2024-10-23 09:52:03 +02:00
parent f086fa9c02
commit fd17fa10a2
2 changed files with 38 additions and 36 deletions

View file

@ -410,19 +410,6 @@ impl InlayHint {
}
}
fn opening_paren_before(kind: InlayKind, range: TextRange) -> InlayHint {
InlayHint {
range,
kind,
label: InlayHintLabel::from("("),
text_edit: None,
position: InlayHintPosition::Before,
pad_left: false,
pad_right: false,
resolve_parent: None,
}
}
pub fn needs_resolve(&self) -> Option<TextRange> {
self.resolve_parent.filter(|_| self.text_edit.is_some() || self.label.needs_resolve())
}

View file

@ -9,6 +9,7 @@ use ide_db::famous_defs::FamousDefs;
use span::EditionedFileId;
use syntax::ast::{self, AstNode};
use text_edit::TextEditBuilder;
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
@ -61,33 +62,30 @@ pub(super) fn hints(
hint.label.append_str(r);
});
hint.pad_right = was_mut_last;
if !hint.label.parts.is_empty() {
acc.push(hint);
}
let acc_base = acc.len();
match pat {
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
let bm = sema.binding_mode_of_pat(pat)?;
let bm = match bm {
hir::BindingMode::Move => return None,
hir::BindingMode::Ref(Mutability::Mut) => "ref mut",
hir::BindingMode::Ref(Mutability::Shared) => "ref",
hir::BindingMode::Move => None,
hir::BindingMode::Ref(Mutability::Mut) => Some("ref mut"),
hir::BindingMode::Ref(Mutability::Shared) => Some("ref"),
};
acc.push(InlayHint {
range: pat.syntax().text_range(),
kind: InlayKind::BindingMode,
label: bm.into(),
text_edit: None,
position: InlayHintPosition::Before,
pad_left: false,
pad_right: true,
resolve_parent: Some(pat.syntax().text_range()),
});
if let Some(bm) = bm {
acc.push(InlayHint {
range: pat.syntax().text_range(),
kind: InlayKind::BindingMode,
label: bm.into(),
text_edit: None,
position: InlayHintPosition::Before,
pad_left: false,
pad_right: true,
resolve_parent: Some(pat.syntax().text_range()),
});
}
}
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {
acc.push(InlayHint::opening_paren_before(
InlayKind::BindingMode,
pat.syntax().text_range(),
));
hint.label.append_str("(");
acc.push(InlayHint::closing_paren_after(
InlayKind::BindingMode,
pat.syntax().text_range(),
@ -95,6 +93,24 @@ pub(super) fn hints(
}
_ => (),
}
if !hint.label.parts.is_empty() {
acc.push(hint);
}
if let hints @ [_, ..] = &mut acc[acc_base..] {
let mut edit = TextEditBuilder::default();
for h in &mut *hints {
edit.insert(
match h.position {
InlayHintPosition::Before => h.range.start(),
InlayHintPosition::After => h.range.end(),
},
h.label.parts.iter().map(|p| &*p.text).collect(),
);
}
let edit = edit.finish();
hints.iter_mut().for_each(|h| h.text_edit = Some(edit.clone()));
}
Some(())
}
@ -145,11 +161,10 @@ fn __(
}
match &(0,) {
(x,) | (x,) => (),
//^^^^^^^^^^^&
//^^^^^^^^^^^)
//^^^^^^^^^^^&(
//^ ref
//^ ref
//^^^^^^^^^^^(
//^^^^^^^^^^^)
((x,) | (x,)) => (),
//^^^^^^^^^^^^^&
//^ ref