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