Skip to content

Commit c2190ad

Browse files
committed
feat: Allow reborrow inlay hints to be restricted to mutable reborrows only
1 parent 4b505ce commit c2190ad

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed

crates/ide/src/inlay_hints.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct InlayHintsConfig {
1919
pub type_hints: bool,
2020
pub parameter_hints: bool,
2121
pub chaining_hints: bool,
22-
pub reborrow_hints: bool,
22+
pub reborrow_hints: ReborrowHints,
2323
pub closure_return_type_hints: bool,
2424
pub lifetime_elision_hints: LifetimeElisionHints,
2525
pub param_names_for_lifetime_elision_hints: bool,
@@ -34,6 +34,13 @@ pub enum LifetimeElisionHints {
3434
Never,
3535
}
3636

37+
#[derive(Clone, Debug, PartialEq, Eq)]
38+
pub enum ReborrowHints {
39+
Always,
40+
MutableOnly,
41+
Never,
42+
}
43+
3744
#[derive(Clone, Debug, PartialEq, Eq)]
3845
pub enum InlayKind {
3946
ChainingHint,
@@ -372,18 +379,20 @@ fn reborrow_hints(
372379
config: &InlayHintsConfig,
373380
expr: &ast::Expr,
374381
) -> Option<()> {
375-
if !config.reborrow_hints {
382+
if config.reborrow_hints == ReborrowHints::Never {
376383
return None;
377384
}
378385

379386
let mutability = sema.is_implicit_reborrow(expr)?;
387+
let label = match mutability {
388+
hir::Mutability::Shared if config.reborrow_hints != ReborrowHints::MutableOnly => "&*",
389+
hir::Mutability::Mut => "&mut *",
390+
_ => return None,
391+
};
380392
acc.push(InlayHint {
381393
range: expr.syntax().text_range(),
382394
kind: InlayKind::ImplicitReborrow,
383-
label: match mutability {
384-
hir::Mutability::Shared => SmolStr::new_inline("&*"),
385-
hir::Mutability::Mut => SmolStr::new_inline("&mut *"),
386-
},
395+
label: SmolStr::new_inline(label),
387396
});
388397
Some(())
389398
}
@@ -848,6 +857,7 @@ mod tests {
848857
use syntax::{TextRange, TextSize};
849858
use test_utils::extract_annotations;
850859

860+
use crate::inlay_hints::ReborrowHints;
851861
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
852862

853863
const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
@@ -858,15 +868,15 @@ mod tests {
858868
lifetime_elision_hints: LifetimeElisionHints::Never,
859869
hide_named_constructor_hints: false,
860870
closure_return_type_hints: false,
861-
reborrow_hints: false,
871+
reborrow_hints: ReborrowHints::Always,
862872
param_names_for_lifetime_elision_hints: false,
863873
max_length: None,
864874
};
865875
const TEST_CONFIG: InlayHintsConfig = InlayHintsConfig {
866876
type_hints: true,
867877
parameter_hints: true,
868878
chaining_hints: true,
869-
reborrow_hints: true,
879+
reborrow_hints: ReborrowHints::Always,
870880
closure_return_type_hints: true,
871881
lifetime_elision_hints: LifetimeElisionHints::Always,
872882
..DISABLED_CONFIG
@@ -2146,7 +2156,11 @@ impl () {
21462156
#[test]
21472157
fn hints_implicit_reborrow() {
21482158
check_with_config(
2149-
InlayHintsConfig { reborrow_hints: true, parameter_hints: true, ..DISABLED_CONFIG },
2159+
InlayHintsConfig {
2160+
reborrow_hints: ReborrowHints::Always,
2161+
parameter_hints: true,
2162+
..DISABLED_CONFIG
2163+
},
21502164
r#"
21512165
fn __() {
21522166
let unique = &mut ();

crates/ide/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub use crate::{
8080
folding_ranges::{Fold, FoldKind},
8181
highlight_related::{HighlightRelatedConfig, HighlightedRange},
8282
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
83-
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints},
83+
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints, ReborrowHints},
8484
join_lines::JoinLinesConfig,
8585
markup::Markup,
8686
moniker::{MonikerKind, MonikerResult, PackageInformation},

crates/ide/src/static_index.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ use ide_db::{
1212
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
1313

1414
use crate::{
15-
hover::hover_for_definition, Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult,
16-
InlayHint, InlayHintsConfig, TryToNav,
17-
};
18-
use crate::{
15+
hover::hover_for_definition,
1916
moniker::{crate_for_file, def_to_moniker, MonikerResult},
20-
LifetimeElisionHints,
17+
Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig,
18+
TryToNav,
2119
};
2220

2321
/// A static representation of fully analyzed source code.
@@ -112,8 +110,8 @@ impl StaticIndex<'_> {
112110
parameter_hints: true,
113111
chaining_hints: true,
114112
closure_return_type_hints: true,
115-
lifetime_elision_hints: LifetimeElisionHints::Never,
116-
reborrow_hints: false,
113+
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
114+
reborrow_hints: crate::ReborrowHints::Never,
117115
hide_named_constructor_hints: false,
118116
param_names_for_lifetime_elision_hints: false,
119117
max_length: Some(25),

crates/rust-analyzer/src/config.rs

+31-7
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use std::{ffi::OsString, fmt, iter, path::PathBuf};
1212
use flycheck::FlycheckConfig;
1313
use ide::{
1414
AssistConfig, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, HighlightRelatedConfig,
15-
HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, LifetimeElisionHints, Snippet,
16-
SnippetScope,
15+
HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope,
1716
};
1817
use ide_db::{
1918
imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -263,7 +262,7 @@ config_data! {
263262
/// site.
264263
inlayHints_parameterHints_enable: bool = "true",
265264
/// Whether to show inlay type hints for compiler inserted reborrows.
266-
inlayHints_reborrowHints_enable: bool = "false",
265+
inlayHints_reborrowHints_enable: ReborrowHintsDef = "\"never\"",
267266
/// Whether to render leading colons for type hints, and trailing colons for parameter hints.
268267
inlayHints_renderColons: bool = "true",
269268
/// Whether to show inlay type hints for variables.
@@ -986,12 +985,16 @@ impl Config {
986985
chaining_hints: self.data.inlayHints_chainingHints_enable,
987986
closure_return_type_hints: self.data.inlayHints_closureReturnTypeHints_enable,
988987
lifetime_elision_hints: match self.data.inlayHints_lifetimeElisionHints_enable {
989-
LifetimeElisionDef::Always => LifetimeElisionHints::Always,
990-
LifetimeElisionDef::Never => LifetimeElisionHints::Never,
991-
LifetimeElisionDef::SkipTrivial => LifetimeElisionHints::SkipTrivial,
988+
LifetimeElisionDef::Always => ide::LifetimeElisionHints::Always,
989+
LifetimeElisionDef::Never => ide::LifetimeElisionHints::Never,
990+
LifetimeElisionDef::SkipTrivial => ide::LifetimeElisionHints::SkipTrivial,
992991
},
993992
hide_named_constructor_hints: self.data.inlayHints_typeHints_hideNamedConstructor,
994-
reborrow_hints: self.data.inlayHints_reborrowHints_enable,
993+
reborrow_hints: match self.data.inlayHints_reborrowHints_enable {
994+
ReborrowHintsDef::Always => ide::ReborrowHints::Always,
995+
ReborrowHintsDef::Never => ide::ReborrowHints::Never,
996+
ReborrowHintsDef::Mutable => ide::ReborrowHints::MutableOnly,
997+
},
995998
param_names_for_lifetime_elision_hints: self
996999
.data
9971000
.inlayHints_lifetimeElisionHints_useParameterNames,
@@ -1293,6 +1296,7 @@ macro_rules! named_unit_variant {
12931296
mod de_unit_v {
12941297
named_unit_variant!(all);
12951298
named_unit_variant!(skip_trivial);
1299+
named_unit_variant!(mutable);
12961300
}
12971301

12981302
#[derive(Deserialize, Debug, Clone, Copy)]
@@ -1404,6 +1408,17 @@ enum LifetimeElisionDef {
14041408
SkipTrivial,
14051409
}
14061410

1411+
#[derive(Deserialize, Debug, Clone)]
1412+
#[serde(untagged)]
1413+
enum ReborrowHintsDef {
1414+
#[serde(deserialize_with = "true_or_always")]
1415+
Always,
1416+
#[serde(deserialize_with = "false_or_never")]
1417+
Never,
1418+
#[serde(deserialize_with = "de_unit_v::mutable")]
1419+
Mutable,
1420+
}
1421+
14071422
#[derive(Deserialize, Debug, Clone)]
14081423
#[serde(rename_all = "snake_case")]
14091424
enum ImportPrefixDef {
@@ -1675,6 +1690,15 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
16751690
"Only show lifetime elision hints if a return type is involved."
16761691
],
16771692
},
1693+
"ReborrowHintsDef" => set! {
1694+
"type": ["string", "boolean"],
1695+
"enum": ["always", "never", "mutable"],
1696+
"enumDescriptions": [
1697+
"Always show reborrow hints.",
1698+
"Never show reborrow hints.",
1699+
"Only show mutable reborrow hints."
1700+
],
1701+
},
16781702
"CargoFeatures" => set! {
16791703
"type": ["string", "array"],
16801704
"items": { "type": "string" },

docs/user/generated_config.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ Maximum length for inlay hints. Set to null to have an unlimited length.
365365
Whether to show function parameter name inlay hints at the call
366366
site.
367367
--
368-
[[rust-analyzer.inlayHints.reborrowHints.enable]]rust-analyzer.inlayHints.reborrowHints.enable (default: `false`)::
368+
[[rust-analyzer.inlayHints.reborrowHints.enable]]rust-analyzer.inlayHints.reborrowHints.enable (default: `"never"`)::
369369
+
370370
--
371371
Whether to show inlay type hints for compiler inserted reborrows.

editors/code/package.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -810,8 +810,21 @@
810810
},
811811
"rust-analyzer.inlayHints.reborrowHints.enable": {
812812
"markdownDescription": "Whether to show inlay type hints for compiler inserted reborrows.",
813-
"default": false,
814-
"type": "boolean"
813+
"default": "never",
814+
"type": [
815+
"string",
816+
"boolean"
817+
],
818+
"enum": [
819+
"always",
820+
"never",
821+
"mutable"
822+
],
823+
"enumDescriptions": [
824+
"Always show reborrow hints.",
825+
"Never show reborrow hints.",
826+
"Only show mutable reborrow hints."
827+
]
815828
},
816829
"rust-analyzer.inlayHints.renderColons": {
817830
"markdownDescription": "Whether to render leading colons for type hints, and trailing colons for parameter hints.",

0 commit comments

Comments
 (0)