From d3d768de0d8af96b51cdcdadde511028d3b3d87f Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Sat, 13 Nov 2021 23:12:29 +0000 Subject: [PATCH 1/2] inlay hints: add the option to always show constructor inlay hints --- crates/ide/src/inlay_hints.rs | 68 +++++++++++++++++++++++++++++- crates/ide/src/static_index.rs | 1 + crates/rust-analyzer/src/config.rs | 11 +++-- docs/user/generated_config.adoc | 5 +++ editors/code/package.json | 5 +++ editors/code/src/config.ts | 1 + 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 710d6b788217..79c2475ee898 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -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, } @@ -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() @@ -537,6 +540,7 @@ mod tests { type_hints: true, parameter_hints: true, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }; @@ -552,6 +556,7 @@ mod tests { parameter_hints: true, type_hints: false, chaining_hints: false, + hide_named_constructor_hints: true, max_length: None, }, ra_fixture, @@ -565,6 +570,7 @@ mod tests { parameter_hints: false, type_hints: true, chaining_hints: false, + hide_named_constructor_hints: true, max_length: None, }, ra_fixture, @@ -578,6 +584,7 @@ mod tests { parameter_hints: false, type_hints: false, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }, ra_fixture, @@ -608,6 +615,7 @@ mod tests { type_hints: false, parameter_hints: false, chaining_hints: false, + hide_named_constructor_hints: true, max_length: None, }, r#" @@ -1353,6 +1361,60 @@ fn fallible() -> ControlFlow<()> { ); } + #[test] + fn shows_constructor_type_hints_when_enabled() { + check_with_config( + InlayHintsConfig { + type_hints: true, + parameter_hints: true, + chaining_hints: true, + hide_named_constructor_hints: false, + 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); +impl Generic { + fn new() -> Self { + Generic(0) + } +} + +fn main() { + let strukt = Struct::new(); + // ^^^^^^ Struct + let tuple_struct = TupleStruct(); + // ^^^^^^^^^^^^ TupleStruct + let generic0 = Generic::new(); + // ^^^^^^^^ Generic + let generic1 = Generic::::new(); + // ^^^^^^^^ Generic + let generic2 = >::new(); + // ^^^^^^^^ Generic +} + +fn fallible() -> ControlFlow<()> { + let strukt = Struct::try_new()?; + // ^^^^^^ Struct +} +"#, + ); + } + #[test] fn closures() { check( @@ -1408,6 +1470,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }, r#" @@ -1464,6 +1527,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }, r#" @@ -1508,6 +1572,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }, r#" @@ -1553,6 +1618,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, + hide_named_constructor_hints: true, max_length: None, }, r#" diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 75249a959dcf..036839a801b7 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -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, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 81f4ccd365c3..f2078d86958f 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -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 = "25", + inlayHints_maxLength: Option = "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 = "true", /// Join lines inserts else between consecutive ifs. joinLines_joinElseIf: bool = "true", @@ -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, } } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 9a00a0eb661c..d6af6f738c5e 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -308,6 +308,11 @@ site. -- Whether to show inlay type hints for variables. -- +[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `true`):: ++ +-- +Whether to hide inlay hints for constructors. +-- [[rust-analyzer.joinLines.joinElseIf]]rust-analyzer.joinLines.joinElseIf (default: `true`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index 4c718f0b7984..888c9dccf336 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -752,6 +752,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.inlayHints.hideNamedConstructorHints": { + "markdownDescription": "Whether to hide inlay hints for constructors.", + "default": true, + "type": "boolean" + }, "rust-analyzer.joinLines.joinElseIf": { "markdownDescription": "Join lines inserts else between consecutive ifs.", "default": true, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 3f3ca6309813..a3e29fc156ff 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -114,6 +114,7 @@ export class Config { typeHints: this.get("inlayHints.typeHints"), parameterHints: this.get("inlayHints.parameterHints"), chainingHints: this.get("inlayHints.chainingHints"), + hideNamedConstructorHints: this.get("inlayHints.hideNamedConstructorHints"), smallerHints: this.get("inlayHints.smallerHints"), maxLength: this.get("inlayHints.maxLength"), }; From 520ff62f4e030c45c68207f8abc7bfea072a6e8d Mon Sep 17 00:00:00 2001 From: Jake Heinz Date: Sat, 13 Nov 2021 23:39:34 +0000 Subject: [PATCH 2/2] flip the default --- crates/ide/src/inlay_hints.rs | 36 +++++++++++++++--------------- crates/rust-analyzer/src/config.rs | 2 +- docs/user/generated_config.adoc | 2 +- editors/code/package.json | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 79c2475ee898..f3c5dce7c384 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -540,7 +540,7 @@ mod tests { type_hints: true, parameter_hints: true, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }; @@ -556,7 +556,7 @@ mod tests { parameter_hints: true, type_hints: false, chaining_hints: false, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, ra_fixture, @@ -570,7 +570,7 @@ mod tests { parameter_hints: false, type_hints: true, chaining_hints: false, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, ra_fixture, @@ -584,7 +584,7 @@ mod tests { parameter_hints: false, type_hints: false, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, ra_fixture, @@ -615,7 +615,7 @@ mod tests { type_hints: false, parameter_hints: false, chaining_hints: false, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, r#" @@ -1321,7 +1321,14 @@ fn main() { #[test] fn skip_constructor_type_hints() { - check_types( + 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; @@ -1363,14 +1370,7 @@ fn fallible() -> ControlFlow<()> { #[test] fn shows_constructor_type_hints_when_enabled() { - check_with_config( - InlayHintsConfig { - type_hints: true, - parameter_hints: true, - chaining_hints: true, - hide_named_constructor_hints: false, - max_length: None, - }, + check_types( r#" //- minicore: try use core::ops::ControlFlow; @@ -1470,7 +1470,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, r#" @@ -1527,7 +1527,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, r#" @@ -1572,7 +1572,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, r#" @@ -1618,7 +1618,7 @@ fn main() { parameter_hints: false, type_hints: false, chaining_hints: true, - hide_named_constructor_hints: true, + hide_named_constructor_hints: false, max_length: None, }, r#" diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index f2078d86958f..6a72c4e32618 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -204,7 +204,7 @@ config_data! { /// Whether to show inlay type hints for variables. inlayHints_typeHints: bool = "true", /// Whether to hide inlay hints for constructors. - inlayHints_hideNamedConstructorHints: bool = "true", + inlayHints_hideNamedConstructorHints: bool = "false", /// Join lines inserts else between consecutive ifs. joinLines_joinElseIf: bool = "true", diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index d6af6f738c5e..c7bb1bf6420e 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -308,7 +308,7 @@ site. -- Whether to show inlay type hints for variables. -- -[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `true`):: +[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`):: + -- Whether to hide inlay hints for constructors. diff --git a/editors/code/package.json b/editors/code/package.json index 888c9dccf336..e1cbe8ba6404 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -754,7 +754,7 @@ }, "rust-analyzer.inlayHints.hideNamedConstructorHints": { "markdownDescription": "Whether to hide inlay hints for constructors.", - "default": true, + "default": false, "type": "boolean" }, "rust-analyzer.joinLines.joinElseIf": {