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