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 {
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);
33 pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) {
34 if !ctx.is_label_ref {
37 ctx.scope.process_all_names(&mut |name, res| {
38 if let ScopeDef::Label(_) = res {
39 acc.add_resolution(ctx, name.to_string(), &res);
46 use expect_test::{expect, Expect};
49 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
50 CompletionConfig, CompletionKind,
53 fn check(ra_fixture: &str, expect: Expect) {
54 check_with_config(TEST_CONFIG, ra_fixture, expect);
57 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
58 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
59 expect.assert_eq(&actual)
63 fn check_lifetime_edit() {
67 fn func<'lifetime>(foo: &'li$0) {}
70 fn func<'lifetime>(foo: &'lifetime) {}
73 cov_mark::check!(completes_if_lifetime_without_idents);
77 fn func<'lifetime>(foo: &'$0) {}
80 fn func<'lifetime>(foo: &'lifetime) {}
86 fn complete_lifetime_in_ref() {
89 fn foo<'lifetime>(foo: &'a$0 usize) {}
99 fn complete_lifetime_in_ref_missing_ty() {
102 fn foo<'lifetime>(foo: &'a$0) {}
111 fn complete_lifetime_in_self_ref() {
116 fn foo<'func>(&'a$0 self) {}
128 fn complete_lifetime_in_arg_list() {
132 fn foo<'lifetime>(_: Foo<'a$0>) {}
142 fn complete_lifetime_in_where_pred() {
145 fn foo2<'lifetime, T>() where 'a$0 {}
155 fn complete_lifetime_in_ty_bound() {
158 fn foo2<'lifetime, T>() where T: 'a$0 {}
167 fn foo2<'lifetime, T>() where T: Trait<'a$0> {}
177 fn dont_complete_lifetime_in_assoc_ty_bound() {
180 fn foo2<'lifetime, T>() where T: Trait<Item = 'a$0> {}
187 fn complete_lifetime_in_param_list() {
196 fn foo<'footime, 'lifetime: 'a$0>() {}
205 fn check_label_edit() {
226 fn complete_label_in_loop() {
254 fn complete_label_in_block_nested() {
273 fn complete_label_in_loop_with_value() {
289 fn complete_label_in_while_cond() {
293 'outer: while { 'inner: loop { break '$0 } } {}
304 fn complete_label_in_for_iterable() {
308 'outer: for _ in [{ 'inner: loop { break '$0 } }] {}