1 //! Completes lifetimes.
4 use crate::{completions::Completions, context::CompletionContext};
6 /// Completes lifetimes.
7 pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) {
8 if !ctx.lifetime_allowed {
11 let param_lifetime = match (
13 ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
15 (Some(lt), Some(lp)) if lp == lt.clone() => return,
16 (Some(_), Some(lp)) => Some(lp.to_string()),
20 ctx.scope.process_all_names(&mut |name, res| {
21 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
22 if param_lifetime != Some(name.to_string()) {
23 acc.add_resolution(ctx, name.to_string(), &res);
27 if param_lifetime.is_none() {
28 acc.add_static_lifetime(ctx);
34 use expect_test::{expect, Expect};
37 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
38 CompletionConfig, CompletionKind,
41 fn check(ra_fixture: &str, expect: Expect) {
42 check_with_config(TEST_CONFIG, ra_fixture, expect);
45 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
46 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
47 expect.assert_eq(&actual)
51 fn check_lifetime_edit() {
55 fn func<'lifetime>(foo: &'li$0) {}
58 fn func<'lifetime>(foo: &'lifetime) {}
64 fn complete_lifetime_in_ref() {
67 fn foo<'lifetime>(foo: &'a$0 usize) {}
77 fn complete_lifetime_in_ref_missing_ty() {
80 fn foo<'lifetime>(foo: &'a$0) {}
89 fn complete_lifetime_in_self_ref() {
94 fn foo<'func>(&'a$0 self) {}
106 fn complete_lifetime_in_arg_list() {
110 fn foo<'lifetime>(_: Foo<'a$0>) {}
120 fn complete_lifetime_in_where_pred() {
123 fn foo2<'lifetime, T>() where 'a$0 {}
133 fn complete_lifetime_in_ty_bound() {
136 fn foo2<'lifetime, T>() where T: 'a$0 {}
145 fn foo2<'lifetime, T>() where T: Trait<'a$0> {}
155 fn dont_complete_lifetime_in_assoc_ty_bound() {
158 fn foo2<'lifetime, T>() where T: Trait<Item = 'a$0> {}
165 fn complete_lifetime_in_param_list() {
174 fn foo<'footime, 'lifetime: 'a$0>() {}