]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/completions/item_list.rs
ebbc33c2da0db7d293cd5572239bbc1b03792e69
[rust.git] / crates / ide-completion / src / completions / item_list.rs
1 //! Completion of paths and keywords at item list position.
2
3 use crate::{
4     completions::module_or_fn_macro,
5     context::{PathCompletionCtx, PathKind, PathQualifierCtx},
6     CompletionContext, Completions,
7 };
8
9 pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
10     let _p = profile::span("complete_item_list");
11     if ctx.is_path_disallowed() || ctx.has_unfinished_impl_or_trait_prev_sibling() {
12         return;
13     }
14
15     let (&is_absolute_path, qualifier) = match ctx.path_context() {
16         Some(PathCompletionCtx {
17             kind: PathKind::Item { .. },
18             is_absolute_path,
19             qualifier,
20             ..
21         }) => (is_absolute_path, qualifier),
22         _ => return,
23     };
24
25     match qualifier {
26         Some(PathQualifierCtx { resolution, is_super_chain, .. }) => {
27             if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) = resolution {
28                 for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
29                     if let Some(def) = module_or_fn_macro(ctx.db, def) {
30                         acc.add_resolution(ctx, name, def);
31                     }
32                 }
33             }
34
35             if *is_super_chain {
36                 acc.add_keyword(ctx, "super::");
37             }
38         }
39         None if is_absolute_path => {
40             acc.add_crate_roots(ctx);
41         }
42         None => {
43             ctx.process_all_names(&mut |name, def| {
44                 if let Some(def) = module_or_fn_macro(ctx.db, def) {
45                     acc.add_resolution(ctx, name, def);
46                 }
47             });
48             acc.add_nameref_keywords_with_colon(ctx);
49         }
50     }
51 }