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