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