]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions.rs
Less strings, more hir::Names
[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 either::Either;
22 use hir::{known, HasVisibility};
23 use ide_db::SymbolKind;
24 use rustc_hash::FxHashSet;
25
26 use crate::{
27     item::{Builder, CompletionKind},
28     render::{
29         const_::render_const,
30         enum_variant::render_variant,
31         function::{render_fn, render_method},
32         macro_::render_macro,
33         pattern::{render_struct_pat, render_variant_pat},
34         render_field, render_resolution, render_tuple_field,
35         type_alias::render_type_alias,
36         RenderContext,
37     },
38     CompletionContext, CompletionItem, CompletionItemKind,
39 };
40
41 /// Represents an in-progress set of completions being built.
42 #[derive(Debug, Default)]
43 pub struct Completions {
44     buf: Vec<CompletionItem>,
45 }
46
47 impl Into<Vec<CompletionItem>> for Completions {
48     fn into(self) -> Vec<CompletionItem> {
49         self.buf
50     }
51 }
52
53 impl Builder {
54     /// Convenience method, which allows to add a freshly created completion into accumulator
55     /// without binding it to the variable.
56     pub(crate) fn add_to(self, acc: &mut Completions) {
57         acc.add(self.build())
58     }
59 }
60
61 impl Completions {
62     pub(crate) fn add(&mut self, item: CompletionItem) {
63         self.buf.push(item)
64     }
65
66     pub(crate) fn add_all<I>(&mut self, items: I)
67     where
68         I: IntoIterator,
69         I::Item: Into<CompletionItem>,
70     {
71         items.into_iter().for_each(|item| self.add(item.into()))
72     }
73
74     pub(crate) fn add_field(
75         &mut self,
76         ctx: &CompletionContext,
77         receiver: Option<hir::Name>,
78         field: hir::Field,
79         ty: &hir::Type,
80     ) {
81         let item = render_field(RenderContext::new(ctx), receiver, field, ty);
82         self.add(item);
83     }
84
85     pub(crate) fn add_tuple_field(
86         &mut self,
87         ctx: &CompletionContext,
88         receiver: Option<hir::Name>,
89         field: usize,
90         ty: &hir::Type,
91     ) {
92         let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
93         self.add(item);
94     }
95
96     pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
97         let mut item =
98             CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
99         item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
100         self.add(item.build());
101     }
102
103     pub(crate) fn add_resolution(
104         &mut self,
105         ctx: &CompletionContext,
106         local_name: hir::Name,
107         resolution: &hir::ScopeDef,
108     ) {
109         if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
110             self.add(item);
111         }
112     }
113
114     pub(crate) fn add_macro(
115         &mut self,
116         ctx: &CompletionContext,
117         name: Option<hir::Name>,
118         macro_: hir::MacroDef,
119     ) {
120         let name = match name {
121             Some(it) => it,
122             None => return,
123         };
124         if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
125             self.add(item);
126         }
127     }
128
129     pub(crate) fn add_function(
130         &mut self,
131         ctx: &CompletionContext,
132         func: hir::Function,
133         local_name: Option<hir::Name>,
134     ) {
135         if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
136             self.add(item)
137         }
138     }
139
140     pub(crate) fn add_method(
141         &mut self,
142         ctx: &CompletionContext,
143         func: hir::Function,
144         receiver: Option<hir::Name>,
145         local_name: Option<hir::Name>,
146     ) {
147         if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func)
148         {
149             self.add(item)
150         }
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         if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) {
160             self.add(item);
161         }
162     }
163
164     pub(crate) fn add_qualified_variant_pat(
165         &mut self,
166         ctx: &CompletionContext,
167         variant: hir::Variant,
168         path: hir::ModPath,
169     ) {
170         if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
171             self.add(item);
172         }
173     }
174
175     pub(crate) fn add_struct_pat(
176         &mut self,
177         ctx: &CompletionContext,
178         strukt: hir::Struct,
179         local_name: Option<hir::Name>,
180     ) {
181         if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) {
182             self.add(item);
183         }
184     }
185
186     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
187         if let Some(item) = render_const(RenderContext::new(ctx), constant) {
188             self.add(item);
189         }
190     }
191
192     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
193         if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
194             self.add(item)
195         }
196     }
197
198     pub(crate) fn add_qualified_enum_variant(
199         &mut self,
200         ctx: &CompletionContext,
201         variant: hir::Variant,
202         path: hir::ModPath,
203     ) {
204         let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
205         self.add(item);
206     }
207
208     pub(crate) fn add_enum_variant(
209         &mut self,
210         ctx: &CompletionContext,
211         variant: hir::Variant,
212         local_name: Option<hir::Name>,
213     ) {
214         let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
215         self.add(item);
216     }
217 }
218
219 fn complete_enum_variants(
220     acc: &mut Completions,
221     ctx: &CompletionContext,
222     enum_data: hir::Enum,
223     cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
224 ) {
225     let variants = enum_data.variants(ctx.db);
226
227     let module = if let Some(module) = ctx.scope.module() {
228         // Compute path from the completion site if available.
229         module
230     } else {
231         // Otherwise fall back to the enum's definition site.
232         enum_data.module(ctx.db)
233     };
234
235     if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
236         if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_data)) {
237             for &variant in &variants {
238                 let self_path = hir::ModPath::from_segments(
239                     hir::PathKind::Plain,
240                     iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
241                 );
242                 cb(acc, ctx, variant, self_path);
243             }
244         }
245     }
246
247     for variant in variants {
248         if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
249             // Variants with trivial paths are already added by the existing completion logic,
250             // so we should avoid adding these twice
251             if path.segments().len() > 1 {
252                 cb(acc, ctx, variant, path);
253             }
254         }
255     }
256 }
257
258 fn complete_fields(
259     ctx: &CompletionContext,
260     receiver: &hir::Type,
261     mut f: impl FnMut(Either<hir::Field, usize>, hir::Type),
262 ) {
263     for receiver in receiver.autoderef(ctx.db) {
264         for (field, ty) in receiver.fields(ctx.db) {
265             if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) {
266                 // Skip private field. FIXME: If the definition location of the
267                 // field is editable, we should show the completion
268                 continue;
269             }
270             f(Either::Left(field), ty);
271         }
272         for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() {
273             // FIXME: Handle visibility
274             f(Either::Right(i), ty);
275         }
276     }
277 }
278
279 fn complete_methods(
280     ctx: &CompletionContext,
281     receiver: &hir::Type,
282     mut f: impl FnMut(hir::Function),
283 ) {
284     if let Some(krate) = ctx.krate {
285         let mut seen_methods = FxHashSet::default();
286         let traits_in_scope = ctx.scope.traits_in_scope();
287         receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| {
288             if func.self_param(ctx.db).is_some()
289                 && ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m))
290                 && seen_methods.insert(func.name(ctx.db))
291             {
292                 f(func);
293             }
294             None::<()>
295         });
296     }
297 }