From 7da5b80f259a777a43ea4e208def4672cb0fb545 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 19 Mar 2022 20:12:14 +0100 Subject: [PATCH] Change skip trivial behaviour --- crates/ide/src/inlay_hints.rs | 78 ++++++++++++++++-------------- crates/rust-analyzer/src/config.rs | 8 +-- docs/user/generated_config.adoc | 4 +- editors/code/package.json | 6 +-- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 2a82f54315..13e7a0ac88 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -258,36 +258,29 @@ fn lifetime_hints( return None; } - let skip_due_trivial_single = config.lifetime_elision_hints - == LifetimeElisionHints::SkipTrivial - && (allocated_lifetimes.len() == 1) - && generic_param_list.as_ref().map_or(true, |it| it.lifetime_params().next().is_none()); - - if skip_due_trivial_single { - cov_mark::hit!(lifetime_hints_single); - return None; - } - // apply hints // apply output if required - match (&output, ret_type) { - (Some(output_lt), Some(r)) => { - if let Some(ty) = r.ty() { - walk_ty(&ty, &mut |ty| match ty { - ast::Type::RefType(ty) if ty.lifetime().is_none() => { - if let Some(amp) = ty.amp_token() { - acc.push(InlayHint { - range: amp.text_range(), - kind: InlayKind::LifetimeHint, - label: output_lt.clone(), - }); - } + let mut is_trivial = true; + if let (Some(output_lt), Some(r)) = (&output, ret_type) { + if let Some(ty) = r.ty() { + walk_ty(&ty, &mut |ty| match ty { + ast::Type::RefType(ty) if ty.lifetime().is_none() => { + if let Some(amp) = ty.amp_token() { + is_trivial = false; + acc.push(InlayHint { + range: amp.text_range(), + kind: InlayKind::LifetimeHint, + label: output_lt.clone(), + }); } - _ => (), - }) - } + } + _ => (), + }) } - _ => (), + } + + if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial { + return None; } let mut idx = match &self_param { @@ -2061,6 +2054,9 @@ fn nested_out(a: &()) -> & &X< &()>{} //^'0 ^'0 ^'0 ^'0 impl () { + fn foo(&self) {} + // ^^^<'0> + // ^'0 fn foo(&self) -> &() {} // ^^^<'0> // ^'0 ^'0 @@ -2085,26 +2081,36 @@ fn nested_in<'named>(named: & &X< &()>) {} } #[test] - fn hints_lifetimes_skingle_skip() { - cov_mark::check!(lifetime_hints_single); + fn hints_lifetimes_trivial_skip() { check_with_config( InlayHintsConfig { lifetime_elision_hints: LifetimeElisionHints::SkipTrivial, ..TEST_CONFIG }, r#" -fn single(a: &()) -> &() {} - -fn double(a: &(), b: &()) {} -// ^^^^^^<'0, '1> - // ^'0 ^'1 +fn no_gpl(a: &()) {} +fn empty_gpl<>(a: &()) {} +fn partial<'b>(a: &(), b: &'b ()) {} fn partial<'a>(a: &'a (), b: &()) {} - //^'0, $ ^'0 -fn partial2<'a>(a: &'a ()) -> &() {} - //^'a + +fn single_ret(a: &()) -> &() {} +// ^^^^^^^^^^<'0> + // ^'0 ^'0 +fn full_mul(a: &(), b: &()) {} + +fn foo<'c>(a: &'c ()) -> &() {} + // ^'c + +fn nested_in(a: & &X< &()>) {} +fn nested_out(a: &()) -> & &X< &()>{} +// ^^^^^^^^^^<'0> + //^'0 ^'0 ^'0 ^'0 impl () { + fn foo(&self) {} fn foo(&self) -> &() {} + // ^^^<'0> + // ^'0 ^'0 fn foo(&self, a: &()) -> &() {} // ^^^<'0, '1> // ^'0 ^'1 ^'0 diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index b6f1da17eb..3afbeac47c 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -258,8 +258,8 @@ config_data! { inlayHints_closureReturnTypeHints: bool = "false", /// Whether to show inlay type hints for elided lifetimes in function signatures. inlayHints_lifetimeElisionHints: LifetimeElisionDef = "\"never\"", - /// Whether to show prefer using parameter names as the name for elided lifetime hints. - inlayHints_paramNamesForLifetimeElisionHints: bool = "false", + /// Whether to prefer using parameter names as the name for elided lifetime hints if possible. + inlayHints_lifetimeElisionHints_useParameterNames: bool = "false", /// Whether to hide inlay hints for constructors. inlayHints_hideNamedConstructorHints: bool = "false", @@ -868,7 +868,7 @@ impl Config { hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints, param_names_for_lifetime_elision_hints: self .data - .inlayHints_paramNamesForLifetimeElisionHints, + .inlayHints_lifetimeElisionHints_useParameterNames, max_length: self.data.inlayHints_maxLength, } } @@ -1406,7 +1406,7 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json "enumDescriptions": [ "Always show lifetime elision hints.", "Never show lifetime elision hints.", - "Always show lifetime elision hints but skip them for trivial single input to output mapping." + "Only show lifetime elision hints if a return type is involved." ], }, _ => panic!("missing entry for {}: {}", ty, default), diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 5641031067..42f485b511 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -383,10 +383,10 @@ Whether to show inlay type hints for return types of closures with blocks. -- Whether to show inlay type hints for elided lifetimes in function signatures. -- -[[rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints]]rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints (default: `false`):: +[[rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames]]rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames (default: `false`):: + -- -Whether to show prefer using parameter names as the name for elided lifetime hints. +Whether to prefer using parameter names as the name for elided lifetime hints if possible. -- [[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`):: + diff --git a/editors/code/package.json b/editors/code/package.json index 06c6bcab9e..0343abd0a3 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -812,11 +812,11 @@ "enumDescriptions": [ "Always show lifetime elision hints.", "Never show lifetime elision hints.", - "Always show lifetime elision hints but skip them for trivial single input to output mapping." + "Only show lifetime elision hints if a return type is involved." ] }, - "rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints": { - "markdownDescription": "Whether to show prefer using parameter names as the name for elided lifetime hints.", + "rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames": { + "markdownDescription": "Whether to prefer using parameter names as the name for elided lifetime hints if possible.", "default": false, "type": "boolean" },