]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions.rs
feat: Add very simplistic ident completion for format_args! macro input
[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 pub(crate) mod format_string;
18
19 use std::iter;
20
21 use hir::known;
22 use ide_db::SymbolKind;
23
24 use crate::{
25     item::Builder,
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         struct_literal::render_struct_literal,
34         type_alias::{render_type_alias, render_type_alias_with_eq},
35         RenderContext,
36     },
37     CompletionContext, CompletionItem, CompletionItemKind,
38 };
39
40 /// Represents an in-progress set of completions being built.
41 #[derive(Debug, Default)]
42 pub struct Completions {
43     buf: Vec<CompletionItem>,
44 }
45
46 impl From<Completions> for Vec<CompletionItem> {
47     fn from(val: Completions) -> Self {
48         val.buf
49     }
50 }
51
52 impl Builder {
53     /// Convenience method, which allows to add a freshly created completion into accumulator
54     /// without binding it to the variable.
55     pub(crate) fn add_to(self, acc: &mut Completions) {
56         acc.add(self.build())
57     }
58 }
59
60 impl Completions {
61     fn add(&mut self, item: CompletionItem) {
62         self.buf.push(item)
63     }
64
65     fn add_opt(&mut self, item: Option<CompletionItem>) {
66         if let Some(item) = item {
67             self.buf.push(item)
68         }
69     }
70
71     pub(crate) fn add_all<I>(&mut self, items: I)
72     where
73         I: IntoIterator,
74         I::Item: Into<CompletionItem>,
75     {
76         items.into_iter().for_each(|item| self.add(item.into()))
77     }
78
79     pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext, keyword: &'static str) {
80         let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), 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         if ctx.is_scope_def_hidden(resolution) {
91             cov_mark::hit!(qualified_path_doc_hidden);
92             return;
93         }
94         self.add(render_resolution(RenderContext::new(ctx), local_name, resolution));
95     }
96
97     pub(crate) fn add_macro(
98         &mut self,
99         ctx: &CompletionContext,
100         name: Option<hir::Name>,
101         macro_: hir::MacroDef,
102     ) {
103         let name = match name {
104             Some(it) => it,
105             None => return,
106         };
107         self.add(render_macro(RenderContext::new(ctx), None, name, macro_));
108     }
109
110     pub(crate) fn add_function(
111         &mut self,
112         ctx: &CompletionContext,
113         func: hir::Function,
114         local_name: Option<hir::Name>,
115     ) {
116         if !ctx.is_visible(&func) {
117             return;
118         }
119         self.add(render_fn(RenderContext::new(ctx), None, local_name, func));
120     }
121
122     pub(crate) fn add_method(
123         &mut self,
124         ctx: &CompletionContext,
125         func: hir::Function,
126         receiver: Option<hir::Name>,
127         local_name: Option<hir::Name>,
128     ) {
129         if !ctx.is_visible(&func) {
130             return;
131         }
132         self.add(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
133     }
134
135     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, konst: hir::Const) {
136         if !ctx.is_visible(&konst) {
137             return;
138         }
139         self.add_opt(render_const(RenderContext::new(ctx), konst));
140     }
141
142     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
143         if !ctx.is_visible(&type_alias) {
144             return;
145         }
146         self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
147     }
148
149     pub(crate) fn add_type_alias_with_eq(
150         &mut self,
151         ctx: &CompletionContext,
152         type_alias: hir::TypeAlias,
153     ) {
154         self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
155     }
156
157     pub(crate) fn add_qualified_enum_variant(
158         &mut self,
159         ctx: &CompletionContext,
160         variant: hir::Variant,
161         path: hir::ModPath,
162     ) {
163         let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
164         self.add(item);
165     }
166
167     pub(crate) fn add_enum_variant(
168         &mut self,
169         ctx: &CompletionContext,
170         variant: hir::Variant,
171         local_name: Option<hir::Name>,
172     ) {
173         let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
174         self.add(item);
175     }
176
177     pub(crate) fn add_field(
178         &mut self,
179         ctx: &CompletionContext,
180         receiver: Option<hir::Name>,
181         field: hir::Field,
182         ty: &hir::Type,
183     ) {
184         if !ctx.is_visible(&field) {
185             return;
186         }
187         let item = render_field(RenderContext::new(ctx), receiver, field, ty);
188         self.add(item);
189     }
190
191     pub(crate) fn add_struct_literal(
192         &mut self,
193         ctx: &CompletionContext,
194         strukt: hir::Struct,
195         path: Option<hir::ModPath>,
196         local_name: Option<hir::Name>,
197     ) {
198         let item = render_struct_literal(RenderContext::new(ctx), strukt, path, local_name);
199         self.add_opt(item);
200     }
201
202     pub(crate) fn add_tuple_field(
203         &mut self,
204         ctx: &CompletionContext,
205         receiver: Option<hir::Name>,
206         field: usize,
207         ty: &hir::Type,
208     ) {
209         let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
210         self.add(item);
211     }
212
213     pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
214         let item = CompletionItem::new(SymbolKind::LifetimeParam, ctx.source_range(), "'static");
215         self.add(item.build());
216     }
217
218     pub(crate) fn add_variant_pat(
219         &mut self,
220         ctx: &CompletionContext,
221         variant: hir::Variant,
222         local_name: Option<hir::Name>,
223     ) {
224         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
225     }
226
227     pub(crate) fn add_qualified_variant_pat(
228         &mut self,
229         ctx: &CompletionContext,
230         variant: hir::Variant,
231         path: hir::ModPath,
232     ) {
233         self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
234     }
235
236     pub(crate) fn add_struct_pat(
237         &mut self,
238         ctx: &CompletionContext,
239         strukt: hir::Struct,
240         local_name: Option<hir::Name>,
241     ) {
242         self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
243     }
244 }
245
246 /// Calls the callback for each variant of the provided enum with the path to the variant.
247 /// Skips variants that are visible with single segment paths.
248 fn enum_variants_with_paths(
249     acc: &mut Completions,
250     ctx: &CompletionContext,
251     enum_: hir::Enum,
252     cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
253 ) {
254     let variants = enum_.variants(ctx.db);
255
256     let module = if let Some(module) = ctx.scope.module() {
257         // Compute path from the completion site if available.
258         module
259     } else {
260         // Otherwise fall back to the enum's definition site.
261         enum_.module(ctx.db)
262     };
263
264     if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
265         if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
266             for &variant in &variants {
267                 let self_path = hir::ModPath::from_segments(
268                     hir::PathKind::Plain,
269                     iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
270                 );
271                 cb(acc, ctx, variant, self_path);
272             }
273         }
274     }
275
276     for variant in variants {
277         if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
278             // Variants with trivial paths are already added by the existing completion logic,
279             // so we should avoid adding these twice
280             if path.segments().len() > 1 {
281                 cb(acc, ctx, variant, path);
282             }
283         }
284     }
285 }