]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions.rs
Merge #9526
[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 From<Completions> for Vec<CompletionItem> {
45     fn from(val: Completions) -> Self {
46         val.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_keyword(&mut self, ctx: &CompletionContext, keyword: &'static str) {
78         let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), keyword);
79         item.kind(CompletionItemKind::Keyword);
80         item.add_to(self);
81     }
82
83     pub(crate) fn add_resolution(
84         &mut self,
85         ctx: &CompletionContext,
86         local_name: hir::Name,
87         resolution: &hir::ScopeDef,
88     ) {
89         self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
90     }
91
92     pub(crate) fn add_macro(
93         &mut self,
94         ctx: &CompletionContext,
95         name: Option<hir::Name>,
96         macro_: hir::MacroDef,
97     ) {
98         let name = match name {
99             Some(it) => it,
100             None => return,
101         };
102         self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
103     }
104
105     pub(crate) fn add_function(
106         &mut self,
107         ctx: &CompletionContext,
108         func: hir::Function,
109         local_name: Option<hir::Name>,
110     ) {
111         self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
112     }
113
114     pub(crate) fn add_method(
115         &mut self,
116         ctx: &CompletionContext,
117         func: hir::Function,
118         receiver: Option<hir::Name>,
119         local_name: Option<hir::Name>,
120     ) {
121         self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
122     }
123
124     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
125         self.add_opt(render_const(RenderContext::new(ctx), constant));
126     }
127
128     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
129         self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
130     }
131
132     pub(crate) fn add_type_alias_with_eq(
133         &mut self,
134         ctx: &CompletionContext,
135         type_alias: hir::TypeAlias,
136     ) {
137         self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
138     }
139
140     pub(crate) fn add_qualified_enum_variant(
141         &mut self,
142         ctx: &CompletionContext,
143         variant: hir::Variant,
144         path: hir::ModPath,
145     ) {
146         let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
147         self.add(item);
148     }
149
150     pub(crate) fn add_enum_variant(
151         &mut self,
152         ctx: &CompletionContext,
153         variant: hir::Variant,
154         local_name: Option<hir::Name>,
155     ) {
156         let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
157         self.add(item);
158     }
159
160     pub(crate) fn add_field(
161         &mut self,
162         ctx: &CompletionContext,
163         receiver: Option<hir::Name>,
164         field: hir::Field,
165         ty: &hir::Type,
166     ) {
167         let item = render_field(RenderContext::new(ctx), receiver, field, ty);
168         self.add(item);
169     }
170
171     pub(crate) fn add_tuple_field(
172         &mut self,
173         ctx: &CompletionContext,
174         receiver: Option<hir::Name>,
175         field: usize,
176         ty: &hir::Type,
177     ) {
178         let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
179         self.add(item);
180     }
181
182     pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
183         let mut item =
184             CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
185         item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
186         self.add(item.build());
187     }
188
189     pub(crate) fn add_variant_pat(
190         &mut self,
191         ctx: &CompletionContext,
192         variant: hir::Variant,
193         local_name: Option<hir::Name>,
194     ) {
195         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
196     }
197
198     pub(crate) fn add_qualified_variant_pat(
199         &mut self,
200         ctx: &CompletionContext,
201         variant: hir::Variant,
202         path: hir::ModPath,
203     ) {
204         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
205     }
206
207     pub(crate) fn add_struct_pat(
208         &mut self,
209         ctx: &CompletionContext,
210         strukt: hir::Struct,
211         local_name: Option<hir::Name>,
212     ) {
213         self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
214     }
215 }
216
217 /// Calls the callback for each variant of the provided enum with the path to the variant.
218 /// Skips variants that are visible with single segment paths.
219 fn enum_variants_with_paths(
220     acc: &mut Completions,
221     ctx: &CompletionContext,
222     enum_: hir::Enum,
223     cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
224 ) {
225     let variants = enum_.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_.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_)) {
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 }