]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions.rs
Merge #9264
[rust.git] / crates / ide_completion / src / completions.rs
1 //! This module defines an accumulator for completions which are going to be presented to user.
2
3 pub(crate) mod attribute;
4 pub(crate) mod dot;
5 pub(crate) mod flyimport;
6 pub(crate) mod fn_param;
7 pub(crate) mod keyword;
8 pub(crate) mod lifetime;
9 pub(crate) mod mod_;
10 pub(crate) mod pattern;
11 pub(crate) mod postfix;
12 pub(crate) mod qualified_path;
13 pub(crate) mod record;
14 pub(crate) mod snippet;
15 pub(crate) mod trait_impl;
16 pub(crate) mod unqualified_path;
17
18 use std::iter;
19
20 use hir::known;
21 use ide_db::SymbolKind;
22
23 use crate::{
24     item::{Builder, CompletionKind},
25     render::{
26         const_::render_const,
27         enum_variant::render_variant,
28         function::{render_fn, render_method},
29         macro_::render_macro,
30         pattern::{render_struct_pat, render_variant_pat},
31         render_field, render_resolution, render_tuple_field,
32         type_alias::{render_type_alias, render_type_alias_with_eq},
33         RenderContext,
34     },
35     CompletionContext, CompletionItem, CompletionItemKind,
36 };
37
38 /// Represents an in-progress set of completions being built.
39 #[derive(Debug, Default)]
40 pub struct Completions {
41     buf: Vec<CompletionItem>,
42 }
43
44 impl From<Completions> for Vec<CompletionItem> {
45     fn from(val: Completions) -> Self {
46         val.buf
47     }
48 }
49
50 impl Builder {
51     /// Convenience method, which allows to add a freshly created completion into accumulator
52     /// without binding it to the variable.
53     pub(crate) fn add_to(self, acc: &mut Completions) {
54         acc.add(self.build())
55     }
56 }
57
58 impl Completions {
59     fn add(&mut self, item: CompletionItem) {
60         self.buf.push(item)
61     }
62
63     fn add_opt(&mut self, item: Option<CompletionItem>) {
64         if let Some(item) = item {
65             self.buf.push(item)
66         }
67     }
68
69     pub(crate) fn add_all<I>(&mut self, items: I)
70     where
71         I: IntoIterator,
72         I::Item: Into<CompletionItem>,
73     {
74         items.into_iter().for_each(|item| self.add(item.into()))
75     }
76
77     pub(crate) fn add_resolution(
78         &mut self,
79         ctx: &CompletionContext,
80         local_name: hir::Name,
81         resolution: &hir::ScopeDef,
82     ) {
83         self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
84     }
85
86     pub(crate) fn add_macro(
87         &mut self,
88         ctx: &CompletionContext,
89         name: Option<hir::Name>,
90         macro_: hir::MacroDef,
91     ) {
92         let name = match name {
93             Some(it) => it,
94             None => return,
95         };
96         self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
97     }
98
99     pub(crate) fn add_function(
100         &mut self,
101         ctx: &CompletionContext,
102         func: hir::Function,
103         local_name: Option<hir::Name>,
104     ) {
105         self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
106     }
107
108     pub(crate) fn add_method(
109         &mut self,
110         ctx: &CompletionContext,
111         func: hir::Function,
112         receiver: Option<hir::Name>,
113         local_name: Option<hir::Name>,
114     ) {
115         self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
116     }
117
118     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
119         self.add_opt(render_const(RenderContext::new(ctx), constant));
120     }
121
122     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
123         self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
124     }
125
126     pub(crate) fn add_type_alias_with_eq(
127         &mut self,
128         ctx: &CompletionContext,
129         type_alias: hir::TypeAlias,
130     ) {
131         self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
132     }
133
134     pub(crate) fn add_qualified_enum_variant(
135         &mut self,
136         ctx: &CompletionContext,
137         variant: hir::Variant,
138         path: hir::ModPath,
139     ) {
140         let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
141         self.add(item);
142     }
143
144     pub(crate) fn add_enum_variant(
145         &mut self,
146         ctx: &CompletionContext,
147         variant: hir::Variant,
148         local_name: Option<hir::Name>,
149     ) {
150         let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
151         self.add(item);
152     }
153
154     pub(crate) fn add_field(
155         &mut self,
156         ctx: &CompletionContext,
157         receiver: Option<hir::Name>,
158         field: hir::Field,
159         ty: &hir::Type,
160     ) {
161         let item = render_field(RenderContext::new(ctx), receiver, field, ty);
162         self.add(item);
163     }
164
165     pub(crate) fn add_tuple_field(
166         &mut self,
167         ctx: &CompletionContext,
168         receiver: Option<hir::Name>,
169         field: usize,
170         ty: &hir::Type,
171     ) {
172         let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
173         self.add(item);
174     }
175
176     pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
177         let mut item =
178             CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
179         item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
180         self.add(item.build());
181     }
182
183     pub(crate) fn add_variant_pat(
184         &mut self,
185         ctx: &CompletionContext,
186         variant: hir::Variant,
187         local_name: Option<hir::Name>,
188     ) {
189         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
190     }
191
192     pub(crate) fn add_qualified_variant_pat(
193         &mut self,
194         ctx: &CompletionContext,
195         variant: hir::Variant,
196         path: hir::ModPath,
197     ) {
198         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
199     }
200
201     pub(crate) fn add_struct_pat(
202         &mut self,
203         ctx: &CompletionContext,
204         strukt: hir::Struct,
205         local_name: Option<hir::Name>,
206     ) {
207         self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
208     }
209 }
210
211 /// Calls the callback for each variant of the provided enum with the path to the variant.
212 /// Skips variants that are visible with single segment paths.
213 fn enum_variants_with_paths(
214     acc: &mut Completions,
215     ctx: &CompletionContext,
216     enum_: hir::Enum,
217     cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
218 ) {
219     let variants = enum_.variants(ctx.db);
220
221     let module = if let Some(module) = ctx.scope.module() {
222         // Compute path from the completion site if available.
223         module
224     } else {
225         // Otherwise fall back to the enum's definition site.
226         enum_.module(ctx.db)
227     };
228
229     if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
230         if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
231             for &variant in &variants {
232                 let self_path = hir::ModPath::from_segments(
233                     hir::PathKind::Plain,
234                     iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
235                 );
236                 cb(acc, ctx, variant, self_path);
237             }
238         }
239     }
240
241     for variant in variants {
242         if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
243             // Variants with trivial paths are already added by the existing completion logic,
244             // so we should avoid adding these twice
245             if path.segments().len() > 1 {
246                 cb(acc, ctx, variant, path);
247             }
248         }
249     }
250 }