ide_completion/
completions.rs

1//! This module defines an accumulator for completions which are going to be presented to user.
2
3pub(crate) mod attribute;
4pub(crate) mod dot;
5pub(crate) mod env_vars;
6pub(crate) mod expr;
7pub(crate) mod extern_abi;
8pub(crate) mod extern_crate;
9pub(crate) mod field;
10pub(crate) mod flyimport;
11pub(crate) mod fn_param;
12pub(crate) mod format_string;
13pub(crate) mod item_list;
14pub(crate) mod keyword;
15pub(crate) mod lifetime;
16pub(crate) mod mod_;
17pub(crate) mod pattern;
18pub(crate) mod postfix;
19pub(crate) mod ra_fixture;
20pub(crate) mod record;
21pub(crate) mod snippet;
22pub(crate) mod r#type;
23pub(crate) mod use_;
24pub(crate) mod vis;
25
26use std::iter;
27
28use hir::{HasAttrs, Name, ScopeDef, Variant, sym};
29use ide_db::{RootDatabase, SymbolKind, imports::import_assets::LocatedImport};
30use syntax::{SmolStr, ToSmolStr, ast};
31
32use crate::{
33    CompletionContext, CompletionItem, CompletionItemKind,
34    context::{
35        DotAccess, ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind,
36        PathCompletionCtx, PathKind, PatternContext, TypeLocation, Visible,
37    },
38    item::Builder,
39    render::{
40        RenderContext,
41        const_::render_const,
42        function::{render_fn, render_method},
43        literal::{render_struct_literal, render_variant_lit},
44        macro_::render_macro,
45        pattern::{render_struct_pat, render_variant_pat},
46        render_expr, render_field, render_path_resolution, render_pattern_resolution,
47        render_tuple_field,
48        type_alias::{render_type_alias, render_type_alias_with_eq},
49        union_literal::render_union_literal,
50    },
51};
52
53/// Represents an in-progress set of completions being built.
54#[derive(Debug, Default)]
55pub struct Completions {
56    buf: Vec<CompletionItem>,
57}
58
59impl From<Completions> for Vec<CompletionItem> {
60    fn from(val: Completions) -> Self {
61        val.buf
62    }
63}
64
65impl Builder {
66    /// Convenience method, which allows to add a freshly created completion into accumulator
67    /// without binding it to the variable.
68    pub(crate) fn add_to(self, acc: &mut Completions, db: &RootDatabase) {
69        acc.add(self.build(db))
70    }
71}
72
73impl Completions {
74    fn add(&mut self, item: CompletionItem) {
75        self.buf.push(item)
76    }
77
78    fn add_many(&mut self, items: impl IntoIterator<Item = CompletionItem>) {
79        self.buf.extend(items)
80    }
81
82    fn add_opt(&mut self, item: Option<CompletionItem>) {
83        if let Some(item) = item {
84            self.buf.push(item)
85        }
86    }
87
88    pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) {
89        let item = CompletionItem::new(
90            CompletionItemKind::Keyword,
91            ctx.source_range(),
92            SmolStr::new_static(keyword),
93            ctx.edition,
94        );
95        item.add_to(self, ctx.db);
96    }
97
98    pub(crate) fn add_nameref_keywords_with_colon(&mut self, ctx: &CompletionContext<'_>) {
99        ["self::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
100
101        if ctx.depth_from_crate_root > 0 {
102            self.add_keyword(ctx, "super::");
103        }
104    }
105
106    pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext<'_>) {
107        ["self", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
108
109        if ctx.depth_from_crate_root > 0 {
110            self.add_keyword(ctx, "super");
111        }
112    }
113
114    pub(crate) fn add_type_keywords(&mut self, ctx: &CompletionContext<'_>) {
115        self.add_keyword_snippet(ctx, "fn", "fn($1)");
116        self.add_keyword_snippet(ctx, "dyn", "dyn $0");
117        self.add_keyword_snippet(ctx, "impl", "impl $0");
118        self.add_keyword_snippet(ctx, "for", "for<$1>");
119    }
120
121    pub(crate) fn add_super_keyword(
122        &mut self,
123        ctx: &CompletionContext<'_>,
124        super_chain_len: Option<usize>,
125    ) {
126        if let Some(len) = super_chain_len
127            && len > 0
128            && len < ctx.depth_from_crate_root
129        {
130            self.add_keyword(ctx, "super::");
131        }
132    }
133
134    pub(crate) fn add_keyword_snippet_expr(
135        &mut self,
136        ctx: &CompletionContext<'_>,
137        incomplete_let: bool,
138        kw: &str,
139        snippet: &str,
140    ) {
141        let mut item =
142            CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw, ctx.edition);
143
144        match ctx.config.snippet_cap {
145            Some(cap) => {
146                if incomplete_let && snippet.ends_with('}') {
147                    // complete block expression snippets with a trailing semicolon, if inside an incomplete let
148                    cov_mark::hit!(let_semi);
149                    item.insert_snippet(cap, format!("{snippet};"));
150                } else {
151                    item.insert_snippet(cap, snippet);
152                }
153            }
154            None => {
155                item.insert_text(if snippet.contains('$') { kw } else { snippet });
156            }
157        };
158        item.add_to(self, ctx.db);
159    }
160
161    pub(crate) fn add_keyword_snippet(
162        &mut self,
163        ctx: &CompletionContext<'_>,
164        kw: &str,
165        snippet: &str,
166    ) {
167        let mut item =
168            CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw, ctx.edition);
169
170        match ctx.config.snippet_cap {
171            Some(cap) => item.insert_snippet(cap, snippet),
172            None => item.insert_text(if snippet.contains('$') { kw } else { snippet }),
173        };
174        item.add_to(self, ctx.db);
175    }
176
177    pub(crate) fn add_expr(
178        &mut self,
179        ctx: &CompletionContext<'_>,
180        expr: &hir::term_search::Expr<'_>,
181    ) {
182        if let Some(item) = render_expr(ctx, expr) {
183            item.add_to(self, ctx.db)
184        }
185    }
186
187    pub(crate) fn add_crate_roots(
188        &mut self,
189        ctx: &CompletionContext<'_>,
190        path_ctx: &PathCompletionCtx<'_>,
191    ) {
192        ctx.process_all_names(&mut |name, res, doc_aliases| match res {
193            ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
194                self.add_module(ctx, path_ctx, m, name, doc_aliases);
195            }
196            _ => (),
197        });
198    }
199
200    pub(crate) fn add_path_resolution(
201        &mut self,
202        ctx: &CompletionContext<'_>,
203        path_ctx: &PathCompletionCtx<'_>,
204        local_name: hir::Name,
205        resolution: hir::ScopeDef,
206        doc_aliases: Vec<syntax::SmolStr>,
207    ) {
208        let is_private_editable = match ctx.def_is_visible(&resolution) {
209            Visible::Yes => false,
210            Visible::Editable => true,
211            Visible::No => return,
212        };
213        self.add(
214            render_path_resolution(
215                RenderContext::new(ctx)
216                    .private_editable(is_private_editable)
217                    .doc_aliases(doc_aliases),
218                path_ctx,
219                local_name,
220                resolution,
221            )
222            .build(ctx.db),
223        );
224    }
225
226    pub(crate) fn add_pattern_resolution(
227        &mut self,
228        ctx: &CompletionContext<'_>,
229        pattern_ctx: &PatternContext,
230        local_name: hir::Name,
231        resolution: hir::ScopeDef,
232    ) {
233        let is_private_editable = match ctx.def_is_visible(&resolution) {
234            Visible::Yes => false,
235            Visible::Editable => true,
236            Visible::No => return,
237        };
238        self.add(
239            render_pattern_resolution(
240                RenderContext::new(ctx).private_editable(is_private_editable),
241                pattern_ctx,
242                local_name,
243                resolution,
244            )
245            .build(ctx.db),
246        );
247    }
248
249    pub(crate) fn add_enum_variants(
250        &mut self,
251        ctx: &CompletionContext<'_>,
252        path_ctx: &PathCompletionCtx<'_>,
253        e: hir::Enum,
254    ) {
255        if !ctx.check_stability_and_hidden(e) {
256            return;
257        }
258        e.variants(ctx.db)
259            .into_iter()
260            .for_each(|variant| self.add_enum_variant(ctx, path_ctx, variant, None));
261    }
262
263    pub(crate) fn add_module(
264        &mut self,
265        ctx: &CompletionContext<'_>,
266        path_ctx: &PathCompletionCtx<'_>,
267        module: hir::Module,
268        local_name: hir::Name,
269        doc_aliases: Vec<syntax::SmolStr>,
270    ) {
271        self.add_path_resolution(
272            ctx,
273            path_ctx,
274            local_name,
275            hir::ScopeDef::ModuleDef(module.into()),
276            doc_aliases,
277        );
278    }
279
280    pub(crate) fn add_macro(
281        &mut self,
282        ctx: &CompletionContext<'_>,
283        path_ctx: &PathCompletionCtx<'_>,
284        mac: hir::Macro,
285        local_name: hir::Name,
286    ) {
287        let is_private_editable = match ctx.is_visible(&mac) {
288            Visible::Yes => false,
289            Visible::Editable => true,
290            Visible::No => return,
291        };
292        self.add(
293            render_macro(
294                RenderContext::new(ctx).private_editable(is_private_editable),
295                path_ctx,
296                local_name,
297                mac,
298            )
299            .build(ctx.db),
300        );
301    }
302
303    pub(crate) fn add_function(
304        &mut self,
305        ctx: &CompletionContext<'_>,
306        path_ctx: &PathCompletionCtx<'_>,
307        func: hir::Function,
308        local_name: Option<hir::Name>,
309    ) {
310        let is_private_editable = match ctx.is_visible(&func) {
311            Visible::Yes => false,
312            Visible::Editable => true,
313            Visible::No => return,
314        };
315        let doc_aliases = ctx.doc_aliases(&func);
316        self.add(
317            render_fn(
318                RenderContext::new(ctx)
319                    .private_editable(is_private_editable)
320                    .doc_aliases(doc_aliases),
321                path_ctx,
322                local_name,
323                func,
324            )
325            .build(ctx.db),
326        );
327    }
328
329    pub(crate) fn add_method(
330        &mut self,
331        ctx: &CompletionContext<'_>,
332        dot_access: &DotAccess<'_>,
333        func: hir::Function,
334        receiver: Option<SmolStr>,
335        local_name: Option<hir::Name>,
336    ) {
337        let is_private_editable = match ctx.is_visible(&func) {
338            Visible::Yes => false,
339            Visible::Editable => true,
340            Visible::No => return,
341        };
342        let doc_aliases = ctx.doc_aliases(&func);
343        self.add(
344            render_method(
345                RenderContext::new(ctx)
346                    .private_editable(is_private_editable)
347                    .doc_aliases(doc_aliases),
348                dot_access,
349                receiver,
350                local_name,
351                func,
352            )
353            .build(ctx.db),
354        );
355    }
356
357    pub(crate) fn add_method_with_import(
358        &mut self,
359        ctx: &CompletionContext<'_>,
360        dot_access: &DotAccess<'_>,
361        func: hir::Function,
362        import: LocatedImport,
363    ) {
364        let is_private_editable = match ctx.is_visible(&func) {
365            Visible::Yes => false,
366            Visible::Editable => true,
367            Visible::No => return,
368        };
369        let doc_aliases = ctx.doc_aliases(&func);
370        self.add(
371            render_method(
372                RenderContext::new(ctx)
373                    .private_editable(is_private_editable)
374                    .doc_aliases(doc_aliases)
375                    .import_to_add(Some(import)),
376                dot_access,
377                None,
378                None,
379                func,
380            )
381            .build(ctx.db),
382        );
383    }
384
385    pub(crate) fn add_const(&mut self, ctx: &CompletionContext<'_>, konst: hir::Const) {
386        let is_private_editable = match ctx.is_visible(&konst) {
387            Visible::Yes => false,
388            Visible::Editable => true,
389            Visible::No => return,
390        };
391        self.add_opt(render_const(
392            RenderContext::new(ctx).private_editable(is_private_editable),
393            konst,
394        ));
395    }
396
397    pub(crate) fn add_type_alias(
398        &mut self,
399        ctx: &CompletionContext<'_>,
400        type_alias: hir::TypeAlias,
401    ) {
402        let is_private_editable = match ctx.is_visible(&type_alias) {
403            Visible::Yes => false,
404            Visible::Editable => true,
405            Visible::No => return,
406        };
407        self.add_opt(render_type_alias(
408            RenderContext::new(ctx).private_editable(is_private_editable),
409            type_alias,
410        ));
411    }
412
413    pub(crate) fn add_type_alias_with_eq(
414        &mut self,
415        ctx: &CompletionContext<'_>,
416        type_alias: hir::TypeAlias,
417    ) {
418        if !ctx.check_stability(Some(&type_alias.attrs(ctx.db))) {
419            return;
420        }
421        self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
422    }
423
424    pub(crate) fn add_qualified_enum_variant(
425        &mut self,
426        ctx: &CompletionContext<'_>,
427        path_ctx: &PathCompletionCtx<'_>,
428        variant: hir::Variant,
429        path: hir::ModPath,
430    ) {
431        if !ctx.check_stability_and_hidden(variant) {
432            return;
433        }
434        if let Some(builder) =
435            render_variant_lit(RenderContext::new(ctx), path_ctx, None, variant, Some(path))
436        {
437            self.add(builder.build(ctx.db));
438        }
439    }
440
441    pub(crate) fn add_enum_variant(
442        &mut self,
443        ctx: &CompletionContext<'_>,
444        path_ctx: &PathCompletionCtx<'_>,
445        variant: hir::Variant,
446        local_name: Option<hir::Name>,
447    ) {
448        if !ctx.check_stability_and_hidden(variant) {
449            return;
450        }
451        if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
452            cov_mark::hit!(enum_variant_pattern_path);
453            self.add_variant_pat(ctx, pat_ctx, Some(path_ctx), variant, local_name);
454            return;
455        }
456
457        if let Some(builder) =
458            render_variant_lit(RenderContext::new(ctx), path_ctx, local_name, variant, None)
459        {
460            self.add(builder.build(ctx.db));
461        }
462    }
463
464    pub(crate) fn add_field(
465        &mut self,
466        ctx: &CompletionContext<'_>,
467        dot_access: &DotAccess<'_>,
468        receiver: Option<SmolStr>,
469        field: hir::Field,
470        ty: &hir::Type<'_>,
471    ) {
472        let is_private_editable = match ctx.is_visible(&field) {
473            Visible::Yes => false,
474            Visible::Editable => true,
475            Visible::No => return,
476        };
477        let doc_aliases = ctx.doc_aliases(&field);
478        let item = render_field(
479            RenderContext::new(ctx).private_editable(is_private_editable).doc_aliases(doc_aliases),
480            dot_access,
481            receiver,
482            field,
483            ty,
484        );
485        self.add(item);
486    }
487
488    pub(crate) fn add_struct_literal(
489        &mut self,
490        ctx: &CompletionContext<'_>,
491        path_ctx: &PathCompletionCtx<'_>,
492        strukt: hir::Struct,
493        path: Option<hir::ModPath>,
494        local_name: Option<hir::Name>,
495    ) {
496        let is_private_editable = match ctx.is_visible(&strukt) {
497            Visible::Yes => false,
498            Visible::Editable => true,
499            Visible::No => return,
500        };
501        if let Some(builder) = render_struct_literal(
502            RenderContext::new(ctx).private_editable(is_private_editable),
503            path_ctx,
504            strukt,
505            path,
506            local_name,
507        ) {
508            self.add(builder.build(ctx.db));
509        }
510    }
511
512    pub(crate) fn add_union_literal(
513        &mut self,
514        ctx: &CompletionContext<'_>,
515        un: hir::Union,
516        path: Option<hir::ModPath>,
517        local_name: Option<hir::Name>,
518    ) {
519        let is_private_editable = match ctx.is_visible(&un) {
520            Visible::Yes => false,
521            Visible::Editable => true,
522            Visible::No => return,
523        };
524        let item = render_union_literal(
525            RenderContext::new(ctx).private_editable(is_private_editable),
526            un,
527            path,
528            local_name,
529        );
530        self.add_opt(item);
531    }
532
533    pub(crate) fn add_tuple_field(
534        &mut self,
535        ctx: &CompletionContext<'_>,
536        receiver: Option<SmolStr>,
537        field: usize,
538        ty: &hir::Type<'_>,
539    ) {
540        // Only used for (unnamed) tuples, whose all fields *are* stable. No need to check
541        // stability here.
542        let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
543        self.add(item);
544    }
545
546    pub(crate) fn add_lifetime(&mut self, ctx: &CompletionContext<'_>, name: hir::Name) {
547        CompletionItem::new(
548            SymbolKind::LifetimeParam,
549            ctx.source_range(),
550            name.display_no_db(ctx.edition).to_smolstr(),
551            ctx.edition,
552        )
553        .add_to(self, ctx.db)
554    }
555
556    pub(crate) fn add_label(&mut self, ctx: &CompletionContext<'_>, name: hir::Name) {
557        CompletionItem::new(
558            SymbolKind::Label,
559            ctx.source_range(),
560            name.display_no_db(ctx.edition).to_smolstr(),
561            ctx.edition,
562        )
563        .add_to(self, ctx.db)
564    }
565
566    pub(crate) fn add_variant_pat(
567        &mut self,
568        ctx: &CompletionContext<'_>,
569        pattern_ctx: &PatternContext,
570        path_ctx: Option<&PathCompletionCtx<'_>>,
571        variant: hir::Variant,
572        local_name: Option<hir::Name>,
573    ) {
574        if !ctx.check_stability_and_hidden(variant) {
575            return;
576        }
577        self.add_opt(render_variant_pat(
578            RenderContext::new(ctx),
579            pattern_ctx,
580            path_ctx,
581            variant,
582            local_name,
583            None,
584        ));
585    }
586
587    pub(crate) fn add_qualified_variant_pat(
588        &mut self,
589        ctx: &CompletionContext<'_>,
590        pattern_ctx: &PatternContext,
591        variant: hir::Variant,
592        path: hir::ModPath,
593    ) {
594        if !ctx.check_stability_and_hidden(variant) {
595            return;
596        }
597        let path = Some(&path);
598        self.add_opt(render_variant_pat(
599            RenderContext::new(ctx),
600            pattern_ctx,
601            None,
602            variant,
603            None,
604            path,
605        ));
606    }
607
608    pub(crate) fn add_struct_pat(
609        &mut self,
610        ctx: &CompletionContext<'_>,
611        pattern_ctx: &PatternContext,
612        strukt: hir::Struct,
613        local_name: Option<hir::Name>,
614    ) {
615        let is_private_editable = match ctx.is_visible(&strukt) {
616            Visible::Yes => false,
617            Visible::Editable => true,
618            Visible::No => return,
619        };
620        self.add_opt(render_struct_pat(
621            RenderContext::new(ctx).private_editable(is_private_editable),
622            pattern_ctx,
623            strukt,
624            local_name,
625        ));
626    }
627
628    pub(crate) fn suggest_name(&mut self, ctx: &CompletionContext<'_>, name: &str) {
629        let item = CompletionItem::new(
630            CompletionItemKind::Binding,
631            ctx.source_range(),
632            SmolStr::from(name),
633            ctx.edition,
634        );
635        item.add_to(self, ctx.db);
636    }
637}
638
639/// Calls the callback for each variant of the provided enum with the path to the variant.
640/// Skips variants that are visible with single segment paths.
641fn enum_variants_with_paths(
642    acc: &mut Completions,
643    ctx: &CompletionContext<'_>,
644    enum_: hir::Enum,
645    impl_: Option<&ast::Impl>,
646    cb: impl Fn(&mut Completions, &CompletionContext<'_>, hir::Variant, hir::ModPath),
647) {
648    let mut process_variant = |variant: Variant| {
649        let self_path = hir::ModPath::from_segments(
650            hir::PathKind::Plain,
651            iter::once(Name::new_symbol_root(sym::Self_)).chain(iter::once(variant.name(ctx.db))),
652        );
653
654        cb(acc, ctx, variant, self_path);
655    };
656
657    let variants = enum_.variants(ctx.db);
658
659    if let Some(impl_) = impl_.and_then(|impl_| ctx.sema.to_def(impl_))
660        && impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_))
661    {
662        variants.iter().for_each(|variant| process_variant(*variant));
663    }
664
665    for variant in variants {
666        if let Some(path) = ctx.module.find_path(
667            ctx.db,
668            hir::ModuleDef::from(variant),
669            ctx.config.find_path_config(ctx.is_nightly),
670        ) {
671            // Variants with trivial paths are already added by the existing completion logic,
672            // so we should avoid adding these twice
673            if path.segments().len() > 1 {
674                cb(acc, ctx, variant, path);
675            }
676        }
677    }
678}
679
680pub(super) fn complete_name(
681    acc: &mut Completions,
682    ctx: &CompletionContext<'_>,
683    NameContext { name, kind }: &NameContext,
684) {
685    match kind {
686        NameKind::Const => {
687            item_list::trait_impl::complete_trait_impl_const(acc, ctx, name);
688        }
689        NameKind::Function => {
690            item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
691        }
692        NameKind::IdentPat(pattern_ctx) => {
693            if ctx.token.kind() != syntax::T![_] {
694                complete_patterns(acc, ctx, pattern_ctx)
695            }
696        }
697        NameKind::Module(mod_under_caret) => {
698            mod_::complete_mod(acc, ctx, mod_under_caret);
699        }
700        NameKind::TypeAlias => {
701            item_list::trait_impl::complete_trait_impl_type_alias(acc, ctx, name);
702        }
703        NameKind::RecordField => {
704            field::complete_field_list_record_variant(acc, ctx);
705        }
706        NameKind::TypeParam => {
707            acc.add_keyword_snippet(ctx, "const", "const $1: $0");
708        }
709        NameKind::ConstParam
710        | NameKind::Enum
711        | NameKind::MacroDef
712        | NameKind::MacroRules
713        | NameKind::Rename
714        | NameKind::SelfParam
715        | NameKind::Static
716        | NameKind::Struct
717        | NameKind::Trait
718        | NameKind::Union
719        | NameKind::Variant => (),
720    }
721}
722
723pub(super) fn complete_name_ref(
724    acc: &mut Completions,
725    ctx: &CompletionContext<'_>,
726    NameRefContext { nameref, kind }: &NameRefContext<'_>,
727) {
728    match kind {
729        NameRefKind::Path(path_ctx) => {
730            flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
731
732            match &path_ctx.kind {
733                PathKind::Expr { expr_ctx } => {
734                    expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
735                    expr::complete_expr(acc, ctx);
736
737                    dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
738                    item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
739                    snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
740                }
741                PathKind::Type { location } => {
742                    r#type::complete_type_path(acc, ctx, path_ctx, location);
743
744                    match location {
745                        TypeLocation::TupleField => {
746                            field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
747                        }
748                        TypeLocation::TypeAscription(ascription) => {
749                            r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
750                        }
751                        TypeLocation::GenericArg { .. }
752                        | TypeLocation::AssocConstEq
753                        | TypeLocation::AssocTypeEq
754                        | TypeLocation::TypeBound
755                        | TypeLocation::ImplTarget
756                        | TypeLocation::ImplTrait
757                        | TypeLocation::Other => (),
758                    }
759                }
760                PathKind::Attr { attr_ctx } => {
761                    attribute::complete_attribute_path(acc, ctx, path_ctx, attr_ctx);
762                }
763                PathKind::Derive { existing_derives } => {
764                    attribute::complete_derive_path(acc, ctx, path_ctx, existing_derives);
765                }
766                PathKind::Item { kind } => {
767                    item_list::complete_item_list(acc, ctx, path_ctx, kind);
768
769                    snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
770                    if let ItemListKind::TraitImpl(impl_) = kind {
771                        item_list::trait_impl::complete_trait_impl_item_by_name(
772                            acc, ctx, path_ctx, nameref, impl_,
773                        );
774                    }
775                }
776                PathKind::Pat { .. } => {
777                    pattern::complete_pattern_path(acc, ctx, path_ctx);
778                }
779                PathKind::Vis { has_in_token } => {
780                    vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
781                }
782                PathKind::Use => {
783                    use_::complete_use_path(acc, ctx, path_ctx, nameref);
784                }
785            }
786        }
787        NameRefKind::ExternCrate => extern_crate::complete_extern_crate(acc, ctx),
788        NameRefKind::DotAccess(dot_access) => {
789            flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
790            dot::complete_dot(acc, ctx, dot_access);
791            postfix::complete_postfix(acc, ctx, dot_access);
792        }
793        NameRefKind::Keyword(item) => {
794            keyword::complete_for_and_where(acc, ctx, item);
795        }
796        NameRefKind::RecordExpr { dot_prefix, expr } => {
797            record::complete_record_expr_fields(acc, ctx, expr, dot_prefix);
798        }
799        NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
800    }
801}
802
803fn complete_patterns(
804    acc: &mut Completions,
805    ctx: &CompletionContext<'_>,
806    pattern_ctx: &PatternContext,
807) {
808    flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
809    fn_param::complete_fn_param(acc, ctx, pattern_ctx);
810    pattern::complete_pattern(acc, ctx, pattern_ctx);
811    record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
812}