Unnamed repository; edit this file 'description' to name the repository.
Add text edit to binding mode hints
Lukas Wirth 2024-10-23
parent f086fa9 · commit fd17fa1
-rw-r--r--crates/ide/src/inlay_hints.rs13
-rw-r--r--crates/ide/src/inlay_hints/binding_mode.rs61
2 files changed, 38 insertions, 36 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index fcf262877d..b6e46c3202 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -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())
}
diff --git a/crates/ide/src/inlay_hints/binding_mode.rs b/crates/ide/src/inlay_hints/binding_mode.rs
index db36a06cbe..e38450b73f 100644
--- a/crates/ide/src/inlay_hints/binding_mode.rs
+++ b/crates/ide/src/inlay_hints/binding_mode.rs
@@ -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