mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
Add text edit to binding mode hints
This commit is contained in:
parent
f086fa9c02
commit
fd17fa10a2
2 changed files with 38 additions and 36 deletions
|
@ -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> {
|
pub fn needs_resolve(&self) -> Option<TextRange> {
|
||||||
self.resolve_parent.filter(|_| self.text_edit.is_some() || self.label.needs_resolve())
|
self.resolve_parent.filter(|_| self.text_edit.is_some() || self.label.needs_resolve())
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ use ide_db::famous_defs::FamousDefs;
|
||||||
|
|
||||||
use span::EditionedFileId;
|
use span::EditionedFileId;
|
||||||
use syntax::ast::{self, AstNode};
|
use syntax::ast::{self, AstNode};
|
||||||
|
use text_edit::TextEditBuilder;
|
||||||
|
|
||||||
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
|
use crate::{InlayHint, InlayHintLabel, InlayHintPosition, InlayHintsConfig, InlayKind};
|
||||||
|
|
||||||
|
@ -61,33 +62,30 @@ pub(super) fn hints(
|
||||||
hint.label.append_str(r);
|
hint.label.append_str(r);
|
||||||
});
|
});
|
||||||
hint.pad_right = was_mut_last;
|
hint.pad_right = was_mut_last;
|
||||||
if !hint.label.parts.is_empty() {
|
let acc_base = acc.len();
|
||||||
acc.push(hint);
|
|
||||||
}
|
|
||||||
match pat {
|
match pat {
|
||||||
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
|
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 = sema.binding_mode_of_pat(pat)?;
|
||||||
let bm = match bm {
|
let bm = match bm {
|
||||||
hir::BindingMode::Move => return None,
|
hir::BindingMode::Move => None,
|
||||||
hir::BindingMode::Ref(Mutability::Mut) => "ref mut",
|
hir::BindingMode::Ref(Mutability::Mut) => Some("ref mut"),
|
||||||
hir::BindingMode::Ref(Mutability::Shared) => "ref",
|
hir::BindingMode::Ref(Mutability::Shared) => Some("ref"),
|
||||||
};
|
};
|
||||||
acc.push(InlayHint {
|
if let Some(bm) = bm {
|
||||||
range: pat.syntax().text_range(),
|
acc.push(InlayHint {
|
||||||
kind: InlayKind::BindingMode,
|
range: pat.syntax().text_range(),
|
||||||
label: bm.into(),
|
kind: InlayKind::BindingMode,
|
||||||
text_edit: None,
|
label: bm.into(),
|
||||||
position: InlayHintPosition::Before,
|
text_edit: None,
|
||||||
pad_left: false,
|
position: InlayHintPosition::Before,
|
||||||
pad_right: true,
|
pad_left: false,
|
||||||
resolve_parent: Some(pat.syntax().text_range()),
|
pad_right: true,
|
||||||
});
|
resolve_parent: Some(pat.syntax().text_range()),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {
|
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {
|
||||||
acc.push(InlayHint::opening_paren_before(
|
hint.label.append_str("(");
|
||||||
InlayKind::BindingMode,
|
|
||||||
pat.syntax().text_range(),
|
|
||||||
));
|
|
||||||
acc.push(InlayHint::closing_paren_after(
|
acc.push(InlayHint::closing_paren_after(
|
||||||
InlayKind::BindingMode,
|
InlayKind::BindingMode,
|
||||||
pat.syntax().text_range(),
|
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(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
@ -145,11 +161,10 @@ fn __(
|
||||||
}
|
}
|
||||||
match &(0,) {
|
match &(0,) {
|
||||||
(x,) | (x,) => (),
|
(x,) | (x,) => (),
|
||||||
//^^^^^^^^^^^&
|
//^^^^^^^^^^^)
|
||||||
|
//^^^^^^^^^^^&(
|
||||||
//^ ref
|
//^ ref
|
||||||
//^ ref
|
//^ ref
|
||||||
//^^^^^^^^^^^(
|
|
||||||
//^^^^^^^^^^^)
|
|
||||||
((x,) | (x,)) => (),
|
((x,) | (x,)) => (),
|
||||||
//^^^^^^^^^^^^^&
|
//^^^^^^^^^^^^^&
|
||||||
//^ ref
|
//^ ref
|
||||||
|
|
Loading…
Reference in a new issue