Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7315d97

Browse files
bors[bot]Veykril
andauthored
11755: feat: Implement lifetime elision hints r=Veykril a=Veykril With names on: ![Code_erl26zKvuf](https://user-images.githubusercontent.com/3757771/159134856-e2c75d2d-f17c-45c7-9a78-3da5ee8b1acd.png) With names off: ![Code_MRP1Pbfe9d](https://user-images.githubusercontent.com/3757771/159134857-30fac3a1-825e-4f49-ba9b-9fa0bb215694.png) Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 85311a8 + 7da5b80 commit 7315d97

File tree

8 files changed

+432
-65
lines changed

8 files changed

+432
-65
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 336 additions & 43 deletions
Large diffs are not rendered by default.

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub use crate::{
8080
folding_ranges::{Fold, FoldKind},
8181
highlight_related::{HighlightRelatedConfig, HighlightedRange},
8282
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
83-
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind},
83+
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints},
8484
join_lines::JoinLinesConfig,
8585
markup::Markup,
8686
moniker::{MonikerKind, MonikerResult, PackageInformation},

crates/ide/src/static_index.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ use ide_db::{
1212
use rustc_hash::FxHashSet;
1313
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};
1414

15-
use crate::moniker::{crate_for_file, def_to_moniker, MonikerResult};
1615
use crate::{
1716
hover::hover_for_definition, Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult,
1817
InlayHint, InlayHintsConfig, TryToNav,
1918
};
19+
use crate::{
20+
moniker::{crate_for_file, def_to_moniker, MonikerResult},
21+
LifetimeElisionHints,
22+
};
2023

2124
/// A static representation of fully analyzed source code.
2225
///
@@ -110,7 +113,9 @@ impl StaticIndex<'_> {
110113
parameter_hints: true,
111114
chaining_hints: true,
112115
closure_return_type_hints: true,
116+
lifetime_elision_hints: LifetimeElisionHints::Never,
113117
hide_named_constructor_hints: false,
118+
param_names_for_lifetime_elision_hints: false,
114119
max_length: Some(25),
115120
},
116121
file_id,

crates/rust-analyzer/src/config.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use std::{ffi::OsString, iter, path::PathBuf};
1212
use flycheck::FlycheckConfig;
1313
use ide::{
1414
AssistConfig, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, HighlightRelatedConfig,
15-
HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope,
15+
HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, LifetimeElisionHints, Snippet,
16+
SnippetScope,
1617
};
1718
use ide_db::{
1819
imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind},
@@ -243,20 +244,24 @@ config_data! {
243244
hoverActions_run: bool = "true",
244245

245246
/// Whether to render trailing colons for parameter hints, and trailing colons for parameter hints.
246-
inlayHints_renderColons: bool = "true",
247+
inlayHints_renderColons: bool = "true",
247248
/// Maximum length for inlay hints. Set to null to have an unlimited length.
248-
inlayHints_maxLength: Option<usize> = "25",
249+
inlayHints_maxLength: Option<usize> = "25",
249250
/// Whether to show function parameter name inlay hints at the call
250251
/// site.
251-
inlayHints_parameterHints: bool = "true",
252+
inlayHints_parameterHints: bool = "true",
252253
/// Whether to show inlay type hints for variables.
253-
inlayHints_typeHints: bool = "true",
254+
inlayHints_typeHints: bool = "true",
254255
/// Whether to show inlay type hints for method chains.
255-
inlayHints_chainingHints: bool = "true",
256+
inlayHints_chainingHints: bool = "true",
256257
/// Whether to show inlay type hints for return types of closures with blocks.
257-
inlayHints_closureReturnTypeHints: bool = "false",
258+
inlayHints_closureReturnTypeHints: bool = "false",
259+
/// Whether to show inlay type hints for elided lifetimes in function signatures.
260+
inlayHints_lifetimeElisionHints: LifetimeElisionDef = "\"never\"",
261+
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
262+
inlayHints_lifetimeElisionHints_useParameterNames: bool = "false",
258263
/// Whether to hide inlay hints for constructors.
259-
inlayHints_hideNamedConstructorHints: bool = "false",
264+
inlayHints_hideNamedConstructorHints: bool = "false",
260265

261266
/// Join lines inserts else between consecutive ifs.
262267
joinLines_joinElseIf: bool = "true",
@@ -855,7 +860,15 @@ impl Config {
855860
parameter_hints: self.data.inlayHints_parameterHints,
856861
chaining_hints: self.data.inlayHints_chainingHints,
857862
closure_return_type_hints: self.data.inlayHints_closureReturnTypeHints,
863+
lifetime_elision_hints: match self.data.inlayHints_lifetimeElisionHints {
864+
LifetimeElisionDef::Always => LifetimeElisionHints::Always,
865+
LifetimeElisionDef::Never => LifetimeElisionHints::Never,
866+
LifetimeElisionDef::SkipTrivial => LifetimeElisionHints::SkipTrivial,
867+
},
858868
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
869+
param_names_for_lifetime_elision_hints: self
870+
.data
871+
.inlayHints_lifetimeElisionHints_useParameterNames,
859872
max_length: self.data.inlayHints_maxLength,
860873
}
861874
}
@@ -1125,6 +1138,16 @@ enum ImportGranularityDef {
11251138
Module,
11261139
}
11271140

1141+
#[derive(Deserialize, Debug, Clone)]
1142+
#[serde(rename_all = "snake_case")]
1143+
enum LifetimeElisionDef {
1144+
#[serde(alias = "true")]
1145+
Always,
1146+
#[serde(alias = "false")]
1147+
Never,
1148+
SkipTrivial,
1149+
}
1150+
11281151
#[derive(Deserialize, Debug, Clone)]
11291152
#[serde(rename_all = "snake_case")]
11301153
enum ImportPrefixDef {
@@ -1377,7 +1400,16 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
13771400
"minimum": 0,
13781401
"maximum": 255
13791402
},
1380-
_ => panic!("{}: {}", ty, default),
1403+
"LifetimeElisionDef" => set! {
1404+
"type": "string",
1405+
"enum": ["always", "never", "skip_trivial"],
1406+
"enumDescriptions": [
1407+
"Always show lifetime elision hints.",
1408+
"Never show lifetime elision hints.",
1409+
"Only show lifetime elision hints if a return type is involved."
1410+
],
1411+
},
1412+
_ => panic!("missing entry for {}: {}", ty, default),
13811413
}
13821414

13831415
map.into()

crates/rust-analyzer/src/to_proto.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,27 +427,34 @@ pub(crate) fn inlay_hint(
427427
}),
428428
position: match inlay_hint.kind {
429429
InlayKind::ParameterHint => position(line_index, inlay_hint.range.start()),
430-
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
431-
position(line_index, inlay_hint.range.end())
432-
}
430+
InlayKind::ClosureReturnTypeHint
431+
| InlayKind::TypeHint
432+
| InlayKind::ChainingHint
433+
| InlayKind::GenericParamListHint
434+
| InlayKind::LifetimeHint => position(line_index, inlay_hint.range.end()),
433435
},
434436
kind: match inlay_hint.kind {
435437
InlayKind::ParameterHint => Some(lsp_ext::InlayHintKind::PARAMETER),
436438
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
437439
Some(lsp_ext::InlayHintKind::TYPE)
438440
}
441+
InlayKind::GenericParamListHint | InlayKind::LifetimeHint => None,
439442
},
440443
tooltip: None,
441444
padding_left: Some(match inlay_hint.kind {
442445
InlayKind::TypeHint => !render_colons,
443446
InlayKind::ParameterHint | InlayKind::ClosureReturnTypeHint => false,
444447
InlayKind::ChainingHint => true,
448+
InlayKind::GenericParamListHint => false,
449+
InlayKind::LifetimeHint => false,
445450
}),
446451
padding_right: Some(match inlay_hint.kind {
447452
InlayKind::TypeHint | InlayKind::ChainingHint | InlayKind::ClosureReturnTypeHint => {
448453
false
449454
}
450455
InlayKind::ParameterHint => true,
456+
InlayKind::LifetimeHint => true,
457+
InlayKind::GenericParamListHint => false,
451458
}),
452459
}
453460
}

crates/test_utils/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ fn extract_line_annotations(mut line: &str) -> Vec<LineAnnotation> {
306306
let end_marker = line_no_caret.find(|c| c == '$');
307307
let next = line_no_caret.find(marker).map_or(line.len(), |it| it + len);
308308

309-
let mut content = match end_marker {
310-
Some(end_marker)
311-
if end_marker < next
312-
&& line_no_caret[end_marker..]
309+
let cond = |end_marker| {
310+
end_marker < next
311+
&& (line_no_caret[end_marker + 1..].is_empty()
312+
|| line_no_caret[end_marker + 1..]
313313
.strip_prefix(|c: char| c.is_whitespace() || c == '^')
314-
.is_some() =>
315-
{
316-
&line_no_caret[..end_marker]
317-
}
314+
.is_some())
315+
};
316+
let mut content = match end_marker {
317+
Some(end_marker) if cond(end_marker) => &line_no_caret[..end_marker],
318318
_ => line_no_caret[..next - len].trim_end(),
319319
};
320320

docs/user/generated_config.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ Whether to show inlay type hints for method chains.
378378
--
379379
Whether to show inlay type hints for return types of closures with blocks.
380380
--
381+
[[rust-analyzer.inlayHints.lifetimeElisionHints]]rust-analyzer.inlayHints.lifetimeElisionHints (default: `"never"`)::
382+
+
383+
--
384+
Whether to show inlay type hints for elided lifetimes in function signatures.
385+
--
386+
[[rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames]]rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames (default: `false`)::
387+
+
388+
--
389+
Whether to prefer using parameter names as the name for elided lifetime hints if possible.
390+
--
381391
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
382392
+
383393
--

editors/code/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,26 @@
795795
"default": false,
796796
"type": "boolean"
797797
},
798+
"rust-analyzer.inlayHints.lifetimeElisionHints": {
799+
"markdownDescription": "Whether to show inlay type hints for elided lifetimes in function signatures.",
800+
"default": "never",
801+
"type": "string",
802+
"enum": [
803+
"always",
804+
"never",
805+
"skip_trivial"
806+
],
807+
"enumDescriptions": [
808+
"Always show lifetime elision hints.",
809+
"Never show lifetime elision hints.",
810+
"Only show lifetime elision hints if a return type is involved."
811+
]
812+
},
813+
"rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames": {
814+
"markdownDescription": "Whether to prefer using parameter names as the name for elided lifetime hints if possible.",
815+
"default": false,
816+
"type": "boolean"
817+
},
798818
"rust-analyzer.inlayHints.hideNamedConstructorHints": {
799819
"markdownDescription": "Whether to hide inlay hints for constructors.",
800820
"default": false,

0 commit comments

Comments
 (0)