summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs')
-rw-r--r--src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs55
1 files changed, 32 insertions, 23 deletions
diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs
index a0166d004..5d9729263 100644
--- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs
+++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints/binding_mode.rs
@@ -7,7 +7,7 @@ use ide_db::RootDatabase;
use syntax::ast::{self, AstNode};
-use crate::{InlayHint, InlayHintsConfig, InlayKind, InlayTooltip};
+use crate::{InlayHint, InlayHintsConfig, InlayKind};
pub(super) fn hints(
acc: &mut Vec<InlayHint>,
@@ -29,8 +29,17 @@ pub(super) fn hints(
_ => None,
})
.last();
- let range =
- outer_paren_pat.as_ref().map_or_else(|| pat.syntax(), |it| it.syntax()).text_range();
+ let range = outer_paren_pat.as_ref().map_or_else(
+ || match pat {
+ // for ident patterns that @ bind a name, render the un-ref patterns in front of the inner pattern
+ // instead of the name as that makes it more clear and doesn't really change the outcome
+ ast::Pat::IdentPat(it) => {
+ it.pat().map_or_else(|| it.syntax().text_range(), |it| it.syntax().text_range())
+ }
+ it => it.syntax().text_range(),
+ },
+ |it| it.syntax().text_range(),
+ );
let pattern_adjustments = sema.pattern_adjustments(pat);
pattern_adjustments.iter().for_each(|ty| {
let reference = ty.is_reference();
@@ -40,12 +49,7 @@ pub(super) fn hints(
(true, false) => "&",
_ => return,
};
- acc.push(InlayHint {
- range,
- kind: InlayKind::BindingModeHint,
- label: r.to_string().into(),
- tooltip: Some(InlayTooltip::String("Inferred binding mode".into())),
- });
+ acc.push(InlayHint { range, kind: InlayKind::BindingMode, label: r.to_string().into() });
});
match pat {
ast::Pat::IdentPat(pat) if pat.ref_token().is_none() && pat.mut_token().is_none() => {
@@ -57,24 +61,13 @@ pub(super) fn hints(
};
acc.push(InlayHint {
range: pat.syntax().text_range(),
- kind: InlayKind::BindingModeHint,
+ kind: InlayKind::BindingMode,
label: bm.to_string().into(),
- tooltip: Some(InlayTooltip::String("Inferred binding mode".into())),
});
}
ast::Pat::OrPat(pat) if !pattern_adjustments.is_empty() && outer_paren_pat.is_none() => {
- acc.push(InlayHint {
- range: pat.syntax().text_range(),
- kind: InlayKind::OpeningParenthesis,
- label: "(".into(),
- tooltip: None,
- });
- acc.push(InlayHint {
- range: pat.syntax().text_range(),
- kind: InlayKind::ClosingParenthesis,
- label: ")".into(),
- tooltip: None,
- });
+ acc.push(InlayHint::opening_paren(pat.syntax().text_range()));
+ acc.push(InlayHint::closing_paren(pat.syntax().text_range()));
}
_ => (),
}
@@ -139,4 +132,20 @@ fn __(
}"#,
);
}
+
+ #[test]
+ fn hints_binding_modes_complex_ident_pat() {
+ check_with_config(
+ InlayHintsConfig { binding_mode_hints: true, ..DISABLED_CONFIG },
+ r#"
+struct Struct {
+ field: &'static str,
+}
+fn foo(s @ Struct { field, .. }: &Struct) {}
+ //^^^^^^^^^^^^^^^^^^^^^^^^ref
+ //^^^^^^^^^^^^^^^^^^^^&
+ //^^^^^ref
+"#,
+ );
+ }
}