1 //! Completes lifetimes and labels.
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 {
12 let param_lifetime = match (
14 ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
16 (Some(lt), Some(lp)) if lp == lt.clone() => return,
17 (Some(_), Some(lp)) => {
18 lp_string = lp.to_string();
24 ctx.scope.process_all_names(&mut |name, res| {
25 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
26 let name = name.to_string();
27 if param_lifetime != Some(&name) {
28 acc.add_resolution(ctx, name, &res);
32 if param_lifetime.is_none() {
33 acc.add_static_lifetime(ctx);
38 pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
39 if !ctx.is_label_ref {
42 ctx.scope.process_all_names(&mut |name, res| {
43 if let ScopeDef::Label(_) = res {
44 acc.add_resolution(ctx, name.to_string(), &res);
51 use expect_test::{expect, Expect};
54 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
55 CompletionConfig, CompletionKind,
58 fn check(ra_fixture: &str, expect: Expect) {
59 check_with_config(TEST_CONFIG, ra_fixture, expect);
62 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
63 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
64 expect.assert_eq(&actual)
68 fn check_lifetime_edit() {
72 fn func<'lifetime>(foo: &'li$0) {}
75 fn func<'lifetime>(foo: &'lifetime) {}
78 cov_mark::check!(completes_if_lifetime_without_idents);
82 fn func<'lifetime>(foo: &'$0) {}
85 fn func<'lifetime>(foo: &'lifetime) {}
91 fn complete_lifetime_in_ref() {
94 fn foo<'lifetime>(foo: &'a$0 usize) {}
104 fn complete_lifetime_in_ref_missing_ty() {
107 fn foo<'lifetime>(foo: &'a$0) {}
116 fn complete_lifetime_in_self_ref() {
121 fn foo<'func>(&'a$0 self) {}
133 fn complete_lifetime_in_arg_list() {
137 fn foo<'lifetime>(_: Foo<'a$0>) {}
147 fn complete_lifetime_in_where_pred() {
150 fn foo2<'lifetime, T>() where 'a$0 {}
160 fn complete_lifetime_in_ty_bound() {
163 fn foo2<'lifetime, T>() where T: 'a$0 {}
172 fn foo2<'lifetime, T>() where T: Trait<'a$0> {}
182 fn dont_complete_lifetime_in_assoc_ty_bound() {
185 fn foo2<'lifetime, T>() where T: Trait<Item = 'a$0> {}
192 fn complete_lifetime_in_param_list() {
201 fn foo<'footime, 'lifetime: 'a$0>() {}
210 fn check_label_edit() {
231 fn complete_label_in_loop() {
259 fn complete_label_in_block_nested() {
278 fn complete_label_in_loop_with_value() {
294 fn complete_label_in_while_cond() {
298 'outer: while { 'inner: loop { break '$0 } } {}
309 fn complete_label_in_for_iterable() {
313 'outer: for _ in [{ 'inner: loop { break '$0 } }] {}