Skip to content

Commit c92f76d

Browse files
committed
Add text edit to binding mode hints
1 parent cb816ad commit c92f76d

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,19 +410,6 @@ impl InlayHint {
410410
}
411411
}
412412

413-
fn opening_paren_before(kind: InlayKind, range: TextRange) -> InlayHint {
414-
InlayHint {
415-
range,
416-
kind,
417-
label: InlayHintLabel::from("("),
418-
text_edit: None,
419-
position: InlayHintPosition::Before,
420-
pad_left: false,
421-
pad_right: false,
422-
resolve_parent: None,
423-
}
424-
}
425-
426413
pub fn needs_resolve(&self) -> Option<TextRange> {
427414
self.resolve_parent.filter(|_| self.text_edit.is_some() || self.label.needs_resolve())
428415
}

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ide_db::famous_defs::FamousDefs;
99

1010
use span::EditionedFileId;
1111
use syntax::ast::{self, AstNode};
12+
use text_edit::TextEditBuilder;
1213

1314
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
1415

@@ -61,40 +62,55 @@ pub(super) fn hints(
6162
hint.label.append_str(r);
6263
});
6364
hint.pad_right = was_mut_last;
64-
if !hint.label.parts.is_empty() {
65-
acc.push(hint);
66-
}
65+
let acc_base = acc.len();
6766
match pat {
6867
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
6968
let bm = sema.binding_mode_of_pat(pat)?;
7069
let bm = match bm {
71-
hir::BindingMode::Move => return None,
72-
hir::BindingMode::Ref(Mutability::Mut) => "ref mut",
73-
hir::BindingMode::Ref(Mutability::Shared) => "ref",
70+
hir::BindingMode::Move => None,
71+
hir::BindingMode::Ref(Mutability::Mut) => Some("ref mut"),
72+
hir::BindingMode::Ref(Mutability::Shared) => Some("ref"),
7473
};
75-
acc.push(InlayHint {
76-
range: pat.syntax().text_range(),
77-
kind: InlayKind::BindingMode,
78-
label: bm.into(),
79-
text_edit: None,
80-
position: InlayHintPosition::Before,
81-
pad_left: false,
82-
pad_right: true,
83-
resolve_parent: Some(pat.syntax().text_range()),
84-
});
74+
if let Some(bm) = bm {
75+
acc.push(InlayHint {
76+
range: pat.syntax().text_range(),
77+
kind: InlayKind::BindingMode,
78+
label: bm.into(),
79+
text_edit: None,
80+
position: InlayHintPosition::Before,
81+
pad_left: false,
82+
pad_right: true,
83+
resolve_parent: Some(pat.syntax().text_range()),
84+
});
85+
}
8586
}
8687
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {
87-
acc.push(InlayHint::opening_paren_before(
88-
InlayKind::BindingMode,
89-
pat.syntax().text_range(),
90-
));
88+
hint.label.append_str("(");
9189
acc.push(InlayHint::closing_paren_after(
9290
InlayKind::BindingMode,
9391
pat.syntax().text_range(),
9492
));
9593
}
9694
_ => (),
9795
}
96+
if !hint.label.parts.is_empty() {
97+
acc.push(hint);
98+
}
99+
100+
if let hints @ [_, ..] = &mut acc[acc_base..] {
101+
let mut edit = TextEditBuilder::default();
102+
for h in &mut *hints {
103+
edit.insert(
104+
match h.position {
105+
InlayHintPosition::Before => h.range.start(),
106+
InlayHintPosition::After => h.range.end(),
107+
},
108+
h.label.parts.iter().map(|p| &*p.text).collect(),
109+
);
110+
}
111+
let edit = edit.finish();
112+
hints.iter_mut().for_each(|h| h.text_edit = Some(edit.clone()));
113+
}
98114

99115
Some(())
100116
}
@@ -145,11 +161,10 @@ fn __(
145161
}
146162
match &(0,) {
147163
(x,) | (x,) => (),
148-
//^^^^^^^^^^^&
164+
//^^^^^^^^^^^)
165+
//^^^^^^^^^^^&(
149166
//^ ref
150167
//^ ref
151-
//^^^^^^^^^^^(
152-
//^^^^^^^^^^^)
153168
((x,) | (x,)) => (),
154169
//^^^^^^^^^^^^^&
155170
//^ ref

0 commit comments

Comments
 (0)