1 //! This module defines an accumulator for completions which are going to be presented to user.
3 pub(crate) mod attribute;
6 pub(crate) mod extern_abi;
8 pub(crate) mod flyimport;
9 pub(crate) mod fn_param;
10 pub(crate) mod format_string;
11 pub(crate) mod item_list;
12 pub(crate) mod keyword;
13 pub(crate) mod lifetime;
15 pub(crate) mod pattern;
16 pub(crate) mod postfix;
17 pub(crate) mod record;
18 pub(crate) mod snippet;
19 pub(crate) mod r#type;
25 use hir::{known, ScopeDef};
26 use ide_db::{imports::import_assets::LocatedImport, SymbolKind};
31 DotAccess, ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind,
32 PathCompletionCtx, PathKind, PatternContext, TypeLocation, Visible,
37 function::{render_fn, render_method},
38 literal::{render_struct_literal, render_variant_lit},
40 pattern::{render_struct_pat, render_variant_pat},
41 render_field, render_path_resolution, render_pattern_resolution, render_tuple_field,
42 type_alias::{render_type_alias, render_type_alias_with_eq},
43 union_literal::render_union_literal,
46 CompletionContext, CompletionItem, CompletionItemKind,
49 /// Represents an in-progress set of completions being built.
50 #[derive(Debug, Default)]
51 pub struct Completions {
52 buf: Vec<CompletionItem>,
55 impl From<Completions> for Vec<CompletionItem> {
56 fn from(val: Completions) -> Self {
62 /// Convenience method, which allows to add a freshly created completion into accumulator
63 /// without binding it to the variable.
64 pub(crate) fn add_to(self, acc: &mut Completions) {
70 fn add(&mut self, item: CompletionItem) {
74 fn add_opt(&mut self, item: Option<CompletionItem>) {
75 if let Some(item) = item {
80 pub(crate) fn add_all<I>(&mut self, items: I)
83 I::Item: Into<CompletionItem>,
85 items.into_iter().for_each(|item| self.add(item.into()))
88 pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) {
89 let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword);
93 pub(crate) fn add_nameref_keywords_with_colon(&mut self, ctx: &CompletionContext<'_>) {
94 ["self::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
96 if ctx.depth_from_crate_root > 0 {
97 self.add_keyword(ctx, "super::");
101 pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext<'_>) {
102 ["self", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
104 if ctx.depth_from_crate_root > 0 {
105 self.add_keyword(ctx, "super");
109 pub(crate) fn add_super_keyword(
111 ctx: &CompletionContext<'_>,
112 super_chain_len: Option<usize>,
114 if let Some(len) = super_chain_len {
115 if len > 0 && len < ctx.depth_from_crate_root {
116 self.add_keyword(ctx, "super::");
121 pub(crate) fn add_keyword_snippet_expr(
123 ctx: &CompletionContext<'_>,
124 incomplete_let: bool,
128 let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
130 match ctx.config.snippet_cap {
132 if incomplete_let && snippet.ends_with('}') {
133 // complete block expression snippets with a trailing semicolon, if inside an incomplete let
134 cov_mark::hit!(let_semi);
135 item.insert_snippet(cap, format!("{};", snippet));
137 item.insert_snippet(cap, snippet);
141 item.insert_text(if snippet.contains('$') { kw } else { snippet });
147 pub(crate) fn add_keyword_snippet(
149 ctx: &CompletionContext<'_>,
153 let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
155 match ctx.config.snippet_cap {
156 Some(cap) => item.insert_snippet(cap, snippet),
157 None => item.insert_text(if snippet.contains('$') { kw } else { snippet }),
162 pub(crate) fn add_crate_roots(
164 ctx: &CompletionContext<'_>,
165 path_ctx: &PathCompletionCtx,
167 ctx.process_all_names(&mut |name, res| match res {
168 ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
169 self.add_module(ctx, path_ctx, m, name);
175 pub(crate) fn add_path_resolution(
177 ctx: &CompletionContext<'_>,
178 path_ctx: &PathCompletionCtx,
179 local_name: hir::Name,
180 resolution: hir::ScopeDef,
182 let is_private_editable = match ctx.def_is_visible(&resolution) {
183 Visible::Yes => false,
184 Visible::Editable => true,
185 Visible::No => return,
188 render_path_resolution(
189 RenderContext::new(ctx).private_editable(is_private_editable),
198 pub(crate) fn add_pattern_resolution(
200 ctx: &CompletionContext<'_>,
201 pattern_ctx: &PatternContext,
202 local_name: hir::Name,
203 resolution: hir::ScopeDef,
205 let is_private_editable = match ctx.def_is_visible(&resolution) {
206 Visible::Yes => false,
207 Visible::Editable => true,
208 Visible::No => return,
211 render_pattern_resolution(
212 RenderContext::new(ctx).private_editable(is_private_editable),
221 pub(crate) fn add_enum_variants(
223 ctx: &CompletionContext<'_>,
224 path_ctx: &PathCompletionCtx,
229 .for_each(|variant| self.add_enum_variant(ctx, path_ctx, variant, None));
232 pub(crate) fn add_module(
234 ctx: &CompletionContext<'_>,
235 path_ctx: &PathCompletionCtx,
237 local_name: hir::Name,
239 self.add_path_resolution(
243 hir::ScopeDef::ModuleDef(module.into()),
247 pub(crate) fn add_macro(
249 ctx: &CompletionContext<'_>,
250 path_ctx: &PathCompletionCtx,
252 local_name: hir::Name,
254 let is_private_editable = match ctx.is_visible(&mac) {
255 Visible::Yes => false,
256 Visible::Editable => true,
257 Visible::No => return,
261 RenderContext::new(ctx).private_editable(is_private_editable),
270 pub(crate) fn add_function(
272 ctx: &CompletionContext<'_>,
273 path_ctx: &PathCompletionCtx,
275 local_name: Option<hir::Name>,
277 let is_private_editable = match ctx.is_visible(&func) {
278 Visible::Yes => false,
279 Visible::Editable => true,
280 Visible::No => return,
284 RenderContext::new(ctx).private_editable(is_private_editable),
293 pub(crate) fn add_method(
295 ctx: &CompletionContext<'_>,
296 dot_access: &DotAccess,
298 receiver: Option<hir::Name>,
299 local_name: Option<hir::Name>,
301 let is_private_editable = match ctx.is_visible(&func) {
302 Visible::Yes => false,
303 Visible::Editable => true,
304 Visible::No => return,
308 RenderContext::new(ctx).private_editable(is_private_editable),
318 pub(crate) fn add_method_with_import(
320 ctx: &CompletionContext<'_>,
321 dot_access: &DotAccess,
323 import: LocatedImport,
325 let is_private_editable = match ctx.is_visible(&func) {
326 Visible::Yes => false,
327 Visible::Editable => true,
328 Visible::No => return,
332 RenderContext::new(ctx)
333 .private_editable(is_private_editable)
334 .import_to_add(Some(import)),
344 pub(crate) fn add_const(&mut self, ctx: &CompletionContext<'_>, konst: hir::Const) {
345 let is_private_editable = match ctx.is_visible(&konst) {
346 Visible::Yes => false,
347 Visible::Editable => true,
348 Visible::No => return,
350 self.add_opt(render_const(
351 RenderContext::new(ctx).private_editable(is_private_editable),
356 pub(crate) fn add_type_alias(
358 ctx: &CompletionContext<'_>,
359 type_alias: hir::TypeAlias,
361 let is_private_editable = match ctx.is_visible(&type_alias) {
362 Visible::Yes => false,
363 Visible::Editable => true,
364 Visible::No => return,
366 self.add_opt(render_type_alias(
367 RenderContext::new(ctx).private_editable(is_private_editable),
372 pub(crate) fn add_type_alias_with_eq(
374 ctx: &CompletionContext<'_>,
375 type_alias: hir::TypeAlias,
377 self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
380 pub(crate) fn add_qualified_enum_variant(
382 ctx: &CompletionContext<'_>,
383 path_ctx: &PathCompletionCtx,
384 variant: hir::Variant,
387 if let Some(builder) =
388 render_variant_lit(RenderContext::new(ctx), path_ctx, None, variant, Some(path))
390 self.add(builder.build());
394 pub(crate) fn add_enum_variant(
396 ctx: &CompletionContext<'_>,
397 path_ctx: &PathCompletionCtx,
398 variant: hir::Variant,
399 local_name: Option<hir::Name>,
401 if let PathCompletionCtx { kind: PathKind::Pat { pat_ctx }, .. } = path_ctx {
402 cov_mark::hit!(enum_variant_pattern_path);
403 self.add_variant_pat(ctx, pat_ctx, Some(path_ctx), variant, local_name);
407 if let Some(builder) =
408 render_variant_lit(RenderContext::new(ctx), path_ctx, local_name, variant, None)
410 self.add(builder.build());
414 pub(crate) fn add_field(
416 ctx: &CompletionContext<'_>,
417 dot_access: &DotAccess,
418 receiver: Option<hir::Name>,
422 let is_private_editable = match ctx.is_visible(&field) {
423 Visible::Yes => false,
424 Visible::Editable => true,
425 Visible::No => return,
427 let item = render_field(
428 RenderContext::new(ctx).private_editable(is_private_editable),
437 pub(crate) fn add_struct_literal(
439 ctx: &CompletionContext<'_>,
440 path_ctx: &PathCompletionCtx,
442 path: Option<hir::ModPath>,
443 local_name: Option<hir::Name>,
445 if let Some(builder) =
446 render_struct_literal(RenderContext::new(ctx), path_ctx, strukt, path, local_name)
448 self.add(builder.build());
452 pub(crate) fn add_union_literal(
454 ctx: &CompletionContext<'_>,
456 path: Option<hir::ModPath>,
457 local_name: Option<hir::Name>,
459 let item = render_union_literal(RenderContext::new(ctx), un, path, local_name);
463 pub(crate) fn add_tuple_field(
465 ctx: &CompletionContext<'_>,
466 receiver: Option<hir::Name>,
470 let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
474 pub(crate) fn add_lifetime(&mut self, ctx: &CompletionContext<'_>, name: hir::Name) {
475 CompletionItem::new(SymbolKind::LifetimeParam, ctx.source_range(), name.to_smol_str())
479 pub(crate) fn add_label(&mut self, ctx: &CompletionContext<'_>, name: hir::Name) {
480 CompletionItem::new(SymbolKind::Label, ctx.source_range(), name.to_smol_str()).add_to(self)
483 pub(crate) fn add_variant_pat(
485 ctx: &CompletionContext<'_>,
486 pattern_ctx: &PatternContext,
487 path_ctx: Option<&PathCompletionCtx>,
488 variant: hir::Variant,
489 local_name: Option<hir::Name>,
491 self.add_opt(render_variant_pat(
492 RenderContext::new(ctx),
501 pub(crate) fn add_qualified_variant_pat(
503 ctx: &CompletionContext<'_>,
504 pattern_ctx: &PatternContext,
505 variant: hir::Variant,
508 let path = Some(&path);
509 self.add_opt(render_variant_pat(
510 RenderContext::new(ctx),
519 pub(crate) fn add_struct_pat(
521 ctx: &CompletionContext<'_>,
522 pattern_ctx: &PatternContext,
524 local_name: Option<hir::Name>,
526 self.add_opt(render_struct_pat(RenderContext::new(ctx), pattern_ctx, strukt, local_name));
530 /// Calls the callback for each variant of the provided enum with the path to the variant.
531 /// Skips variants that are visible with single segment paths.
532 fn enum_variants_with_paths(
533 acc: &mut Completions,
534 ctx: &CompletionContext<'_>,
536 impl_: &Option<ast::Impl>,
537 cb: impl Fn(&mut Completions, &CompletionContext<'_>, hir::Variant, hir::ModPath),
539 let variants = enum_.variants(ctx.db);
541 if let Some(impl_) = impl_.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
542 if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
543 for &variant in &variants {
544 let self_path = hir::ModPath::from_segments(
545 hir::PathKind::Plain,
546 iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
548 cb(acc, ctx, variant, self_path);
553 for variant in variants {
554 if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
555 // Variants with trivial paths are already added by the existing completion logic,
556 // so we should avoid adding these twice
557 if path.segments().len() > 1 {
558 cb(acc, ctx, variant, path);
564 pub(super) fn complete_name(
565 acc: &mut Completions,
566 ctx: &CompletionContext<'_>,
567 NameContext { name, kind }: &NameContext,
571 item_list::trait_impl::complete_trait_impl_const(acc, ctx, name);
573 NameKind::Function => {
574 item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
576 NameKind::IdentPat(pattern_ctx) => {
577 if ctx.token.kind() != syntax::T![_] {
578 complete_patterns(acc, ctx, pattern_ctx)
581 NameKind::Module(mod_under_caret) => {
582 mod_::complete_mod(acc, ctx, mod_under_caret);
584 NameKind::TypeAlias => {
585 item_list::trait_impl::complete_trait_impl_type_alias(acc, ctx, name);
587 NameKind::RecordField => {
588 field::complete_field_list_record_variant(acc, ctx);
593 | NameKind::MacroRules
595 | NameKind::SelfParam
599 | NameKind::TypeParam
601 | NameKind::Variant => (),
605 pub(super) fn complete_name_ref(
606 acc: &mut Completions,
607 ctx: &CompletionContext<'_>,
608 NameRefContext { nameref, kind }: &NameRefContext,
611 NameRefKind::Path(path_ctx) => {
612 flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
614 match &path_ctx.kind {
615 PathKind::Expr { expr_ctx } => {
616 expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
618 dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
619 item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
620 record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx);
621 snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
623 PathKind::Type { location } => {
624 r#type::complete_type_path(acc, ctx, path_ctx, location);
627 TypeLocation::TupleField => {
628 field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
630 TypeLocation::TypeAscription(ascription) => {
631 r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
633 TypeLocation::GenericArgList(_)
634 | TypeLocation::TypeBound
635 | TypeLocation::ImplTarget
636 | TypeLocation::ImplTrait
637 | TypeLocation::Other => (),
640 PathKind::Attr { attr_ctx } => {
641 attribute::complete_attribute_path(acc, ctx, path_ctx, attr_ctx);
643 PathKind::Derive { existing_derives } => {
644 attribute::complete_derive_path(acc, ctx, path_ctx, existing_derives);
646 PathKind::Item { kind } => {
647 item_list::complete_item_list(acc, ctx, path_ctx, kind);
649 snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
650 if let ItemListKind::TraitImpl(impl_) = kind {
651 item_list::trait_impl::complete_trait_impl_item_by_name(
652 acc, ctx, path_ctx, nameref, impl_,
656 PathKind::Pat { .. } => {
657 pattern::complete_pattern_path(acc, ctx, path_ctx);
659 PathKind::Vis { has_in_token } => {
660 vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
663 use_::complete_use_path(acc, ctx, path_ctx, nameref);
667 NameRefKind::DotAccess(dot_access) => {
668 flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
669 dot::complete_dot(acc, ctx, dot_access);
670 postfix::complete_postfix(acc, ctx, dot_access);
672 NameRefKind::Keyword(item) => {
673 keyword::complete_for_and_where(acc, ctx, item);
675 NameRefKind::RecordExpr { dot_prefix, expr } => {
676 record::complete_record_expr_fields(acc, ctx, expr, dot_prefix);
678 NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
682 fn complete_patterns(
683 acc: &mut Completions,
684 ctx: &CompletionContext<'_>,
685 pattern_ctx: &PatternContext,
687 flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
688 fn_param::complete_fn_param(acc, ctx, pattern_ctx);
689 pattern::complete_pattern(acc, ctx, pattern_ctx);
690 record::complete_record_pattern_fields(acc, ctx, pattern_ctx);