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:
commit
9ceaff91d3
@ -21,7 +21,7 @@ pub struct InlayHintsConfig {
|
|||||||
pub parameter_hints: bool,
|
pub parameter_hints: bool,
|
||||||
pub chaining_hints: bool,
|
pub chaining_hints: bool,
|
||||||
pub reborrow_hints: ReborrowHints,
|
pub reborrow_hints: ReborrowHints,
|
||||||
pub closure_return_type_hints: bool,
|
pub closure_return_type_hints: ClosureReturnTypeHints,
|
||||||
pub binding_mode_hints: bool,
|
pub binding_mode_hints: bool,
|
||||||
pub lifetime_elision_hints: LifetimeElisionHints,
|
pub lifetime_elision_hints: LifetimeElisionHints,
|
||||||
pub param_names_for_lifetime_elision_hints: bool,
|
pub param_names_for_lifetime_elision_hints: bool,
|
||||||
@ -31,6 +31,13 @@ pub struct InlayHintsConfig {
|
|||||||
pub closing_brace_hints_min_lines: Option<usize>,
|
pub closing_brace_hints_min_lines: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub enum ClosureReturnTypeHints {
|
||||||
|
Always,
|
||||||
|
WithBlock,
|
||||||
|
Never,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum LifetimeElisionHints {
|
pub enum LifetimeElisionHints {
|
||||||
Always,
|
Always,
|
||||||
@ -86,7 +93,7 @@ pub enum InlayTooltip {
|
|||||||
//
|
//
|
||||||
// Optionally, one can enable additional hints for
|
// Optionally, one can enable additional hints for
|
||||||
//
|
//
|
||||||
// * return types of closure expressions with blocks
|
// * return types of closure expressions
|
||||||
// * elided lifetimes
|
// * elided lifetimes
|
||||||
// * compiler inserted reborrows
|
// * compiler inserted reborrows
|
||||||
//
|
//
|
||||||
@ -460,7 +467,7 @@ fn closure_ret_hints(
|
|||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
closure: ast::ClosureExpr,
|
closure: ast::ClosureExpr,
|
||||||
) -> Option<()> {
|
) -> Option<()> {
|
||||||
if !config.closure_return_type_hints {
|
if config.closure_return_type_hints == ClosureReturnTypeHints::Never {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,7 +475,9 @@ fn closure_ret_hints(
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !closure_has_block_body(&closure) {
|
if !closure_has_block_body(&closure)
|
||||||
|
&& config.closure_return_type_hints == ClosureReturnTypeHints::WithBlock
|
||||||
|
{
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1092,13 +1101,15 @@ mod tests {
|
|||||||
use crate::inlay_hints::ReborrowHints;
|
use crate::inlay_hints::ReborrowHints;
|
||||||
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
|
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
|
||||||
|
|
||||||
|
use super::ClosureReturnTypeHints;
|
||||||
|
|
||||||
const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
|
const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
|
||||||
render_colons: false,
|
render_colons: false,
|
||||||
type_hints: false,
|
type_hints: false,
|
||||||
parameter_hints: false,
|
parameter_hints: false,
|
||||||
chaining_hints: false,
|
chaining_hints: false,
|
||||||
lifetime_elision_hints: LifetimeElisionHints::Never,
|
lifetime_elision_hints: LifetimeElisionHints::Never,
|
||||||
closure_return_type_hints: false,
|
closure_return_type_hints: ClosureReturnTypeHints::Never,
|
||||||
reborrow_hints: ReborrowHints::Always,
|
reborrow_hints: ReborrowHints::Always,
|
||||||
binding_mode_hints: false,
|
binding_mode_hints: false,
|
||||||
hide_named_constructor_hints: false,
|
hide_named_constructor_hints: false,
|
||||||
@ -1112,7 +1123,7 @@ mod tests {
|
|||||||
parameter_hints: true,
|
parameter_hints: true,
|
||||||
chaining_hints: true,
|
chaining_hints: true,
|
||||||
reborrow_hints: ReborrowHints::Always,
|
reborrow_hints: ReborrowHints::Always,
|
||||||
closure_return_type_hints: true,
|
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
|
||||||
binding_mode_hints: true,
|
binding_mode_hints: true,
|
||||||
lifetime_elision_hints: LifetimeElisionHints::Always,
|
lifetime_elision_hints: LifetimeElisionHints::Always,
|
||||||
..DISABLED_CONFIG
|
..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]
|
#[test]
|
||||||
fn skip_closure_type_hints() {
|
fn skip_closure_type_hints() {
|
||||||
check_with_config(
|
check_with_config(
|
||||||
|
@ -81,7 +81,8 @@ pub use crate::{
|
|||||||
highlight_related::{HighlightRelatedConfig, HighlightedRange},
|
highlight_related::{HighlightRelatedConfig, HighlightedRange},
|
||||||
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
|
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
|
||||||
inlay_hints::{
|
inlay_hints::{
|
||||||
InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, ReborrowHints,
|
ClosureReturnTypeHints, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
|
||||||
|
LifetimeElisionHints, ReborrowHints,
|
||||||
},
|
},
|
||||||
join_lines::JoinLinesConfig,
|
join_lines::JoinLinesConfig,
|
||||||
markup::Markup,
|
markup::Markup,
|
||||||
|
@ -109,7 +109,7 @@ impl StaticIndex<'_> {
|
|||||||
type_hints: true,
|
type_hints: true,
|
||||||
parameter_hints: true,
|
parameter_hints: true,
|
||||||
chaining_hints: true,
|
chaining_hints: true,
|
||||||
closure_return_type_hints: true,
|
closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
|
||||||
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
|
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
|
||||||
reborrow_hints: crate::ReborrowHints::Never,
|
reborrow_hints: crate::ReborrowHints::Never,
|
||||||
hide_named_constructor_hints: false,
|
hide_named_constructor_hints: false,
|
||||||
|
@ -264,8 +264,8 @@ config_data! {
|
|||||||
/// Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
|
/// Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
|
||||||
/// to always show them).
|
/// to always show them).
|
||||||
inlayHints_closingBraceHints_minLines: usize = "25",
|
inlayHints_closingBraceHints_minLines: usize = "25",
|
||||||
/// Whether to show inlay type hints for return types of closures with blocks.
|
/// Whether to show inlay type hints for return types of closures.
|
||||||
inlayHints_closureReturnTypeHints_enable: bool = "false",
|
inlayHints_closureReturnTypeHints_enable: ClosureReturnTypeHintsDef = "\"never\"",
|
||||||
/// Whether to show inlay type hints for elided lifetimes in function signatures.
|
/// Whether to show inlay type hints for elided lifetimes in function signatures.
|
||||||
inlayHints_lifetimeElisionHints_enable: LifetimeElisionDef = "\"never\"",
|
inlayHints_lifetimeElisionHints_enable: LifetimeElisionDef = "\"never\"",
|
||||||
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
|
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
|
||||||
@ -1014,7 +1014,11 @@ impl Config {
|
|||||||
type_hints: self.data.inlayHints_typeHints_enable,
|
type_hints: self.data.inlayHints_typeHints_enable,
|
||||||
parameter_hints: self.data.inlayHints_parameterHints_enable,
|
parameter_hints: self.data.inlayHints_parameterHints_enable,
|
||||||
chaining_hints: self.data.inlayHints_chainingHints_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 {
|
lifetime_elision_hints: match self.data.inlayHints_lifetimeElisionHints_enable {
|
||||||
LifetimeElisionDef::Always => ide::LifetimeElisionHints::Always,
|
LifetimeElisionDef::Always => ide::LifetimeElisionHints::Always,
|
||||||
LifetimeElisionDef::Never => ide::LifetimeElisionHints::Never,
|
LifetimeElisionDef::Never => ide::LifetimeElisionHints::Never,
|
||||||
@ -1342,6 +1346,7 @@ mod de_unit_v {
|
|||||||
named_unit_variant!(all);
|
named_unit_variant!(all);
|
||||||
named_unit_variant!(skip_trivial);
|
named_unit_variant!(skip_trivial);
|
||||||
named_unit_variant!(mutable);
|
named_unit_variant!(mutable);
|
||||||
|
named_unit_variant!(with_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone, Copy)]
|
#[derive(Deserialize, Debug, Clone, Copy)]
|
||||||
@ -1454,6 +1459,17 @@ enum LifetimeElisionDef {
|
|||||||
SkipTrivial,
|
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)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
enum ReborrowHintsDef {
|
enum ReborrowHintsDef {
|
||||||
@ -1740,6 +1756,19 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
|
|||||||
"Only show lifetime elision hints if a return type is involved."
|
"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! {
|
"ReborrowHintsDef" => set! {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": [
|
"enum": [
|
||||||
|
@ -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
|
Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
|
||||||
to always show them).
|
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"`)::
|
[[rust-analyzer.inlayHints.lifetimeElisionHints.enable]]rust-analyzer.inlayHints.lifetimeElisionHints.enable (default: `"never"`)::
|
||||||
+
|
+
|
||||||
|
@ -811,9 +811,19 @@
|
|||||||
"minimum": 0
|
"minimum": 0
|
||||||
},
|
},
|
||||||
"rust-analyzer.inlayHints.closureReturnTypeHints.enable": {
|
"rust-analyzer.inlayHints.closureReturnTypeHints.enable": {
|
||||||
"markdownDescription": "Whether to show inlay type hints for return types of closures with blocks.",
|
"markdownDescription": "Whether to show inlay type hints for return types of closures.",
|
||||||
"default": false,
|
"default": "never",
|
||||||
"type": "boolean"
|
"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": {
|
"rust-analyzer.inlayHints.lifetimeElisionHints.enable": {
|
||||||
"markdownDescription": "Whether to show inlay type hints for elided lifetimes in function signatures.",
|
"markdownDescription": "Whether to show inlay type hints for elided lifetimes in function signatures.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user