]> git.lizzy.rs Git - rust.git/blob - crates/completion/src/completions.rs
Higher-ranked trait bounds for where clauses
[rust.git] / crates / 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 record;
6 pub(crate) mod pattern;
7 pub(crate) mod fn_param;
8 pub(crate) mod keyword;
9 pub(crate) mod snippet;
10 pub(crate) mod qualified_path;
11 pub(crate) mod unqualified_path;
12 pub(crate) mod postfix;
13 pub(crate) mod macro_in_item_position;
14 pub(crate) mod trait_impl;
15 pub(crate) mod mod_;
16
17 use hir::{ModPath, ScopeDef, Type};
18
19 use crate::{
20     item::Builder,
21     render::{
22         const_::render_const, enum_variant::render_enum_variant, function::render_fn,
23         macro_::render_macro, render_field, render_resolution, render_tuple_field,
24         type_alias::render_type_alias, RenderContext,
25     },
26     CompletionContext, CompletionItem,
27 };
28
29 /// Represents an in-progress set of completions being built.
30 #[derive(Debug, Default)]
31 pub struct Completions {
32     buf: Vec<CompletionItem>,
33 }
34
35 impl Into<Vec<CompletionItem>> for Completions {
36     fn into(self) -> Vec<CompletionItem> {
37         self.buf
38     }
39 }
40
41 impl Builder {
42     /// Convenience method, which allows to add a freshly created completion into accumulator
43     /// without binding it to the variable.
44     pub(crate) fn add_to(self, acc: &mut Completions) {
45         acc.add(self.build())
46     }
47 }
48
49 impl Completions {
50     pub(crate) fn add(&mut self, item: CompletionItem) {
51         self.buf.push(item.into())
52     }
53
54     pub(crate) fn add_all<I>(&mut self, items: I)
55     where
56         I: IntoIterator,
57         I::Item: Into<CompletionItem>,
58     {
59         items.into_iter().for_each(|item| self.add(item.into()))
60     }
61
62     pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
63         let item = render_field(RenderContext::new(ctx), field, ty);
64         self.add(item);
65     }
66
67     pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
68         let item = render_tuple_field(RenderContext::new(ctx), field, ty);
69         self.add(item);
70     }
71
72     pub(crate) fn add_resolution(
73         &mut self,
74         ctx: &CompletionContext,
75         local_name: String,
76         resolution: &ScopeDef,
77     ) {
78         if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
79             self.add(item);
80         }
81     }
82
83     pub(crate) fn add_macro(
84         &mut self,
85         ctx: &CompletionContext,
86         name: Option<String>,
87         macro_: hir::MacroDef,
88     ) {
89         let name = match name {
90             Some(it) => it,
91             None => return,
92         };
93         if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
94             self.add(item);
95         }
96     }
97
98     pub(crate) fn add_function(
99         &mut self,
100         ctx: &CompletionContext,
101         func: hir::Function,
102         local_name: Option<String>,
103     ) {
104         let item = render_fn(RenderContext::new(ctx), None, local_name, func);
105         self.add(item)
106     }
107
108     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
109         if let Some(item) = render_const(RenderContext::new(ctx), constant) {
110             self.add(item);
111         }
112     }
113
114     pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
115         if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
116             self.add(item)
117         }
118     }
119
120     pub(crate) fn add_qualified_enum_variant(
121         &mut self,
122         ctx: &CompletionContext,
123         variant: hir::EnumVariant,
124         path: ModPath,
125     ) {
126         let item = render_enum_variant(RenderContext::new(ctx), None, None, variant, Some(path));
127         self.add(item);
128     }
129
130     pub(crate) fn add_enum_variant(
131         &mut self,
132         ctx: &CompletionContext,
133         variant: hir::EnumVariant,
134         local_name: Option<String>,
135     ) {
136         let item = render_enum_variant(RenderContext::new(ctx), None, local_name, variant, None);
137         self.add(item);
138     }
139 }