Skip to content

Commit 673e2b1

Browse files
committed
feat: Implement lifetime elision hints
1 parent 890f98f commit 673e2b1

File tree

5 files changed

+242
-17
lines changed

5 files changed

+242
-17
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 220 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ide_db::{base_db::FileRange, famous_defs::FamousDefs, RootDatabase};
44
use itertools::Itertools;
55
use stdx::to_lower_snake_case;
66
use syntax::{
7-
ast::{self, AstNode, HasArgList, HasName, UnaryOp},
7+
ast::{self, AstNode, HasArgList, HasGenericParams, HasName, UnaryOp},
88
match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, SyntaxNode, TextRange, T,
99
};
1010

@@ -17,6 +17,7 @@ pub struct InlayHintsConfig {
1717
pub parameter_hints: bool,
1818
pub chaining_hints: bool,
1919
pub closure_return_type_hints: bool,
20+
pub lifetime_elision_hints: bool,
2021
pub hide_named_constructor_hints: bool,
2122
pub max_length: Option<usize>,
2223
}
@@ -27,6 +28,8 @@ pub enum InlayKind {
2728
ParameterHint,
2829
ClosureReturnTypeHint,
2930
ChainingHint,
31+
GenericParamListHint,
32+
LifetimeHint,
3033
}
3134

3235
#[derive(Debug)]
@@ -41,12 +44,16 @@ pub struct InlayHint {
4144
// rust-analyzer shows additional information inline with the source code.
4245
// Editors usually render this using read-only virtual text snippets interspersed with code.
4346
//
44-
// rust-analyzer shows hints for
47+
// rust-analyzer by default shows hints for
4548
//
4649
// * types of local variables
4750
// * names of function arguments
4851
// * types of chained expressions
4952
//
53+
// Optionally, one can enable additional hints for
54+
//
55+
// * return types of closure expressions with blocks
56+
//
5057
// **Note:** VS Code does not have native support for inlay hints https://github.com/microsoft/vscode/issues/16221[yet] and the hints are implemented using decorations.
5158
// This approach has limitations, the caret movement and bracket highlighting near the edges of the hint may be weird:
5259
// https://github.com/rust-analyzer/rust-analyzer/issues/1623[1], https://github.com/rust-analyzer/rust-analyzer/issues/3453[2].
@@ -108,11 +115,172 @@ fn hints(
108115
}
109116
_ => (),
110117
}
111-
} else if let Some(it) = ast::IdentPat::cast(node) {
118+
} else if let Some(it) = ast::IdentPat::cast(node.clone()) {
112119
bind_pat_hints(hints, sema, config, &it);
120+
} else if let Some(it) = ast::Fn::cast(node) {
121+
lifetime_hints(hints, config, it);
113122
}
114123
}
115124

125+
fn lifetime_hints(
126+
acc: &mut Vec<InlayHint>,
127+
config: &InlayHintsConfig,
128+
func: ast::Fn,
129+
) -> Option<()> {
130+
if !config.lifetime_elision_hints {
131+
return None;
132+
}
133+
let param_list = func.param_list()?;
134+
let generic_param_list = func.generic_param_list();
135+
let ret_type = func.ret_type();
136+
let self_param = param_list.self_param();
137+
138+
let mut allocated_lifetimes = vec![];
139+
let mut gen_name = {
140+
let mut iter = 'a'..;
141+
let allocated_lifetimes = &mut allocated_lifetimes;
142+
move || {
143+
if let Some(it) = iter.next() {
144+
allocated_lifetimes.push(SmolStr::from_iter(['\'', it]))
145+
}
146+
}
147+
};
148+
149+
let potential_lt_refs: Vec<_> = param_list
150+
.params()
151+
.filter_map(|it| {
152+
let ty = it.ty()?;
153+
// FIXME: look into the nested types here and check path types
154+
match ty {
155+
ast::Type::RefType(r) => Some(r),
156+
_ => None,
157+
}
158+
})
159+
.collect();
160+
161+
enum LifetimeKind {
162+
Elided,
163+
Named(SmolStr),
164+
Static,
165+
}
166+
167+
let fetch_lt_text = |lt: Option<ast::Lifetime>| match lt {
168+
Some(lt) => match lt.text().as_str() {
169+
"'_" => LifetimeKind::Elided,
170+
"'static" => LifetimeKind::Static,
171+
name => LifetimeKind::Named(name.into()),
172+
},
173+
None => LifetimeKind::Elided,
174+
};
175+
let is_elided = |lt: Option<ast::Lifetime>| match lt {
176+
Some(lt) => matches!(lt.text().as_str(), "'_"),
177+
None => true,
178+
};
179+
180+
// allocate names
181+
if let Some(self_param) = &self_param {
182+
if is_elided(self_param.lifetime()) {
183+
gen_name();
184+
}
185+
}
186+
potential_lt_refs.iter().for_each(|it| {
187+
// FIXME: look into the nested types here and check path types
188+
if is_elided(it.lifetime()) {
189+
gen_name();
190+
}
191+
});
192+
193+
// fetch output lifetime if elision rule applies
194+
195+
let output = if let Some(self_param) = &self_param {
196+
match fetch_lt_text(self_param.lifetime()) {
197+
LifetimeKind::Elided => allocated_lifetimes.get(0).cloned(),
198+
LifetimeKind::Named(name) => Some(name),
199+
LifetimeKind::Static => None,
200+
}
201+
} else {
202+
match potential_lt_refs.as_slice() {
203+
[r] => match fetch_lt_text(r.lifetime()) {
204+
LifetimeKind::Elided => allocated_lifetimes.get(0).cloned(),
205+
LifetimeKind::Named(name) => Some(name),
206+
LifetimeKind::Static => None,
207+
},
208+
[..] => None,
209+
}
210+
};
211+
212+
// apply hints
213+
214+
// apply output if required
215+
match (&output, ret_type) {
216+
(Some(output_lt), Some(r)) => {
217+
if let Some(ast::Type::RefType(t)) = r.ty() {
218+
if t.lifetime().is_none() {
219+
let amp = t.amp_token()?;
220+
acc.push(InlayHint {
221+
range: amp.text_range(),
222+
kind: InlayKind::LifetimeHint,
223+
label: output_lt.clone(),
224+
});
225+
}
226+
}
227+
}
228+
_ => (),
229+
}
230+
231+
let mut idx = if let Some(self_param) = &self_param {
232+
if is_elided(self_param.lifetime()) {
233+
if let Some(amp) = self_param.amp_token() {
234+
let lt = allocated_lifetimes[0].clone();
235+
acc.push(InlayHint {
236+
range: amp.text_range(),
237+
kind: InlayKind::LifetimeHint,
238+
label: lt,
239+
});
240+
}
241+
1
242+
} else {
243+
0
244+
}
245+
} else {
246+
0
247+
};
248+
249+
for p in potential_lt_refs.iter() {
250+
if is_elided(p.lifetime()) {
251+
let t = p.amp_token()?;
252+
let lt = allocated_lifetimes[idx].clone();
253+
acc.push(InlayHint { range: t.text_range(), kind: InlayKind::LifetimeHint, label: lt });
254+
idx += 1;
255+
}
256+
}
257+
258+
// generate generic param list things
259+
match (generic_param_list, allocated_lifetimes.as_slice()) {
260+
(_, []) => (),
261+
(Some(gpl), allocated_lifetimes) => {
262+
let angle_tok = gpl.l_angle_token()?;
263+
let is_empty = gpl.generic_params().next().is_none();
264+
acc.push(InlayHint {
265+
range: angle_tok.text_range(),
266+
kind: InlayKind::GenericParamListHint,
267+
label: format!(
268+
"{}{}",
269+
allocated_lifetimes.iter().format(", "),
270+
if is_empty { "" } else { ", " }
271+
)
272+
.into(),
273+
});
274+
}
275+
(None, allocated_lifetimes) => acc.push(InlayHint {
276+
range: func.name()?.syntax().text_range(),
277+
kind: InlayKind::GenericParamListHint,
278+
label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),
279+
}),
280+
}
281+
Some(())
282+
}
283+
116284
fn closure_ret_hints(
117285
acc: &mut Vec<InlayHint>,
118286
sema: &Semantics<RootDatabase>,
@@ -600,6 +768,7 @@ fn get_callable(
600768
mod tests {
601769
use expect_test::{expect, Expect};
602770
use ide_db::base_db::FileRange;
771+
use itertools::Itertools;
603772
use syntax::{TextRange, TextSize};
604773
use test_utils::extract_annotations;
605774

@@ -610,6 +779,7 @@ mod tests {
610779
type_hints: false,
611780
parameter_hints: false,
612781
chaining_hints: false,
782+
lifetime_elision_hints: false,
613783
hide_named_constructor_hints: false,
614784
closure_return_type_hints: false,
615785
max_length: None,
@@ -619,6 +789,7 @@ mod tests {
619789
parameter_hints: true,
620790
chaining_hints: true,
621791
closure_return_type_hints: true,
792+
lifetime_elision_hints: true,
622793
..DISABLED_CONFIG
623794
};
624795

@@ -648,10 +819,15 @@ mod tests {
648819
#[track_caller]
649820
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
650821
let (analysis, file_id) = fixture::file(ra_fixture);
651-
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
822+
let mut expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
652823
let inlay_hints = analysis.inlay_hints(&config, file_id, None).unwrap();
653-
let actual =
654-
inlay_hints.into_iter().map(|it| (it.range, it.label.to_string())).collect::<Vec<_>>();
824+
let actual = inlay_hints
825+
.into_iter()
826+
.map(|it| (it.range, it.label.to_string()))
827+
.sorted_by_key(|(range, _)| range.start())
828+
.collect::<Vec<_>>();
829+
expected.sort_by_key(|(range, _)| range.start());
830+
655831
assert_eq!(expected, actual, "\nExpected:\n{:#?}\n\nActual:\n{:#?}", expected, actual);
656832
}
657833

@@ -1825,4 +2001,42 @@ fn main() {
18252001
"#]],
18262002
);
18272003
}
2004+
2005+
#[test]
2006+
fn hints_sssin_attr_call() {
2007+
check(
2008+
r#"
2009+
fn empty() {}
2010+
2011+
fn no_gpl(a: &()) {}
2012+
//^^^^^^<'a>
2013+
// ^'a
2014+
fn empty_gpl<>(a: &()) {}
2015+
// ^'a ^'a
2016+
fn partial<'b>(a: &(), b: &'b ()) {}
2017+
// ^'a, $ ^'a
2018+
fn partial<'b>(a: &'b (), b: &()) {}
2019+
// ^'a, $ ^'a
2020+
2021+
fn single_ret(a: &()) -> &() {}
2022+
// ^^^^^^^^^^<'a>
2023+
// ^'a ^'a
2024+
fn full_mul(a: &(), b: &()) {}
2025+
// ^^^^^^^^<'a, 'b>
2026+
// ^'a ^'b
2027+
2028+
fn foo<'c>(a: &'c ()) -> &() {}
2029+
// ^'c
2030+
2031+
impl () {
2032+
fn foo(&self) -> &() {}
2033+
// ^^^<'a>
2034+
// ^'a ^'a
2035+
fn foo(&self, a: &()) -> &() {}
2036+
// ^^^<'a, 'b>
2037+
// ^'a ^'b ^'a$
2038+
}
2039+
"#,
2040+
);
2041+
}
18282042
}

crates/ide/src/static_index.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl StaticIndex<'_> {
110110
parameter_hints: true,
111111
chaining_hints: true,
112112
closure_return_type_hints: true,
113+
lifetime_elision_hints: false,
113114
hide_named_constructor_hints: false,
114115
max_length: Some(25),
115116
},

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ config_data! {
255255
inlayHints_chainingHints: bool = "true",
256256
/// Whether to show inlay type hints for return types of closures with blocks.
257257
inlayHints_closureReturnTypeHints: bool = "false",
258+
/// Whether to show inlay type hints for elided lifetimes in function signatures.
259+
inlayHints_lifetimeElisionHints: bool = "false",
258260
/// Whether to hide inlay hints for constructors.
259261
inlayHints_hideNamedConstructorHints: bool = "false",
260262

@@ -855,6 +857,7 @@ impl Config {
855857
parameter_hints: self.data.inlayHints_parameterHints,
856858
chaining_hints: self.data.inlayHints_chainingHints,
857859
closure_return_type_hints: self.data.inlayHints_closureReturnTypeHints,
860+
lifetime_elision_hints: self.data.inlayHints_lifetimeElisionHints,
858861
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
859862
max_length: self.data.inlayHints_maxLength,
860863
}

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

0 commit comments

Comments
 (0)