Skip to content

Commit 766b52b

Browse files
bors[bot]jhgg
andauthored
Merge #10761
10761: inlay hints: add the option to always show constructor inlay hints r=Veykril a=jhgg This PR adds a config to *disable* the functionality in #10441 - _and makes that functionality disabled by default._ I actually *really* like inlay hints always showing up, and it helps a lot as a teaching tool too, and also it's quite reassuring to know that r-a indeed understands the code I've written. This PR adds the option `rust-analyzer.inlayHints.hideNamedConstructorHints` (i can also invert this to be `rust-analyzer.inlayHints.showNamedConstructorHints`, just let me know.) Co-authored-by: Jake Heinz <[email protected]>
2 parents c634615 + 520ff62 commit 766b52b

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct InlayHintsConfig {
1616
pub type_hints: bool,
1717
pub parameter_hints: bool,
1818
pub chaining_hints: bool,
19+
pub hide_named_constructor_hints: bool,
1920
pub max_length: Option<usize>,
2021
}
2122

@@ -213,7 +214,9 @@ fn get_bind_pat_hints(
213214
Some(label) => label,
214215
None => {
215216
let ty_name = ty.display_truncated(sema.db, config.max_length).to_string();
216-
if is_named_constructor(sema, pat, &ty_name).is_some() {
217+
if config.hide_named_constructor_hints
218+
&& is_named_constructor(sema, pat, &ty_name).is_some()
219+
{
217220
return None;
218221
}
219222
ty_name.into()
@@ -537,6 +540,7 @@ mod tests {
537540
type_hints: true,
538541
parameter_hints: true,
539542
chaining_hints: true,
543+
hide_named_constructor_hints: false,
540544
max_length: None,
541545
};
542546

@@ -552,6 +556,7 @@ mod tests {
552556
parameter_hints: true,
553557
type_hints: false,
554558
chaining_hints: false,
559+
hide_named_constructor_hints: false,
555560
max_length: None,
556561
},
557562
ra_fixture,
@@ -565,6 +570,7 @@ mod tests {
565570
parameter_hints: false,
566571
type_hints: true,
567572
chaining_hints: false,
573+
hide_named_constructor_hints: false,
568574
max_length: None,
569575
},
570576
ra_fixture,
@@ -578,6 +584,7 @@ mod tests {
578584
parameter_hints: false,
579585
type_hints: false,
580586
chaining_hints: true,
587+
hide_named_constructor_hints: false,
581588
max_length: None,
582589
},
583590
ra_fixture,
@@ -608,6 +615,7 @@ mod tests {
608615
type_hints: false,
609616
parameter_hints: false,
610617
chaining_hints: false,
618+
hide_named_constructor_hints: false,
611619
max_length: None,
612620
},
613621
r#"
@@ -1313,6 +1321,55 @@ fn main() {
13131321

13141322
#[test]
13151323
fn skip_constructor_type_hints() {
1324+
check_with_config(
1325+
InlayHintsConfig {
1326+
type_hints: true,
1327+
parameter_hints: true,
1328+
chaining_hints: true,
1329+
hide_named_constructor_hints: true,
1330+
max_length: None,
1331+
},
1332+
r#"
1333+
//- minicore: try
1334+
use core::ops::ControlFlow;
1335+
1336+
struct Struct;
1337+
struct TupleStruct();
1338+
1339+
impl Struct {
1340+
fn new() -> Self {
1341+
Struct
1342+
}
1343+
fn try_new() -> ControlFlow<(), Self> {
1344+
ControlFlow::Continue(Struct)
1345+
}
1346+
}
1347+
1348+
struct Generic<T>(T);
1349+
impl Generic<i32> {
1350+
fn new() -> Self {
1351+
Generic(0)
1352+
}
1353+
}
1354+
1355+
fn main() {
1356+
let strukt = Struct::new();
1357+
let tuple_struct = TupleStruct();
1358+
let generic0 = Generic::new();
1359+
// ^^^^^^^^ Generic<i32>
1360+
let generic1 = Generic::<i32>::new();
1361+
let generic2 = <Generic<i32>>::new();
1362+
}
1363+
1364+
fn fallible() -> ControlFlow<()> {
1365+
let strukt = Struct::try_new()?;
1366+
}
1367+
"#,
1368+
);
1369+
}
1370+
1371+
#[test]
1372+
fn shows_constructor_type_hints_when_enabled() {
13161373
check_types(
13171374
r#"
13181375
//- minicore: try
@@ -1339,15 +1396,20 @@ impl Generic<i32> {
13391396
13401397
fn main() {
13411398
let strukt = Struct::new();
1399+
// ^^^^^^ Struct
13421400
let tuple_struct = TupleStruct();
1401+
// ^^^^^^^^^^^^ TupleStruct
13431402
let generic0 = Generic::new();
13441403
// ^^^^^^^^ Generic<i32>
13451404
let generic1 = Generic::<i32>::new();
1405+
// ^^^^^^^^ Generic<i32>
13461406
let generic2 = <Generic<i32>>::new();
1407+
// ^^^^^^^^ Generic<i32>
13471408
}
13481409
13491410
fn fallible() -> ControlFlow<()> {
13501411
let strukt = Struct::try_new()?;
1412+
// ^^^^^^ Struct
13511413
}
13521414
"#,
13531415
);
@@ -1408,6 +1470,7 @@ fn main() {
14081470
parameter_hints: false,
14091471
type_hints: false,
14101472
chaining_hints: true,
1473+
hide_named_constructor_hints: false,
14111474
max_length: None,
14121475
},
14131476
r#"
@@ -1464,6 +1527,7 @@ fn main() {
14641527
parameter_hints: false,
14651528
type_hints: false,
14661529
chaining_hints: true,
1530+
hide_named_constructor_hints: false,
14671531
max_length: None,
14681532
},
14691533
r#"
@@ -1508,6 +1572,7 @@ fn main() {
15081572
parameter_hints: false,
15091573
type_hints: false,
15101574
chaining_hints: true,
1575+
hide_named_constructor_hints: false,
15111576
max_length: None,
15121577
},
15131578
r#"
@@ -1553,6 +1618,7 @@ fn main() {
15531618
parameter_hints: false,
15541619
type_hints: false,
15551620
chaining_hints: true,
1621+
hide_named_constructor_hints: false,
15561622
max_length: None,
15571623
},
15581624
r#"

crates/ide/src/static_index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl StaticIndex<'_> {
106106
type_hints: true,
107107
parameter_hints: true,
108108
chaining_hints: true,
109+
hide_named_constructor_hints: false,
109110
max_length: Some(25),
110111
},
111112
file_id,

crates/rust-analyzer/src/config.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,16 @@ config_data! {
195195
hoverActions_run: bool = "true",
196196

197197
/// Whether to show inlay type hints for method chains.
198-
inlayHints_chainingHints: bool = "true",
198+
inlayHints_chainingHints: bool = "true",
199199
/// Maximum length for inlay hints. Set to null to have an unlimited length.
200-
inlayHints_maxLength: Option<usize> = "25",
200+
inlayHints_maxLength: Option<usize> = "25",
201201
/// Whether to show function parameter name inlay hints at the call
202202
/// site.
203-
inlayHints_parameterHints: bool = "true",
203+
inlayHints_parameterHints: bool = "true",
204204
/// Whether to show inlay type hints for variables.
205-
inlayHints_typeHints: bool = "true",
205+
inlayHints_typeHints: bool = "true",
206+
/// Whether to hide inlay hints for constructors.
207+
inlayHints_hideNamedConstructorHints: bool = "false",
206208

207209
/// Join lines inserts else between consecutive ifs.
208210
joinLines_joinElseIf: bool = "true",
@@ -768,6 +770,7 @@ impl Config {
768770
type_hints: self.data.inlayHints_typeHints,
769771
parameter_hints: self.data.inlayHints_parameterHints,
770772
chaining_hints: self.data.inlayHints_chainingHints,
773+
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
771774
max_length: self.data.inlayHints_maxLength,
772775
}
773776
}

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ site.
308308
--
309309
Whether to show inlay type hints for variables.
310310
--
311+
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
312+
+
313+
--
314+
Whether to hide inlay hints for constructors.
315+
--
311316
[[rust-analyzer.joinLines.joinElseIf]]rust-analyzer.joinLines.joinElseIf (default: `true`)::
312317
+
313318
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@
752752
"default": true,
753753
"type": "boolean"
754754
},
755+
"rust-analyzer.inlayHints.hideNamedConstructorHints": {
756+
"markdownDescription": "Whether to hide inlay hints for constructors.",
757+
"default": false,
758+
"type": "boolean"
759+
},
755760
"rust-analyzer.joinLines.joinElseIf": {
756761
"markdownDescription": "Join lines inserts else between consecutive ifs.",
757762
"default": true,

editors/code/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export class Config {
114114
typeHints: this.get<boolean>("inlayHints.typeHints"),
115115
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
116116
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
117+
hideNamedConstructorHints: this.get<boolean>("inlayHints.hideNamedConstructorHints"),
117118
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
118119
maxLength: this.get<null | number>("inlayHints.maxLength"),
119120
};

0 commit comments

Comments
 (0)