]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions.rs
Add completion for struct literal in which all fields are visible.
[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         struct_literal::render_struct_literal,
33         type_alias::{render_type_alias, render_type_alias_with_eq},
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 From<Completions> for Vec<CompletionItem> {
46     fn from(val: Completions) -> Self {
47         val.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     fn add(&mut self, item: CompletionItem) {
61         self.buf.push(item)
62     }
63
64     fn add_opt(&mut self, item: Option<CompletionItem>) {
65         if let Some(item) = item {
66             self.buf.push(item)
67         }
68     }
69
70     pub(crate) fn add_all<I>(&mut self, items: I)
71     where
72         I: IntoIterator,
73         I::Item: Into<CompletionItem>,
74     {
75         items.into_iter().for_each(|item| self.add(item.into()))
76     }
77
78     pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext, keyword: &'static str) {
79         let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), keyword);
80         item.kind(CompletionItemKind::Keyword);
81         item.add_to(self);
82     }
83
84     pub(crate) fn add_resolution(
85         &mut self,
86         ctx: &CompletionContext,
87         local_name: hir::Name,
88         resolution: &hir::ScopeDef,
89     ) {
90         self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
91     }
92
93     pub(crate) fn add_macro(
94         &mut self,
95         ctx: &CompletionContext,
96         name: Option<hir::Name>,
97         macro_: hir::MacroDef,
98     ) {
99         let name = match name {
100             Some(it) => it,
101             None => return,
102         };
103         self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
104     }
105
106     pub(crate) fn add_function(
107         &mut self,
108         ctx: &CompletionContext,
109         func: hir::Function,
110         local_name: Option<hir::Name>,
111     ) {
112         self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
113     }
114
115     pub(crate) fn add_method(
116         &mut self,
117         ctx: &CompletionContext,
118         func: hir::Function,
119         receiver: Option<hir::Name>,
120         local_name: Option<hir::Name>,
121     ) {
122         self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
123     }
124
125     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
126         self.add_opt(render_const(RenderContext::new(ctx), constant));
127     }
128
129     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
130         self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
131     }
132
133     pub(crate) fn add_type_alias_with_eq(
134         &mut self,
135         ctx: &CompletionContext,
136         type_alias: hir::TypeAlias,
137     ) {
138         self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
139     }
140
141     pub(crate) fn add_qualified_enum_variant(
142         &mut self,
143         ctx: &CompletionContext,
144         variant: hir::Variant,
145         path: hir::ModPath,
146     ) {
147         let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
148         self.add(item);
149     }
150
151     pub(crate) fn add_enum_variant(
152         &mut self,
153         ctx: &CompletionContext,
154         variant: hir::Variant,
155         local_name: Option<hir::Name>,
156     ) {
157         let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
158         self.add(item);
159     }
160
161     pub(crate) fn add_field(
162         &mut self,
163         ctx: &CompletionContext,
164         receiver: Option<hir::Name>,
165         field: hir::Field,
166         ty: &hir::Type,
167     ) {
168         let item = render_field(RenderContext::new(ctx), receiver, field, ty);
169         self.add(item);
170     }
171
172     pub(crate) fn add_struct_literal(
173         &mut self,
174         ctx: &CompletionContext,
175         strukt: hir::Struct,
176         local_name: Option<hir::Name>,
177     ) {
178         let item = render_struct_literal(RenderContext::new(ctx), strukt, local_name);
179         self.add_opt(item);
180     }
181
182     pub(crate) fn add_tuple_field(
183         &mut self,
184         ctx: &CompletionContext,
185         receiver: Option<hir::Name>,
186         field: usize,
187         ty: &hir::Type,
188     ) {
189         let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
190         self.add(item);
191     }
192
193     pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
194         let mut item =
195             CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
196         item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
197         self.add(item.build());
198     }
199
200     pub(crate) fn add_variant_pat(
201         &mut self,
202         ctx: &CompletionContext,
203         variant: hir::Variant,
204         local_name: Option<hir::Name>,
205     ) {
206         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
207     }
208
209     pub(crate) fn add_qualified_variant_pat(
210         &mut self,
211         ctx: &CompletionContext,
212         variant: hir::Variant,
213         path: hir::ModPath,
214     ) {
215         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
216     }
217
218     pub(crate) fn add_struct_pat(
219         &mut self,
220         ctx: &CompletionContext,
221         strukt: hir::Struct,
222         local_name: Option<hir::Name>,
223     ) {
224         self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
225     }
226 }
227
228 /// Calls the callback for each variant of the provided enum with the path to the variant.
229 /// Skips variants that are visible with single segment paths.
230 fn enum_variants_with_paths(
231     acc: &mut Completions,
232     ctx: &CompletionContext,
233     enum_: hir::Enum,
234     cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
235 ) {
236     let variants = enum_.variants(ctx.db);
237
238     let module = if let Some(module) = ctx.scope.module() {
239         // Compute path from the completion site if available.
240         module
241     } else {
242         // Otherwise fall back to the enum's definition site.
243         enum_.module(ctx.db)
244     };
245
246     if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
247         if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
248             for &variant in &variants {
249                 let self_path = hir::ModPath::from_segments(
250                     hir::PathKind::Plain,
251                     iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
252                 );
253                 cb(acc, ctx, variant, self_path);
254             }
255         }
256     }
257
258     for variant in variants {
259         if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
260             // Variants with trivial paths are already added by the existing completion logic,
261             // so we should avoid adding these twice
262             if path.segments().len() > 1 {
263                 cb(acc, ctx, variant, path);
264             }
265         }
266     }
267 }