Auto merge of #12406 - harpsword:fix-add-inlayHints-closures-without-block, r=Veykril

fix: add an option to show inlay hint for return type of closures wit…

fix #12321
This commit is contained in:
bors 2022-05-30 11:36:55 +00:00
commit 9ceaff91d3
6 changed files with 84 additions and 16 deletions

View File

@ -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<usize>,
}
#[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(

View File

@ -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,

View File

@ -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,

View File

@ -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": [

View File

@ -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"`)::
+

View File

@ -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.",