1 //! Implementation of "lifetime elision" inlay hints:
3 //! fn example/* <'0> */(a: &/* '0 */()) {}
5 use ide_db::{syntax_helpers::node_ext::walk_ty, FxHashMap};
6 use itertools::Itertools;
9 ast::{self, AstNode, HasGenericParams, HasName},
13 use crate::{InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints};
16 acc: &mut Vec<InlayHint>,
17 config: &InlayHintsConfig,
20 if config.lifetime_elision_hints == LifetimeElisionHints::Never {
24 let mk_lt_hint = |t: SyntaxToken, label: String| InlayHint {
25 range: t.text_range(),
26 kind: InlayKind::LifetimeHint,
28 tooltip: Some(InlayTooltip::String("Elided lifetime".into())),
31 let param_list = func.param_list()?;
32 let generic_param_list = func.generic_param_list();
33 let ret_type = func.ret_type();
34 let self_param = param_list.self_param().filter(|it| it.amp_token().is_some());
36 let is_elided = |lt: &Option<ast::Lifetime>| match lt {
37 Some(lt) => matches!(lt.text().as_str(), "'_"),
41 let potential_lt_refs = {
42 let mut acc: Vec<_> = vec![];
43 if let Some(self_param) = &self_param {
44 let lifetime = self_param.lifetime();
45 let is_elided = is_elided(&lifetime);
46 acc.push((None, self_param.amp_token(), lifetime, is_elided));
48 param_list.params().filter_map(|it| Some((it.pat(), it.ty()?))).for_each(|(pat, ty)| {
49 // FIXME: check path types
50 walk_ty(&ty, &mut |ty| match ty {
51 ast::Type::RefType(r) => {
52 let lifetime = r.lifetime();
53 let is_elided = is_elided(&lifetime);
55 pat.as_ref().and_then(|it| match it {
56 ast::Pat::IdentPat(p) => p.name(),
71 let mut gen_idx_name = {
72 let mut gen = (0u8..).map(|idx| match idx {
73 idx if idx < 10 => SmolStr::from_iter(['\'', (idx + 48) as char]),
74 idx => format!("'{idx}").into(),
76 move || gen.next().unwrap_or_default()
78 let mut allocated_lifetimes = vec![];
80 let mut used_names: FxHashMap<SmolStr, usize> =
81 match config.param_names_for_lifetime_elision_hints {
82 true => generic_param_list
84 .flat_map(|gpl| gpl.lifetime_params())
85 .filter_map(|param| param.lifetime())
86 .filter_map(|lt| Some((SmolStr::from(lt.text().as_str().get(1..)?), 0)))
88 false => Default::default(),
91 let mut potential_lt_refs = potential_lt_refs.iter().filter(|&&(.., is_elided)| is_elided);
92 if let Some(_) = &self_param {
93 if let Some(_) = potential_lt_refs.next() {
94 allocated_lifetimes.push(if config.param_names_for_lifetime_elision_hints {
95 // self can't be used as a lifetime, so no need to check for collisions
102 potential_lt_refs.for_each(|(name, ..)| {
103 let name = match name {
104 Some(it) if config.param_names_for_lifetime_elision_hints => {
105 if let Some(c) = used_names.get_mut(it.text().as_str()) {
107 SmolStr::from(format!("'{text}{c}", text = it.text().as_str()))
109 used_names.insert(it.text().as_str().into(), 0);
110 SmolStr::from_iter(["\'", it.text().as_str()])
115 allocated_lifetimes.push(name);
119 // fetch output lifetime if elision rule applies
120 let output = match potential_lt_refs.as_slice() {
121 [(_, _, lifetime, _), ..] if self_param.is_some() || potential_lt_refs.len() == 1 => {
123 Some(lt) => match lt.text().as_str() {
124 "'_" => allocated_lifetimes.get(0).cloned(),
126 name => Some(name.into()),
128 None => allocated_lifetimes.get(0).cloned(),
134 if allocated_lifetimes.is_empty() && output.is_none() {
139 // apply output if required
140 let mut is_trivial = true;
141 if let (Some(output_lt), Some(r)) = (&output, ret_type) {
142 if let Some(ty) = r.ty() {
143 walk_ty(&ty, &mut |ty| match ty {
144 ast::Type::RefType(ty) if ty.lifetime().is_none() => {
145 if let Some(amp) = ty.amp_token() {
147 acc.push(mk_lt_hint(amp, output_lt.to_string()));
155 if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial {
159 let mut a = allocated_lifetimes.iter();
160 for (_, amp_token, _, is_elided) in potential_lt_refs {
164 acc.push(mk_lt_hint(t, lt.to_string()));
168 // generate generic param list things
169 match (generic_param_list, allocated_lifetimes.as_slice()) {
171 (Some(gpl), allocated_lifetimes) => {
172 let angle_tok = gpl.l_angle_token()?;
173 let is_empty = gpl.generic_params().next().is_none();
175 range: angle_tok.text_range(),
176 kind: InlayKind::LifetimeHint,
179 allocated_lifetimes.iter().format(", "),
180 if is_empty { "" } else { ", " }
183 tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
186 (None, allocated_lifetimes) => acc.push(InlayHint {
187 range: func.name()?.syntax().text_range(),
188 kind: InlayKind::GenericParamListHint,
189 label: format!("<{}>", allocated_lifetimes.iter().format(", "),).into(),
190 tooltip: Some(InlayTooltip::String("Elided lifetimes".into())),
199 inlay_hints::tests::{check, check_with_config, TEST_CONFIG},
200 InlayHintsConfig, LifetimeElisionHints,
204 fn hints_lifetimes() {
212 fn empty_gpl<>(a: &()) {}
214 fn partial<'b>(a: &(), b: &'b ()) {}
216 fn partial<'a>(a: &'a (), b: &()) {}
219 fn single_ret(a: &()) -> &() {}
222 fn full_mul(a: &(), b: &()) {}
226 fn foo<'c>(a: &'c ()) -> &() {}
229 fn nested_in(a: & &X< &()>) {}
230 // ^^^^^^^^^<'0, '1, '2>
232 fn nested_out(a: &()) -> & &X< &()>{}
240 fn foo(&self) -> &() {}
243 fn foo(&self, a: &()) -> &() {}
252 fn hints_lifetimes_named() {
254 InlayHintsConfig { param_names_for_lifetime_elision_hints: true, ..TEST_CONFIG },
256 fn nested_in<'named>(named: & &X< &()>) {}
257 // ^'named1, 'named2, 'named3, $
258 //^'named1 ^'named2 ^'named3
264 fn hints_lifetimes_trivial_skip() {
267 lifetime_elision_hints: LifetimeElisionHints::SkipTrivial,
272 fn empty_gpl<>(a: &()) {}
273 fn partial<'b>(a: &(), b: &'b ()) {}
274 fn partial<'a>(a: &'a (), b: &()) {}
276 fn single_ret(a: &()) -> &() {}
279 fn full_mul(a: &(), b: &()) {}
281 fn foo<'c>(a: &'c ()) -> &() {}
284 fn nested_in(a: & &X< &()>) {}
285 fn nested_out(a: &()) -> & &X< &()>{}
291 fn foo(&self) -> &() {}
294 fn foo(&self, a: &()) -> &() {}