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 if param_lifetime != Some(&*name.to_string()) {
27 acc.add_resolution(ctx, name, &res);
31 if param_lifetime.is_none() {
32 acc.add_static_lifetime(ctx);
37 pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
38 if !ctx.is_label_ref {
41 ctx.scope.process_all_names(&mut |name, res| {
42 if let ScopeDef::Label(_) = res {
43 acc.add_resolution(ctx, name, &res);
50 use expect_test::{expect, Expect};
52 use crate::tests::{check_edit, completion_list};
54 fn check(ra_fixture: &str, expect: Expect) {
55 let actual = completion_list(ra_fixture);
56 expect.assert_eq(&actual);
60 fn check_lifetime_edit() {
64 fn func<'lifetime>(foo: &'li$0) {}
67 fn func<'lifetime>(foo: &'lifetime) {}
70 cov_mark::check!(completes_if_lifetime_without_idents);
74 fn func<'lifetime>(foo: &'$0) {}
77 fn func<'lifetime>(foo: &'lifetime) {}
83 fn complete_lifetime_in_ref() {
86 fn foo<'lifetime>(foo: &'a$0 usize) {}
96 fn complete_lifetime_in_ref_missing_ty() {
99 fn foo<'lifetime>(foo: &'a$0) {}
108 fn complete_lifetime_in_self_ref() {
113 fn foo<'func>(&'a$0 self) {}
125 fn complete_lifetime_in_arg_list() {
129 fn foo<'lifetime>(_: Foo<'a$0>) {}
139 fn complete_lifetime_in_where_pred() {
142 fn foo2<'lifetime, T>() where 'a$0 {}
152 fn complete_lifetime_in_ty_bound() {
155 fn foo2<'lifetime, T>() where T: 'a$0 {}
164 fn foo2<'lifetime, T>() where T: Trait<'a$0> {}
174 fn dont_complete_lifetime_in_assoc_ty_bound() {
177 fn foo2<'lifetime, T>() where T: Trait<Item = 'a$0> {}
184 fn complete_lifetime_in_param_list() {
193 fn foo<'footime, 'lifetime: 'a$0>() {}
202 fn check_label_edit() {
223 fn complete_label_in_loop() {
251 fn complete_label_in_block_nested() {
270 fn complete_label_in_loop_with_value() {
286 fn complete_label_in_while_cond() {
290 'outer: while { 'inner: loop { break '$0 } } {}
301 fn complete_label_in_for_iterable() {
305 'outer: for _ in [{ 'inner: loop { break '$0 } }] {}