From e8ee597340e4baf3c7eedf8eae4457e2ec994905 Mon Sep 17 00:00:00 2001 From: A4-Tacks Date: Thu, 4 Dec 2025 12:29:45 +0800 Subject: [PATCH] Add config hide placeholders type hints In the inferred type hints, expand the line too long. add config to disable it. Example --- ```json {"rust-analyzer.inlayHints.typeHints.hideInferredTypes": true} ``` ```rust use std::collections::HashMap; fn foo(iter: Vec, std::io::Error>>) { let output = iter.into_iter().collect::, _>>().unwrap(); } ``` **Before this PR** ```rust let output: Vec> = iter.into_iter().collect::>, _ = Error>>().unwrap(); ``` **After this PR** ```rust let output: Vec> = iter.into_iter().collect::, _>>().unwrap(); ``` --- crates/ide/src/inlay_hints.rs | 2 ++ crates/ide/src/inlay_hints/placeholders.rs | 20 ++++++++++++++++++- crates/ide/src/static_index.rs | 1 + .../rust-analyzer/src/cli/analysis_stats.rs | 1 + crates/rust-analyzer/src/config.rs | 4 ++++ docs/book/src/configuration_generated.md | 7 +++++++ editors/code/package.json | 10 ++++++++++ 7 files changed, 44 insertions(+), 1 deletion(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index deacc7fafbc0..260da900f402 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -320,6 +320,7 @@ pub struct InlayHintsConfig<'a> { pub implied_dyn_trait_hints: bool, pub lifetime_elision_hints: LifetimeElisionHints, pub param_names_for_lifetime_elision_hints: bool, + pub hide_inferred_type_hints: bool, pub hide_named_constructor_hints: bool, pub hide_closure_initialization_hints: bool, pub hide_closure_parameter_hints: bool, @@ -900,6 +901,7 @@ mod tests { adjustment_hints_mode: AdjustmentHintsMode::Prefix, adjustment_hints_hide_outside_unsafe: false, binding_mode_hints: false, + hide_inferred_type_hints: false, hide_named_constructor_hints: false, hide_closure_initialization_hints: false, hide_closure_parameter_hints: false, diff --git a/crates/ide/src/inlay_hints/placeholders.rs b/crates/ide/src/inlay_hints/placeholders.rs index 96d2c17c03bb..e535b92a5792 100644 --- a/crates/ide/src/inlay_hints/placeholders.rs +++ b/crates/ide/src/inlay_hints/placeholders.rs @@ -20,7 +20,7 @@ pub(super) fn type_hints( display_target: DisplayTarget, placeholder: InferType, ) -> Option<()> { - if !config.type_hints { + if !config.type_hints || config.hide_inferred_type_hints { return None; } @@ -73,4 +73,22 @@ fn foo() { "#, ); } + + #[test] + fn hide_inferred_types() { + check_with_config( + InlayHintsConfig { + type_hints: true, + hide_inferred_type_hints: true, + ..DISABLED_CONFIG + }, + r#" +struct S(T); + +fn foo() { + let t: (_, _, [_; _]) = (1_u32, S(2), [false] as _); +} + "#, + ); + } } diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 0cf2e15bc6f0..f3cda9156bbb 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -183,6 +183,7 @@ impl StaticIndex<'_> { adjustment_hints_hide_outside_unsafe: false, implicit_drop_hints: false, implied_dyn_trait_hints: false, + hide_inferred_type_hints: false, hide_named_constructor_hints: false, hide_closure_initialization_hints: false, hide_closure_parameter_hints: false, diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index ed0b64668559..bd3f05977ef0 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -1222,6 +1222,7 @@ impl flags::AnalysisStats { implied_dyn_trait_hints: true, lifetime_elision_hints: ide::LifetimeElisionHints::Always, param_names_for_lifetime_elision_hints: true, + hide_inferred_type_hints: false, hide_named_constructor_hints: false, hide_closure_initialization_hints: false, hide_closure_parameter_hints: false, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index c380621d81a1..1a2ea97204aa 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -304,6 +304,9 @@ config_data! { /// Hide inlay parameter type hints for closures. inlayHints_typeHints_hideClosureParameter: bool = false, + /// Hide inlay type hints for inferred types. + inlayHints_typeHints_hideInferredTypes: bool = false, + /// Hide inlay type hints for constructors. inlayHints_typeHints_hideNamedConstructor: bool = false, @@ -1937,6 +1940,7 @@ impl Config { hide_named_constructor_hints: self .inlayHints_typeHints_hideNamedConstructor() .to_owned(), + hide_inferred_type_hints: self.inlayHints_typeHints_hideInferredTypes().to_owned(), hide_closure_initialization_hints: self .inlayHints_typeHints_hideClosureInitialization() .to_owned(), diff --git a/docs/book/src/configuration_generated.md b/docs/book/src/configuration_generated.md index fe1ea57c9b18..b36576b4bb20 100644 --- a/docs/book/src/configuration_generated.md +++ b/docs/book/src/configuration_generated.md @@ -1118,6 +1118,13 @@ Default: `false` Hide inlay parameter type hints for closures. +## rust-analyzer.inlayHints.typeHints.hideInferredTypes {#inlayHints.typeHints.hideInferredTypes} + +Default: `false` + +Hide inlay type hints for inferred types. + + ## rust-analyzer.inlayHints.typeHints.hideNamedConstructor {#inlayHints.typeHints.hideNamedConstructor} Default: `false` diff --git a/editors/code/package.json b/editors/code/package.json index 8475864427e1..abe85d6c9d72 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -2466,6 +2466,16 @@ } } }, + { + "title": "Inlay Hints", + "properties": { + "rust-analyzer.inlayHints.typeHints.hideInferredTypes": { + "markdownDescription": "Hide inlay type hints for inferred types.", + "default": false, + "type": "boolean" + } + } + }, { "title": "Inlay Hints", "properties": {