Skip to content

Commit

Permalink
Merge #10761
Browse files Browse the repository at this point in the history
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 <jh@discordapp.com>
  • Loading branch information
bors[bot] and jhgg committed Nov 13, 2021
2 parents c634615 + 520ff62 commit 766b52b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
68 changes: 67 additions & 1 deletion crates/ide/src/inlay_hints.rs
Expand Up @@ -16,6 +16,7 @@ pub struct InlayHintsConfig {
pub type_hints: bool,
pub parameter_hints: bool,
pub chaining_hints: bool,
pub hide_named_constructor_hints: bool,
pub max_length: Option<usize>,
}

Expand Down Expand Up @@ -213,7 +214,9 @@ fn get_bind_pat_hints(
Some(label) => label,
None => {
let ty_name = ty.display_truncated(sema.db, config.max_length).to_string();
if is_named_constructor(sema, pat, &ty_name).is_some() {
if config.hide_named_constructor_hints
&& is_named_constructor(sema, pat, &ty_name).is_some()
{
return None;
}
ty_name.into()
Expand Down Expand Up @@ -537,6 +540,7 @@ mod tests {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
};

Expand All @@ -552,6 +556,7 @@ mod tests {
parameter_hints: true,
type_hints: false,
chaining_hints: false,
hide_named_constructor_hints: false,
max_length: None,
},
ra_fixture,
Expand All @@ -565,6 +570,7 @@ mod tests {
parameter_hints: false,
type_hints: true,
chaining_hints: false,
hide_named_constructor_hints: false,
max_length: None,
},
ra_fixture,
Expand All @@ -578,6 +584,7 @@ mod tests {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
},
ra_fixture,
Expand Down Expand Up @@ -608,6 +615,7 @@ mod tests {
type_hints: false,
parameter_hints: false,
chaining_hints: false,
hide_named_constructor_hints: false,
max_length: None,
},
r#"
Expand Down Expand Up @@ -1313,6 +1321,55 @@ fn main() {

#[test]
fn skip_constructor_type_hints() {
check_with_config(
InlayHintsConfig {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_named_constructor_hints: true,
max_length: None,
},
r#"
//- minicore: try
use core::ops::ControlFlow;
struct Struct;
struct TupleStruct();
impl Struct {
fn new() -> Self {
Struct
}
fn try_new() -> ControlFlow<(), Self> {
ControlFlow::Continue(Struct)
}
}
struct Generic<T>(T);
impl Generic<i32> {
fn new() -> Self {
Generic(0)
}
}
fn main() {
let strukt = Struct::new();
let tuple_struct = TupleStruct();
let generic0 = Generic::new();
// ^^^^^^^^ Generic<i32>
let generic1 = Generic::<i32>::new();
let generic2 = <Generic<i32>>::new();
}
fn fallible() -> ControlFlow<()> {
let strukt = Struct::try_new()?;
}
"#,
);
}

#[test]
fn shows_constructor_type_hints_when_enabled() {
check_types(
r#"
//- minicore: try
Expand All @@ -1339,15 +1396,20 @@ impl Generic<i32> {
fn main() {
let strukt = Struct::new();
// ^^^^^^ Struct
let tuple_struct = TupleStruct();
// ^^^^^^^^^^^^ TupleStruct
let generic0 = Generic::new();
// ^^^^^^^^ Generic<i32>
let generic1 = Generic::<i32>::new();
// ^^^^^^^^ Generic<i32>
let generic2 = <Generic<i32>>::new();
// ^^^^^^^^ Generic<i32>
}
fn fallible() -> ControlFlow<()> {
let strukt = Struct::try_new()?;
// ^^^^^^ Struct
}
"#,
);
Expand Down Expand Up @@ -1408,6 +1470,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
},
r#"
Expand Down Expand Up @@ -1464,6 +1527,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
},
r#"
Expand Down Expand Up @@ -1508,6 +1572,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
},
r#"
Expand Down Expand Up @@ -1553,6 +1618,7 @@ fn main() {
parameter_hints: false,
type_hints: false,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: None,
},
r#"
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/static_index.rs
Expand Up @@ -106,6 +106,7 @@ impl StaticIndex<'_> {
type_hints: true,
parameter_hints: true,
chaining_hints: true,
hide_named_constructor_hints: false,
max_length: Some(25),
},
file_id,
Expand Down
11 changes: 7 additions & 4 deletions crates/rust-analyzer/src/config.rs
Expand Up @@ -195,14 +195,16 @@ config_data! {
hoverActions_run: bool = "true",

/// Whether to show inlay type hints for method chains.
inlayHints_chainingHints: bool = "true",
inlayHints_chainingHints: bool = "true",
/// Maximum length for inlay hints. Set to null to have an unlimited length.
inlayHints_maxLength: Option<usize> = "25",
inlayHints_maxLength: Option<usize> = "25",
/// Whether to show function parameter name inlay hints at the call
/// site.
inlayHints_parameterHints: bool = "true",
inlayHints_parameterHints: bool = "true",
/// Whether to show inlay type hints for variables.
inlayHints_typeHints: bool = "true",
inlayHints_typeHints: bool = "true",
/// Whether to hide inlay hints for constructors.
inlayHints_hideNamedConstructorHints: bool = "false",

/// Join lines inserts else between consecutive ifs.
joinLines_joinElseIf: bool = "true",
Expand Down Expand Up @@ -768,6 +770,7 @@ impl Config {
type_hints: self.data.inlayHints_typeHints,
parameter_hints: self.data.inlayHints_parameterHints,
chaining_hints: self.data.inlayHints_chainingHints,
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
max_length: self.data.inlayHints_maxLength,
}
}
Expand Down
5 changes: 5 additions & 0 deletions docs/user/generated_config.adoc
Expand Up @@ -308,6 +308,11 @@ site.
--
Whether to show inlay type hints for variables.
--
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
+
--
Whether to hide inlay hints for constructors.
--
[[rust-analyzer.joinLines.joinElseIf]]rust-analyzer.joinLines.joinElseIf (default: `true`)::
+
--
Expand Down
5 changes: 5 additions & 0 deletions editors/code/package.json
Expand Up @@ -752,6 +752,11 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.inlayHints.hideNamedConstructorHints": {
"markdownDescription": "Whether to hide inlay hints for constructors.",
"default": false,
"type": "boolean"
},
"rust-analyzer.joinLines.joinElseIf": {
"markdownDescription": "Join lines inserts else between consecutive ifs.",
"default": true,
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/config.ts
Expand Up @@ -114,6 +114,7 @@ export class Config {
typeHints: this.get<boolean>("inlayHints.typeHints"),
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
hideNamedConstructorHints: this.get<boolean>("inlayHints.hideNamedConstructorHints"),
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
maxLength: this.get<null | number>("inlayHints.maxLength"),
};
Expand Down

0 comments on commit 766b52b

Please sign in to comment.