Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Completion of names from the current scope in expression position.

use hir::ScopeDef;

use crate::{
    context::{PathCompletionCtx, PathKind, PathQualifierCtx},
    CompletionContext, Completions,
};

pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext) {
    let _p = profile::span("complete_expr_path");
    if ctx.is_path_disallowed() {
        return;
    }

    let (&is_absolute_path, qualifier) = match &ctx.path_context {
        Some(PathCompletionCtx {
            kind: Some(PathKind::Expr), is_absolute_path, qualifier, ..
        }) => (is_absolute_path, qualifier),
        _ => return,
    };

    match qualifier {
        Some(PathQualifierCtx { .. }) => return,
        None if is_absolute_path => acc.add_crate_roots(ctx),
        None => {
            acc.add_nameref_keywords_with_colon(ctx);
            if let Some(hir::Adt::Enum(e)) =
                ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
            {
                super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
                    acc.add_qualified_enum_variant(ctx, variant, path)
                });
            }
            ctx.process_all_names(&mut |name, def| {
                use hir::{GenericParam::*, ModuleDef::*};
                let add_resolution = match def {
                    ScopeDef::GenericParam(LifetimeParam(_)) | ScopeDef::Label(_) => false,
                    // Don't suggest attribute macros and derives.
                    ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
                    _ => true,
                };
                if add_resolution {
                    acc.add_resolution(ctx, name, def);
                }
            });
        }
    }
}