From 55509548e8e1679fca623f14802289ae365075f4 Mon Sep 17 00:00:00 2001 From: harpsword Date: Sat, 28 May 2022 20:13:25 +0800 Subject: [PATCH] fix: add an option to show inlay hint for return type of closures without block --- crates/ide/src/inlay_hints.rs | 40 +++++++++++++++++++++++++----- crates/ide/src/lib.rs | 3 ++- crates/ide/src/static_index.rs | 2 +- crates/rust-analyzer/src/config.rs | 35 +++++++++++++++++++++++--- docs/user/generated_config.adoc | 4 +-- editors/code/package.json | 16 +++++++++--- 6 files changed, 84 insertions(+), 16 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 3689bd5a42a..910aee7b05c 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -21,7 +21,7 @@ pub struct InlayHintsConfig { pub parameter_hints: bool, pub chaining_hints: bool, pub reborrow_hints: ReborrowHints, - pub closure_return_type_hints: bool, + pub closure_return_type_hints: ClosureReturnTypeHints, pub binding_mode_hints: bool, pub lifetime_elision_hints: LifetimeElisionHints, pub param_names_for_lifetime_elision_hints: bool, @@ -31,6 +31,13 @@ pub struct InlayHintsConfig { pub closing_brace_hints_min_lines: Option, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ClosureReturnTypeHints { + Always, + WithBlock, + Never, +} + #[derive(Clone, Debug, PartialEq, Eq)] pub enum LifetimeElisionHints { Always, @@ -86,7 +93,7 @@ pub enum InlayTooltip { // // Optionally, one can enable additional hints for // -// * return types of closure expressions with blocks +// * return types of closure expressions // * elided lifetimes // * compiler inserted reborrows // @@ -460,7 +467,7 @@ fn closure_ret_hints( file_id: FileId, closure: ast::ClosureExpr, ) -> Option<()> { - if !config.closure_return_type_hints { + if config.closure_return_type_hints == ClosureReturnTypeHints::Never { return None; } @@ -468,7 +475,9 @@ fn closure_ret_hints( return None; } - if !closure_has_block_body(&closure) { + if !closure_has_block_body(&closure) + && config.closure_return_type_hints == ClosureReturnTypeHints::WithBlock + { return None; } @@ -1092,13 +1101,15 @@ mod tests { use crate::inlay_hints::ReborrowHints; use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints}; + use super::ClosureReturnTypeHints; + const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig { render_colons: false, type_hints: false, parameter_hints: false, chaining_hints: false, lifetime_elision_hints: LifetimeElisionHints::Never, - closure_return_type_hints: false, + closure_return_type_hints: ClosureReturnTypeHints::Never, reborrow_hints: ReborrowHints::Always, binding_mode_hints: false, hide_named_constructor_hints: false, @@ -1112,7 +1123,7 @@ mod tests { parameter_hints: true, chaining_hints: true, reborrow_hints: ReborrowHints::Always, - closure_return_type_hints: true, + closure_return_type_hints: ClosureReturnTypeHints::WithBlock, binding_mode_hints: true, lifetime_elision_hints: LifetimeElisionHints::Always, ..DISABLED_CONFIG @@ -2054,6 +2065,23 @@ fn main() { ); } + #[test] + fn return_type_hints_for_closure_without_block() { + check_with_config( + InlayHintsConfig { + closure_return_type_hints: ClosureReturnTypeHints::Always, + ..DISABLED_CONFIG + }, + r#" +fn main() { + let a = || { 0 }; + //^^ i32 + let b = || 0; + //^^ i32 +}"#, + ); + } + #[test] fn skip_closure_type_hints() { check_with_config( diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index e02a6919203..20caf6246a4 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -81,7 +81,8 @@ macro_rules! eprintln { highlight_related::{HighlightRelatedConfig, HighlightedRange}, hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult}, inlay_hints::{ - InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, ReborrowHints, + ClosureReturnTypeHints, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, + LifetimeElisionHints, ReborrowHints, }, join_lines::JoinLinesConfig, markup::Markup, diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 006b18b7fdd..95a9635a065 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -109,7 +109,7 @@ fn add_file(&mut self, file_id: FileId) { type_hints: true, parameter_hints: true, chaining_hints: true, - closure_return_type_hints: true, + closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock, lifetime_elision_hints: crate::LifetimeElisionHints::Never, reborrow_hints: crate::ReborrowHints::Never, hide_named_constructor_hints: false, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index c347913fb2f..9fd19589999 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -264,8 +264,8 @@ struct ConfigData { /// Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1 /// to always show them). inlayHints_closingBraceHints_minLines: usize = "25", - /// Whether to show inlay type hints for return types of closures with blocks. - inlayHints_closureReturnTypeHints_enable: bool = "false", + /// Whether to show inlay type hints for return types of closures. + inlayHints_closureReturnTypeHints_enable: ClosureReturnTypeHintsDef = "\"never\"", /// Whether to show inlay type hints for elided lifetimes in function signatures. inlayHints_lifetimeElisionHints_enable: LifetimeElisionDef = "\"never\"", /// Whether to prefer using parameter names as the name for elided lifetime hints if possible. @@ -1014,7 +1014,11 @@ pub fn inlay_hints(&self) -> InlayHintsConfig { type_hints: self.data.inlayHints_typeHints_enable, parameter_hints: self.data.inlayHints_parameterHints_enable, chaining_hints: self.data.inlayHints_chainingHints_enable, - closure_return_type_hints: self.data.inlayHints_closureReturnTypeHints_enable, + closure_return_type_hints: match self.data.inlayHints_closureReturnTypeHints_enable { + ClosureReturnTypeHintsDef::Always => ide::ClosureReturnTypeHints::Always, + ClosureReturnTypeHintsDef::Never => ide::ClosureReturnTypeHints::Never, + ClosureReturnTypeHintsDef::WithBlock => ide::ClosureReturnTypeHints::WithBlock, + }, lifetime_elision_hints: match self.data.inlayHints_lifetimeElisionHints_enable { LifetimeElisionDef::Always => ide::LifetimeElisionHints::Always, LifetimeElisionDef::Never => ide::LifetimeElisionHints::Never, @@ -1342,6 +1346,7 @@ mod de_unit_v { named_unit_variant!(all); named_unit_variant!(skip_trivial); named_unit_variant!(mutable); + named_unit_variant!(with_block); } #[derive(Deserialize, Debug, Clone, Copy)] @@ -1454,6 +1459,17 @@ enum LifetimeElisionDef { SkipTrivial, } +#[derive(Deserialize, Debug, Clone)] +#[serde(untagged)] +enum ClosureReturnTypeHintsDef { + #[serde(deserialize_with = "true_or_always")] + Always, + #[serde(deserialize_with = "false_or_never")] + Never, + #[serde(deserialize_with = "de_unit_v::with_block")] + WithBlock, +} + #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] enum ReborrowHintsDef { @@ -1740,6 +1756,19 @@ macro_rules! set { "Only show lifetime elision hints if a return type is involved." ] }, + "ClosureReturnTypeHintsDef" => set! { + "type": "string", + "enum": [ + "always", + "never", + "with_block" + ], + "enumDescriptions": [ + "Always show type hints for return types of closures.", + "Never show type hints for return types of closures.", + "Only show type hints for return types of closures with blocks." + ] + }, "ReborrowHintsDef" => set! { "type": "string", "enum": [ diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 389124ed8bf..981132f9c4d 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -366,10 +366,10 @@ Whether to show inlay hints after a closing `}` to indicate what item it belongs Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1 to always show them). -- -[[rust-analyzer.inlayHints.closureReturnTypeHints.enable]]rust-analyzer.inlayHints.closureReturnTypeHints.enable (default: `false`):: +[[rust-analyzer.inlayHints.closureReturnTypeHints.enable]]rust-analyzer.inlayHints.closureReturnTypeHints.enable (default: `"never"`):: + -- -Whether to show inlay type hints for return types of closures with blocks. +Whether to show inlay type hints for return types of closures. -- [[rust-analyzer.inlayHints.lifetimeElisionHints.enable]]rust-analyzer.inlayHints.lifetimeElisionHints.enable (default: `"never"`):: + diff --git a/editors/code/package.json b/editors/code/package.json index 1c87f0ad82a..03ec18a5689 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -811,9 +811,19 @@ "minimum": 0 }, "rust-analyzer.inlayHints.closureReturnTypeHints.enable": { - "markdownDescription": "Whether to show inlay type hints for return types of closures with blocks.", - "default": false, - "type": "boolean" + "markdownDescription": "Whether to show inlay type hints for return types of closures.", + "default": "never", + "type": "string", + "enum": [ + "always", + "never", + "with_block" + ], + "enumDescriptions": [ + "Always show type hints for return types of closures.", + "Never show type hints for return types of closures.", + "Only show type hints for return types of closures with blocks." + ] }, "rust-analyzer.inlayHints.lifetimeElisionHints.enable": { "markdownDescription": "Whether to show inlay type hints for elided lifetimes in function signatures.",