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