]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/record.rs
parameters.split_last()
[rust.git] / crates / ide_completion / src / completions / record.rs
1 //! Complete fields in record literals and patterns.
2 use ide_db::SymbolKind;
3 use syntax::{ast::Expr, T};
4
5 use crate::{
6     patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind,
7     CompletionRelevance, Completions,
8 };
9
10 pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
11     let missing_fields = match &ctx.completion_location {
12         Some(
13             ImmediateLocation::RecordExpr(record_expr)
14             | ImmediateLocation::RecordExprUpdate(record_expr),
15         ) => {
16             let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_expr.clone()));
17             let default_trait = ctx.famous_defs().core_default_Default();
18             let impl_default_trait = default_trait.zip(ty).map_or(false, |(default_trait, ty)| {
19                 ty.original.impls_trait(ctx.db, default_trait, &[])
20             });
21
22             let missing_fields = ctx.sema.record_literal_missing_fields(record_expr);
23             if impl_default_trait && !missing_fields.is_empty() && ctx.path_qual().is_none() {
24                 let completion_text = "..Default::default()";
25                 let mut item =
26                     CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
27                 let completion_text =
28                     completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
29                 item.insert_text(completion_text).set_relevance(CompletionRelevance {
30                     exact_postfix_snippet_match: true,
31                     ..Default::default()
32                 });
33                 item.add_to(acc);
34             }
35             if ctx.previous_token_is(T![.]) {
36                 let mut item =
37                     CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
38                 item.insert_text(".");
39                 item.add_to(acc);
40                 return None;
41             }
42             missing_fields
43         }
44         Some(ImmediateLocation::RecordPat(record_pat)) => {
45             ctx.sema.record_pattern_missing_fields(record_pat)
46         }
47         _ => return None,
48     };
49
50     for (field, ty) in missing_fields {
51         acc.add_field(ctx, None, field, &ty);
52     }
53
54     Some(())
55 }
56
57 pub(crate) fn complete_record_literal(
58     acc: &mut Completions,
59     ctx: &CompletionContext,
60 ) -> Option<()> {
61     if !ctx.expects_expression() {
62         return None;
63     }
64
65     if let hir::Adt::Struct(strukt) = ctx.expected_type.as_ref()?.as_adt()? {
66         let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
67
68         let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
69
70         acc.add_struct_literal(ctx, strukt, path, None);
71     }
72
73     Some(())
74 }
75
76 #[cfg(test)]
77 mod tests {
78     use crate::tests::check_edit;
79
80     #[test]
81     fn literal_struct_completion_edit() {
82         check_edit(
83             "FooDesc {…}",
84             r#"
85 struct FooDesc { pub bar: bool }
86
87 fn create_foo(foo_desc: &FooDesc) -> () { () }
88
89 fn baz() {
90     let foo = create_foo(&$0);
91 }
92             "#,
93             r#"
94 struct FooDesc { pub bar: bool }
95
96 fn create_foo(foo_desc: &FooDesc) -> () { () }
97
98 fn baz() {
99     let foo = create_foo(&FooDesc { bar: ${1:()} }$0);
100 }
101             "#,
102         )
103     }
104
105     #[test]
106     fn literal_struct_completion_from_sub_modules() {
107         check_edit(
108             "Struct {…}",
109             r#"
110 mod submod {
111     pub struct Struct {
112         pub a: u64,
113     }
114 }
115
116 fn f() -> submod::Struct {
117     Stru$0
118 }
119             "#,
120             r#"
121 mod submod {
122     pub struct Struct {
123         pub a: u64,
124     }
125 }
126
127 fn f() -> submod::Struct {
128     submod::Struct { a: ${1:()} }$0
129 }
130             "#,
131         )
132     }
133
134     #[test]
135     fn literal_struct_complexion_module() {
136         check_edit(
137             "FooDesc {…}",
138             r#"
139 mod _69latrick {
140     pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
141     pub fn create_foo(foo_desc: &FooDesc) -> () { () }
142 }
143
144 fn baz() {
145     use _69latrick::*;
146
147     let foo = create_foo(&$0);
148 }
149             "#,
150             r#"
151 mod _69latrick {
152     pub struct FooDesc { pub six: bool, pub neuf: Vec<String>, pub bar: bool }
153     pub fn create_foo(foo_desc: &FooDesc) -> () { () }
154 }
155
156 fn baz() {
157     use _69latrick::*;
158
159     let foo = create_foo(&FooDesc { six: ${1:()}, neuf: ${2:()}, bar: ${3:()} }$0);
160 }
161             "#,
162         );
163     }
164
165     #[test]
166     fn default_completion_edit() {
167         check_edit(
168             "..Default::default()",
169             r#"
170 //- minicore: default
171 struct Struct { foo: u32, bar: usize }
172
173 impl Default for Struct {
174     fn default() -> Self {}
175 }
176
177 fn foo() {
178     let other = Struct {
179         foo: 5,
180         .$0
181     };
182 }
183 "#,
184             r#"
185 struct Struct { foo: u32, bar: usize }
186
187 impl Default for Struct {
188     fn default() -> Self {}
189 }
190
191 fn foo() {
192     let other = Struct {
193         foo: 5,
194         ..Default::default()
195     };
196 }
197 "#,
198         );
199         check_edit(
200             "..Default::default()",
201             r#"
202 //- minicore: default
203 struct Struct { foo: u32, bar: usize }
204
205 impl Default for Struct {
206     fn default() -> Self {}
207 }
208
209 fn foo() {
210     let other = Struct {
211         foo: 5,
212         $0
213     };
214 }
215 "#,
216             r#"
217 struct Struct { foo: u32, bar: usize }
218
219 impl Default for Struct {
220     fn default() -> Self {}
221 }
222
223 fn foo() {
224     let other = Struct {
225         foo: 5,
226         ..Default::default()
227     };
228 }
229 "#,
230         );
231         check_edit(
232             "..Default::default()",
233             r#"
234 //- minicore: default
235 struct Struct { foo: u32, bar: usize }
236
237 impl Default for Struct {
238     fn default() -> Self {}
239 }
240
241 fn foo() {
242     let other = Struct {
243         foo: 5,
244         ..$0
245     };
246 }
247 "#,
248             r#"
249 struct Struct { foo: u32, bar: usize }
250
251 impl Default for Struct {
252     fn default() -> Self {}
253 }
254
255 fn foo() {
256     let other = Struct {
257         foo: 5,
258         ..Default::default()
259     };
260 }
261 "#,
262         );
263     }
264 }