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