diff options
Diffstat (limited to 'crates/completion')
23 files changed, 8398 insertions, 0 deletions
diff --git a/crates/completion/Cargo.toml b/crates/completion/Cargo.toml new file mode 100644 index 000000000..b79ee33f7 --- /dev/null +++ b/crates/completion/Cargo.toml | |||
@@ -0,0 +1,30 @@ | |||
1 | [package] | ||
2 | name = "completion" | ||
3 | version = "0.0.0" | ||
4 | description = "TBD" | ||
5 | license = "MIT OR Apache-2.0" | ||
6 | authors = ["rust-analyzer developers"] | ||
7 | edition = "2018" | ||
8 | |||
9 | [lib] | ||
10 | doctest = false | ||
11 | |||
12 | [dependencies] | ||
13 | itertools = "0.9.0" | ||
14 | log = "0.4.8" | ||
15 | rustc-hash = "1.1.0" | ||
16 | |||
17 | stdx = { path = "../stdx", version = "0.0.0" } | ||
18 | syntax = { path = "../syntax", version = "0.0.0" } | ||
19 | text_edit = { path = "../text_edit", version = "0.0.0" } | ||
20 | base_db = { path = "../base_db", version = "0.0.0" } | ||
21 | ide_db = { path = "../ide_db", version = "0.0.0" } | ||
22 | profile = { path = "../profile", version = "0.0.0" } | ||
23 | test_utils = { path = "../test_utils", version = "0.0.0" } | ||
24 | |||
25 | # completions crate should depend only on the top-level `hir` package. if you need | ||
26 | # something from some `hir_xxx` subpackage, reexport the API via `hir`. | ||
27 | hir = { path = "../hir", version = "0.0.0" } | ||
28 | |||
29 | [dev-dependencies] | ||
30 | expect-test = "1.0" | ||
diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs new file mode 100644 index 000000000..d5fb85b79 --- /dev/null +++ b/crates/completion/src/completions.rs | |||
@@ -0,0 +1,1452 @@ | |||
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::{HasAttrs, HasSource, HirDisplay, ModPath, Mutability, ScopeDef, StructKind, Type}; | ||
18 | use itertools::Itertools; | ||
19 | use syntax::{ast::NameOwner, display::*}; | ||
20 | use test_utils::mark; | ||
21 | |||
22 | use crate::{ | ||
23 | item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, | ||
24 | CompletionScore, RootDatabase, | ||
25 | }; | ||
26 | |||
27 | /// Represents an in-progress set of completions being built. | ||
28 | #[derive(Debug, Default)] | ||
29 | pub struct Completions { | ||
30 | buf: Vec<CompletionItem>, | ||
31 | } | ||
32 | |||
33 | impl Into<Vec<CompletionItem>> for Completions { | ||
34 | fn into(self) -> Vec<CompletionItem> { | ||
35 | self.buf | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl Builder { | ||
40 | /// Convenience method, which allows to add a freshly created completion into accumulator | ||
41 | /// without binding it to the variable. | ||
42 | pub(crate) fn add_to(self, acc: &mut Completions) { | ||
43 | acc.add(self.build()) | ||
44 | } | ||
45 | } | ||
46 | |||
47 | impl Completions { | ||
48 | pub(crate) fn add(&mut self, item: CompletionItem) { | ||
49 | self.buf.push(item.into()) | ||
50 | } | ||
51 | |||
52 | pub(crate) fn add_all<I>(&mut self, items: I) | ||
53 | where | ||
54 | I: IntoIterator, | ||
55 | I::Item: Into<CompletionItem>, | ||
56 | { | ||
57 | items.into_iter().for_each(|item| self.add(item.into())) | ||
58 | } | ||
59 | |||
60 | pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) { | ||
61 | let is_deprecated = is_deprecated(field, ctx.db); | ||
62 | let name = field.name(ctx.db); | ||
63 | let mut item = | ||
64 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string()) | ||
65 | .kind(CompletionItemKind::Field) | ||
66 | .detail(ty.display(ctx.db).to_string()) | ||
67 | .set_documentation(field.docs(ctx.db)) | ||
68 | .set_deprecated(is_deprecated); | ||
69 | |||
70 | if let Some(score) = compute_score(ctx, &ty, &name.to_string()) { | ||
71 | item = item.set_score(score); | ||
72 | } | ||
73 | |||
74 | item.add_to(self); | ||
75 | } | ||
76 | |||
77 | pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) { | ||
78 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), field.to_string()) | ||
79 | .kind(CompletionItemKind::Field) | ||
80 | .detail(ty.display(ctx.db).to_string()) | ||
81 | .add_to(self); | ||
82 | } | ||
83 | |||
84 | pub(crate) fn add_resolution( | ||
85 | &mut self, | ||
86 | ctx: &CompletionContext, | ||
87 | local_name: String, | ||
88 | resolution: &ScopeDef, | ||
89 | ) { | ||
90 | use hir::ModuleDef::*; | ||
91 | |||
92 | let completion_kind = match resolution { | ||
93 | ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType, | ||
94 | _ => CompletionKind::Reference, | ||
95 | }; | ||
96 | |||
97 | let kind = match resolution { | ||
98 | ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module, | ||
99 | ScopeDef::ModuleDef(Function(func)) => { | ||
100 | self.add_function(ctx, *func, Some(local_name)); | ||
101 | return; | ||
102 | } | ||
103 | ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct, | ||
104 | // FIXME: add CompletionItemKind::Union | ||
105 | ScopeDef::ModuleDef(Adt(hir::Adt::Union(_))) => CompletionItemKind::Struct, | ||
106 | ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum, | ||
107 | |||
108 | ScopeDef::ModuleDef(EnumVariant(var)) => { | ||
109 | self.add_enum_variant(ctx, *var, Some(local_name)); | ||
110 | return; | ||
111 | } | ||
112 | ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const, | ||
113 | ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static, | ||
114 | ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::Trait, | ||
115 | ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::TypeAlias, | ||
116 | ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType, | ||
117 | ScopeDef::GenericParam(..) => CompletionItemKind::TypeParam, | ||
118 | ScopeDef::Local(..) => CompletionItemKind::Binding, | ||
119 | // (does this need its own kind?) | ||
120 | ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam, | ||
121 | ScopeDef::MacroDef(mac) => { | ||
122 | self.add_macro(ctx, Some(local_name), *mac); | ||
123 | return; | ||
124 | } | ||
125 | ScopeDef::Unknown => { | ||
126 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) | ||
127 | .kind(CompletionItemKind::UnresolvedReference) | ||
128 | .add_to(self); | ||
129 | return; | ||
130 | } | ||
131 | }; | ||
132 | |||
133 | let docs = match resolution { | ||
134 | ScopeDef::ModuleDef(Module(it)) => it.docs(ctx.db), | ||
135 | ScopeDef::ModuleDef(Adt(it)) => it.docs(ctx.db), | ||
136 | ScopeDef::ModuleDef(EnumVariant(it)) => it.docs(ctx.db), | ||
137 | ScopeDef::ModuleDef(Const(it)) => it.docs(ctx.db), | ||
138 | ScopeDef::ModuleDef(Static(it)) => it.docs(ctx.db), | ||
139 | ScopeDef::ModuleDef(Trait(it)) => it.docs(ctx.db), | ||
140 | ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(ctx.db), | ||
141 | _ => None, | ||
142 | }; | ||
143 | |||
144 | let mut item = CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone()); | ||
145 | if let ScopeDef::Local(local) = resolution { | ||
146 | let ty = local.ty(ctx.db); | ||
147 | if !ty.is_unknown() { | ||
148 | item = item.detail(ty.display(ctx.db).to_string()); | ||
149 | } | ||
150 | }; | ||
151 | |||
152 | let mut ref_match = None; | ||
153 | if let ScopeDef::Local(local) = resolution { | ||
154 | if let Some((active_name, active_type)) = ctx.active_name_and_type() { | ||
155 | let ty = local.ty(ctx.db); | ||
156 | if let Some(score) = | ||
157 | compute_score_from_active(&active_type, &active_name, &ty, &local_name) | ||
158 | { | ||
159 | item = item.set_score(score); | ||
160 | } | ||
161 | ref_match = refed_type_matches(&active_type, &active_name, &ty, &local_name); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | // Add `<>` for generic types | ||
166 | if ctx.is_path_type && !ctx.has_type_args && ctx.config.add_call_parenthesis { | ||
167 | if let Some(cap) = ctx.config.snippet_cap { | ||
168 | let has_non_default_type_params = match resolution { | ||
169 | ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(ctx.db), | ||
170 | ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(ctx.db), | ||
171 | _ => false, | ||
172 | }; | ||
173 | if has_non_default_type_params { | ||
174 | mark::hit!(inserts_angle_brackets_for_generics); | ||
175 | item = item | ||
176 | .lookup_by(local_name.clone()) | ||
177 | .label(format!("{}<…>", local_name)) | ||
178 | .insert_snippet(cap, format!("{}<$0>", local_name)); | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | item.kind(kind).set_documentation(docs).set_ref_match(ref_match).add_to(self) | ||
184 | } | ||
185 | |||
186 | pub(crate) fn add_macro( | ||
187 | &mut self, | ||
188 | ctx: &CompletionContext, | ||
189 | name: Option<String>, | ||
190 | macro_: hir::MacroDef, | ||
191 | ) { | ||
192 | // FIXME: Currently proc-macro do not have ast-node, | ||
193 | // such that it does not have source | ||
194 | if macro_.is_proc_macro() { | ||
195 | return; | ||
196 | } | ||
197 | |||
198 | let name = match name { | ||
199 | Some(it) => it, | ||
200 | None => return, | ||
201 | }; | ||
202 | |||
203 | let ast_node = macro_.source(ctx.db).value; | ||
204 | let detail = macro_label(&ast_node); | ||
205 | |||
206 | let docs = macro_.docs(ctx.db); | ||
207 | |||
208 | let mut builder = CompletionItem::new( | ||
209 | CompletionKind::Reference, | ||
210 | ctx.source_range(), | ||
211 | &format!("{}!", name), | ||
212 | ) | ||
213 | .kind(CompletionItemKind::Macro) | ||
214 | .set_documentation(docs.clone()) | ||
215 | .set_deprecated(is_deprecated(macro_, ctx.db)) | ||
216 | .detail(detail); | ||
217 | |||
218 | let needs_bang = ctx.use_item_syntax.is_none() && !ctx.is_macro_call; | ||
219 | builder = match ctx.config.snippet_cap { | ||
220 | Some(cap) if needs_bang => { | ||
221 | let docs = docs.as_ref().map_or("", |s| s.as_str()); | ||
222 | let (bra, ket) = guess_macro_braces(&name, docs); | ||
223 | builder | ||
224 | .insert_snippet(cap, format!("{}!{}$0{}", name, bra, ket)) | ||
225 | .label(format!("{}!{}…{}", name, bra, ket)) | ||
226 | .lookup_by(format!("{}!", name)) | ||
227 | } | ||
228 | None if needs_bang => builder.insert_text(format!("{}!", name)), | ||
229 | _ => { | ||
230 | mark::hit!(dont_insert_macro_call_parens_unncessary); | ||
231 | builder.insert_text(name) | ||
232 | } | ||
233 | }; | ||
234 | |||
235 | self.add(builder.build()); | ||
236 | } | ||
237 | |||
238 | pub(crate) fn add_function( | ||
239 | &mut self, | ||
240 | ctx: &CompletionContext, | ||
241 | func: hir::Function, | ||
242 | local_name: Option<String>, | ||
243 | ) { | ||
244 | fn add_arg(arg: &str, ty: &Type, ctx: &CompletionContext) -> String { | ||
245 | if let Some(derefed_ty) = ty.remove_ref() { | ||
246 | for (name, local) in ctx.locals.iter() { | ||
247 | if name == arg && local.ty(ctx.db) == derefed_ty { | ||
248 | return (if ty.is_mutable_reference() { "&mut " } else { "&" }).to_string() | ||
249 | + &arg.to_string(); | ||
250 | } | ||
251 | } | ||
252 | } | ||
253 | arg.to_string() | ||
254 | }; | ||
255 | let name = local_name.unwrap_or_else(|| func.name(ctx.db).to_string()); | ||
256 | let ast_node = func.source(ctx.db).value; | ||
257 | |||
258 | let mut builder = | ||
259 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) | ||
260 | .kind(if func.self_param(ctx.db).is_some() { | ||
261 | CompletionItemKind::Method | ||
262 | } else { | ||
263 | CompletionItemKind::Function | ||
264 | }) | ||
265 | .set_documentation(func.docs(ctx.db)) | ||
266 | .set_deprecated(is_deprecated(func, ctx.db)) | ||
267 | .detail(function_declaration(&ast_node)); | ||
268 | |||
269 | let params_ty = func.params(ctx.db); | ||
270 | let params = ast_node | ||
271 | .param_list() | ||
272 | .into_iter() | ||
273 | .flat_map(|it| it.params()) | ||
274 | .zip(params_ty) | ||
275 | .flat_map(|(it, param_ty)| { | ||
276 | if let Some(pat) = it.pat() { | ||
277 | let name = pat.to_string(); | ||
278 | let arg = name.trim_start_matches("mut ").trim_start_matches('_'); | ||
279 | return Some(add_arg(arg, param_ty.ty(), ctx)); | ||
280 | } | ||
281 | None | ||
282 | }) | ||
283 | .collect(); | ||
284 | |||
285 | builder = builder.add_call_parens(ctx, name, Params::Named(params)); | ||
286 | |||
287 | self.add(builder.build()) | ||
288 | } | ||
289 | |||
290 | pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { | ||
291 | let ast_node = constant.source(ctx.db).value; | ||
292 | let name = match ast_node.name() { | ||
293 | Some(name) => name, | ||
294 | _ => return, | ||
295 | }; | ||
296 | let detail = const_label(&ast_node); | ||
297 | |||
298 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | ||
299 | .kind(CompletionItemKind::Const) | ||
300 | .set_documentation(constant.docs(ctx.db)) | ||
301 | .set_deprecated(is_deprecated(constant, ctx.db)) | ||
302 | .detail(detail) | ||
303 | .add_to(self); | ||
304 | } | ||
305 | |||
306 | pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { | ||
307 | let type_def = type_alias.source(ctx.db).value; | ||
308 | let name = match type_def.name() { | ||
309 | Some(name) => name, | ||
310 | _ => return, | ||
311 | }; | ||
312 | let detail = type_label(&type_def); | ||
313 | |||
314 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) | ||
315 | .kind(CompletionItemKind::TypeAlias) | ||
316 | .set_documentation(type_alias.docs(ctx.db)) | ||
317 | .set_deprecated(is_deprecated(type_alias, ctx.db)) | ||
318 | .detail(detail) | ||
319 | .add_to(self); | ||
320 | } | ||
321 | |||
322 | pub(crate) fn add_qualified_enum_variant( | ||
323 | &mut self, | ||
324 | ctx: &CompletionContext, | ||
325 | variant: hir::EnumVariant, | ||
326 | path: ModPath, | ||
327 | ) { | ||
328 | self.add_enum_variant_impl(ctx, variant, None, Some(path)) | ||
329 | } | ||
330 | |||
331 | pub(crate) fn add_enum_variant( | ||
332 | &mut self, | ||
333 | ctx: &CompletionContext, | ||
334 | variant: hir::EnumVariant, | ||
335 | local_name: Option<String>, | ||
336 | ) { | ||
337 | self.add_enum_variant_impl(ctx, variant, local_name, None) | ||
338 | } | ||
339 | |||
340 | fn add_enum_variant_impl( | ||
341 | &mut self, | ||
342 | ctx: &CompletionContext, | ||
343 | variant: hir::EnumVariant, | ||
344 | local_name: Option<String>, | ||
345 | path: Option<ModPath>, | ||
346 | ) { | ||
347 | let is_deprecated = is_deprecated(variant, ctx.db); | ||
348 | let name = local_name.unwrap_or_else(|| variant.name(ctx.db).to_string()); | ||
349 | let (qualified_name, short_qualified_name) = match &path { | ||
350 | Some(path) => { | ||
351 | let full = path.to_string(); | ||
352 | let short = | ||
353 | path.segments[path.segments.len().saturating_sub(2)..].iter().join("::"); | ||
354 | (full, short) | ||
355 | } | ||
356 | None => (name.to_string(), name.to_string()), | ||
357 | }; | ||
358 | let detail_types = variant | ||
359 | .fields(ctx.db) | ||
360 | .into_iter() | ||
361 | .map(|field| (field.name(ctx.db), field.signature_ty(ctx.db))); | ||
362 | let variant_kind = variant.kind(ctx.db); | ||
363 | let detail = match variant_kind { | ||
364 | StructKind::Tuple | StructKind::Unit => format!( | ||
365 | "({})", | ||
366 | detail_types.map(|(_, t)| t.display(ctx.db).to_string()).format(", ") | ||
367 | ), | ||
368 | StructKind::Record => format!( | ||
369 | "{{ {} }}", | ||
370 | detail_types | ||
371 | .map(|(n, t)| format!("{}: {}", n, t.display(ctx.db).to_string())) | ||
372 | .format(", ") | ||
373 | ), | ||
374 | }; | ||
375 | let mut res = CompletionItem::new( | ||
376 | CompletionKind::Reference, | ||
377 | ctx.source_range(), | ||
378 | qualified_name.clone(), | ||
379 | ) | ||
380 | .kind(CompletionItemKind::EnumVariant) | ||
381 | .set_documentation(variant.docs(ctx.db)) | ||
382 | .set_deprecated(is_deprecated) | ||
383 | .detail(detail); | ||
384 | |||
385 | if variant_kind == StructKind::Tuple { | ||
386 | mark::hit!(inserts_parens_for_tuple_enums); | ||
387 | let params = Params::Anonymous(variant.fields(ctx.db).len()); | ||
388 | res = res.add_call_parens(ctx, short_qualified_name, params) | ||
389 | } else if path.is_some() { | ||
390 | res = res.lookup_by(short_qualified_name); | ||
391 | } | ||
392 | |||
393 | res.add_to(self); | ||
394 | } | ||
395 | } | ||
396 | |||
397 | fn compute_score_from_active( | ||
398 | active_type: &Type, | ||
399 | active_name: &str, | ||
400 | ty: &Type, | ||
401 | name: &str, | ||
402 | ) -> Option<CompletionScore> { | ||
403 | // Compute score | ||
404 | // For the same type | ||
405 | if active_type != ty { | ||
406 | return None; | ||
407 | } | ||
408 | |||
409 | let mut res = CompletionScore::TypeMatch; | ||
410 | |||
411 | // If same type + same name then go top position | ||
412 | if active_name == name { | ||
413 | res = CompletionScore::TypeAndNameMatch | ||
414 | } | ||
415 | |||
416 | Some(res) | ||
417 | } | ||
418 | fn refed_type_matches( | ||
419 | active_type: &Type, | ||
420 | active_name: &str, | ||
421 | ty: &Type, | ||
422 | name: &str, | ||
423 | ) -> Option<(Mutability, CompletionScore)> { | ||
424 | let derefed_active = active_type.remove_ref()?; | ||
425 | let score = compute_score_from_active(&derefed_active, &active_name, &ty, &name)?; | ||
426 | Some(( | ||
427 | if active_type.is_mutable_reference() { Mutability::Mut } else { Mutability::Shared }, | ||
428 | score, | ||
429 | )) | ||
430 | } | ||
431 | |||
432 | fn compute_score(ctx: &CompletionContext, ty: &Type, name: &str) -> Option<CompletionScore> { | ||
433 | let (active_name, active_type) = ctx.active_name_and_type()?; | ||
434 | compute_score_from_active(&active_type, &active_name, ty, name) | ||
435 | } | ||
436 | |||
437 | enum Params { | ||
438 | Named(Vec<String>), | ||
439 | Anonymous(usize), | ||
440 | } | ||
441 | |||
442 | impl Params { | ||
443 | fn len(&self) -> usize { | ||
444 | match self { | ||
445 | Params::Named(xs) => xs.len(), | ||
446 | Params::Anonymous(len) => *len, | ||
447 | } | ||
448 | } | ||
449 | |||
450 | fn is_empty(&self) -> bool { | ||
451 | self.len() == 0 | ||
452 | } | ||
453 | } | ||
454 | |||
455 | impl Builder { | ||
456 | fn add_call_parens(mut self, ctx: &CompletionContext, name: String, params: Params) -> Builder { | ||
457 | if !ctx.config.add_call_parenthesis { | ||
458 | return self; | ||
459 | } | ||
460 | if ctx.use_item_syntax.is_some() { | ||
461 | mark::hit!(no_parens_in_use_item); | ||
462 | return self; | ||
463 | } | ||
464 | if ctx.is_pattern_call { | ||
465 | mark::hit!(dont_duplicate_pattern_parens); | ||
466 | return self; | ||
467 | } | ||
468 | if ctx.is_call { | ||
469 | return self; | ||
470 | } | ||
471 | |||
472 | // Don't add parentheses if the expected type is some function reference. | ||
473 | if let Some(ty) = &ctx.expected_type { | ||
474 | if ty.is_fn() { | ||
475 | mark::hit!(no_call_parens_if_fn_ptr_needed); | ||
476 | return self; | ||
477 | } | ||
478 | } | ||
479 | |||
480 | let cap = match ctx.config.snippet_cap { | ||
481 | Some(it) => it, | ||
482 | None => return self, | ||
483 | }; | ||
484 | // If not an import, add parenthesis automatically. | ||
485 | mark::hit!(inserts_parens_for_function_calls); | ||
486 | |||
487 | let (snippet, label) = if params.is_empty() { | ||
488 | (format!("{}()$0", name), format!("{}()", name)) | ||
489 | } else { | ||
490 | self = self.trigger_call_info(); | ||
491 | let snippet = match (ctx.config.add_call_argument_snippets, params) { | ||
492 | (true, Params::Named(params)) => { | ||
493 | let function_params_snippet = | ||
494 | params.iter().enumerate().format_with(", ", |(index, param_name), f| { | ||
495 | f(&format_args!("${{{}:{}}}", index + 1, param_name)) | ||
496 | }); | ||
497 | format!("{}({})$0", name, function_params_snippet) | ||
498 | } | ||
499 | _ => { | ||
500 | mark::hit!(suppress_arg_snippets); | ||
501 | format!("{}($0)", name) | ||
502 | } | ||
503 | }; | ||
504 | |||
505 | (snippet, format!("{}(…)", name)) | ||
506 | }; | ||
507 | self.lookup_by(name).label(label).insert_snippet(cap, snippet) | ||
508 | } | ||
509 | } | ||
510 | |||
511 | fn is_deprecated(node: impl HasAttrs, db: &RootDatabase) -> bool { | ||
512 | node.attrs(db).by_key("deprecated").exists() | ||
513 | } | ||
514 | |||
515 | fn guess_macro_braces(macro_name: &str, docs: &str) -> (&'static str, &'static str) { | ||
516 | let mut votes = [0, 0, 0]; | ||
517 | for (idx, s) in docs.match_indices(¯o_name) { | ||
518 | let (before, after) = (&docs[..idx], &docs[idx + s.len()..]); | ||
519 | // Ensure to match the full word | ||
520 | if after.starts_with('!') | ||
521 | && !before.ends_with(|c: char| c == '_' || c.is_ascii_alphanumeric()) | ||
522 | { | ||
523 | // It may have spaces before the braces like `foo! {}` | ||
524 | match after[1..].chars().find(|&c| !c.is_whitespace()) { | ||
525 | Some('{') => votes[0] += 1, | ||
526 | Some('[') => votes[1] += 1, | ||
527 | Some('(') => votes[2] += 1, | ||
528 | _ => {} | ||
529 | } | ||
530 | } | ||
531 | } | ||
532 | |||
533 | // Insert a space before `{}`. | ||
534 | // We prefer the last one when some votes equal. | ||
535 | let (_vote, (bra, ket)) = votes | ||
536 | .iter() | ||
537 | .zip(&[(" {", "}"), ("[", "]"), ("(", ")")]) | ||
538 | .max_by_key(|&(&vote, _)| vote) | ||
539 | .unwrap(); | ||
540 | (*bra, *ket) | ||
541 | } | ||
542 | |||
543 | #[cfg(test)] | ||
544 | mod tests { | ||
545 | use std::cmp::Reverse; | ||
546 | |||
547 | use expect_test::{expect, Expect}; | ||
548 | use test_utils::mark; | ||
549 | |||
550 | use crate::{ | ||
551 | test_utils::{check_edit, check_edit_with_config, do_completion, get_all_items}, | ||
552 | CompletionConfig, CompletionKind, CompletionScore, | ||
553 | }; | ||
554 | |||
555 | fn check(ra_fixture: &str, expect: Expect) { | ||
556 | let actual = do_completion(ra_fixture, CompletionKind::Reference); | ||
557 | expect.assert_debug_eq(&actual); | ||
558 | } | ||
559 | |||
560 | fn check_scores(ra_fixture: &str, expect: Expect) { | ||
561 | fn display_score(score: Option<CompletionScore>) -> &'static str { | ||
562 | match score { | ||
563 | Some(CompletionScore::TypeMatch) => "[type]", | ||
564 | Some(CompletionScore::TypeAndNameMatch) => "[type+name]", | ||
565 | None => "[]".into(), | ||
566 | } | ||
567 | } | ||
568 | |||
569 | let mut completions = get_all_items(CompletionConfig::default(), ra_fixture); | ||
570 | completions.sort_by_key(|it| (Reverse(it.score()), it.label().to_string())); | ||
571 | let actual = completions | ||
572 | .into_iter() | ||
573 | .filter(|it| it.completion_kind == CompletionKind::Reference) | ||
574 | .map(|it| { | ||
575 | let tag = it.kind().unwrap().tag(); | ||
576 | let score = display_score(it.score()); | ||
577 | format!("{} {} {}\n", tag, it.label(), score) | ||
578 | }) | ||
579 | .collect::<String>(); | ||
580 | expect.assert_eq(&actual); | ||
581 | } | ||
582 | |||
583 | #[test] | ||
584 | fn enum_detail_includes_record_fields() { | ||
585 | check( | ||
586 | r#" | ||
587 | enum Foo { Foo { x: i32, y: i32 } } | ||
588 | |||
589 | fn main() { Foo::Fo<|> } | ||
590 | "#, | ||
591 | expect![[r#" | ||
592 | [ | ||
593 | CompletionItem { | ||
594 | label: "Foo", | ||
595 | source_range: 54..56, | ||
596 | delete: 54..56, | ||
597 | insert: "Foo", | ||
598 | kind: EnumVariant, | ||
599 | detail: "{ x: i32, y: i32 }", | ||
600 | }, | ||
601 | ] | ||
602 | "#]], | ||
603 | ); | ||
604 | } | ||
605 | |||
606 | #[test] | ||
607 | fn enum_detail_doesnt_include_tuple_fields() { | ||
608 | check( | ||
609 | r#" | ||
610 | enum Foo { Foo (i32, i32) } | ||
611 | |||
612 | fn main() { Foo::Fo<|> } | ||
613 | "#, | ||
614 | expect![[r#" | ||
615 | [ | ||
616 | CompletionItem { | ||
617 | label: "Foo(…)", | ||
618 | source_range: 46..48, | ||
619 | delete: 46..48, | ||
620 | insert: "Foo($0)", | ||
621 | kind: EnumVariant, | ||
622 | lookup: "Foo", | ||
623 | detail: "(i32, i32)", | ||
624 | trigger_call_info: true, | ||
625 | }, | ||
626 | ] | ||
627 | "#]], | ||
628 | ); | ||
629 | } | ||
630 | |||
631 | #[test] | ||
632 | fn enum_detail_just_parentheses_for_unit() { | ||
633 | check( | ||
634 | r#" | ||
635 | enum Foo { Foo } | ||
636 | |||
637 | fn main() { Foo::Fo<|> } | ||
638 | "#, | ||
639 | expect![[r#" | ||
640 | [ | ||
641 | CompletionItem { | ||
642 | label: "Foo", | ||
643 | source_range: 35..37, | ||
644 | delete: 35..37, | ||
645 | insert: "Foo", | ||
646 | kind: EnumVariant, | ||
647 | detail: "()", | ||
648 | }, | ||
649 | ] | ||
650 | "#]], | ||
651 | ); | ||
652 | } | ||
653 | |||
654 | #[test] | ||
655 | fn lookup_enums_by_two_qualifiers() { | ||
656 | check( | ||
657 | r#" | ||
658 | mod m { | ||
659 | pub enum Spam { Foo, Bar(i32) } | ||
660 | } | ||
661 | fn main() { let _: m::Spam = S<|> } | ||
662 | "#, | ||
663 | expect![[r#" | ||
664 | [ | ||
665 | CompletionItem { | ||
666 | label: "Spam::Bar(…)", | ||
667 | source_range: 75..76, | ||
668 | delete: 75..76, | ||
669 | insert: "Spam::Bar($0)", | ||
670 | kind: EnumVariant, | ||
671 | lookup: "Spam::Bar", | ||
672 | detail: "(i32)", | ||
673 | trigger_call_info: true, | ||
674 | }, | ||
675 | CompletionItem { | ||
676 | label: "m", | ||
677 | source_range: 75..76, | ||
678 | delete: 75..76, | ||
679 | insert: "m", | ||
680 | kind: Module, | ||
681 | }, | ||
682 | CompletionItem { | ||
683 | label: "m::Spam::Foo", | ||
684 | source_range: 75..76, | ||
685 | delete: 75..76, | ||
686 | insert: "m::Spam::Foo", | ||
687 | kind: EnumVariant, | ||
688 | lookup: "Spam::Foo", | ||
689 | detail: "()", | ||
690 | }, | ||
691 | CompletionItem { | ||
692 | label: "main()", | ||
693 | source_range: 75..76, | ||
694 | delete: 75..76, | ||
695 | insert: "main()$0", | ||
696 | kind: Function, | ||
697 | lookup: "main", | ||
698 | detail: "fn main()", | ||
699 | }, | ||
700 | ] | ||
701 | "#]], | ||
702 | ) | ||
703 | } | ||
704 | |||
705 | #[test] | ||
706 | fn sets_deprecated_flag_in_items() { | ||
707 | check( | ||
708 | r#" | ||
709 | #[deprecated] | ||
710 | fn something_deprecated() {} | ||
711 | #[deprecated(since = "1.0.0")] | ||
712 | fn something_else_deprecated() {} | ||
713 | |||
714 | fn main() { som<|> } | ||
715 | "#, | ||
716 | expect![[r#" | ||
717 | [ | ||
718 | CompletionItem { | ||
719 | label: "main()", | ||
720 | source_range: 121..124, | ||
721 | delete: 121..124, | ||
722 | insert: "main()$0", | ||
723 | kind: Function, | ||
724 | lookup: "main", | ||
725 | detail: "fn main()", | ||
726 | }, | ||
727 | CompletionItem { | ||
728 | label: "something_deprecated()", | ||
729 | source_range: 121..124, | ||
730 | delete: 121..124, | ||
731 | insert: "something_deprecated()$0", | ||
732 | kind: Function, | ||
733 | lookup: "something_deprecated", | ||
734 | detail: "fn something_deprecated()", | ||
735 | deprecated: true, | ||
736 | }, | ||
737 | CompletionItem { | ||
738 | label: "something_else_deprecated()", | ||
739 | source_range: 121..124, | ||
740 | delete: 121..124, | ||
741 | insert: "something_else_deprecated()$0", | ||
742 | kind: Function, | ||
743 | lookup: "something_else_deprecated", | ||
744 | detail: "fn something_else_deprecated()", | ||
745 | deprecated: true, | ||
746 | }, | ||
747 | ] | ||
748 | "#]], | ||
749 | ); | ||
750 | |||
751 | check( | ||
752 | r#" | ||
753 | struct A { #[deprecated] the_field: u32 } | ||
754 | fn foo() { A { the<|> } } | ||
755 | "#, | ||
756 | expect![[r#" | ||
757 | [ | ||
758 | CompletionItem { | ||
759 | label: "the_field", | ||
760 | source_range: 57..60, | ||
761 | delete: 57..60, | ||
762 | insert: "the_field", | ||
763 | kind: Field, | ||
764 | detail: "u32", | ||
765 | deprecated: true, | ||
766 | }, | ||
767 | ] | ||
768 | "#]], | ||
769 | ); | ||
770 | } | ||
771 | |||
772 | #[test] | ||
773 | fn renders_docs() { | ||
774 | check( | ||
775 | r#" | ||
776 | struct S { | ||
777 | /// Field docs | ||
778 | foo: | ||
779 | } | ||
780 | impl S { | ||
781 | /// Method docs | ||
782 | fn bar(self) { self.<|> } | ||
783 | }"#, | ||
784 | expect![[r#" | ||
785 | [ | ||
786 | CompletionItem { | ||
787 | label: "bar()", | ||
788 | source_range: 94..94, | ||
789 | delete: 94..94, | ||
790 | insert: "bar()$0", | ||
791 | kind: Method, | ||
792 | lookup: "bar", | ||
793 | detail: "fn bar(self)", | ||
794 | documentation: Documentation( | ||
795 | "Method docs", | ||
796 | ), | ||
797 | }, | ||
798 | CompletionItem { | ||
799 | label: "foo", | ||
800 | source_range: 94..94, | ||
801 | delete: 94..94, | ||
802 | insert: "foo", | ||
803 | kind: Field, | ||
804 | detail: "{unknown}", | ||
805 | documentation: Documentation( | ||
806 | "Field docs", | ||
807 | ), | ||
808 | }, | ||
809 | ] | ||
810 | "#]], | ||
811 | ); | ||
812 | |||
813 | check( | ||
814 | r#" | ||
815 | use self::my<|>; | ||
816 | |||
817 | /// mod docs | ||
818 | mod my { } | ||
819 | |||
820 | /// enum docs | ||
821 | enum E { | ||
822 | /// variant docs | ||
823 | V | ||
824 | } | ||
825 | use self::E::*; | ||
826 | "#, | ||
827 | expect![[r#" | ||
828 | [ | ||
829 | CompletionItem { | ||
830 | label: "E", | ||
831 | source_range: 10..12, | ||
832 | delete: 10..12, | ||
833 | insert: "E", | ||
834 | kind: Enum, | ||
835 | documentation: Documentation( | ||
836 | "enum docs", | ||
837 | ), | ||
838 | }, | ||
839 | CompletionItem { | ||
840 | label: "V", | ||
841 | source_range: 10..12, | ||
842 | delete: 10..12, | ||
843 | insert: "V", | ||
844 | kind: EnumVariant, | ||
845 | detail: "()", | ||
846 | documentation: Documentation( | ||
847 | "variant docs", | ||
848 | ), | ||
849 | }, | ||
850 | CompletionItem { | ||
851 | label: "my", | ||
852 | source_range: 10..12, | ||
853 | delete: 10..12, | ||
854 | insert: "my", | ||
855 | kind: Module, | ||
856 | documentation: Documentation( | ||
857 | "mod docs", | ||
858 | ), | ||
859 | }, | ||
860 | ] | ||
861 | "#]], | ||
862 | ) | ||
863 | } | ||
864 | |||
865 | #[test] | ||
866 | fn dont_render_attrs() { | ||
867 | check( | ||
868 | r#" | ||
869 | struct S; | ||
870 | impl S { | ||
871 | #[inline] | ||
872 | fn the_method(&self) { } | ||
873 | } | ||
874 | fn foo(s: S) { s.<|> } | ||
875 | "#, | ||
876 | expect![[r#" | ||
877 | [ | ||
878 | CompletionItem { | ||
879 | label: "the_method()", | ||
880 | source_range: 81..81, | ||
881 | delete: 81..81, | ||
882 | insert: "the_method()$0", | ||
883 | kind: Method, | ||
884 | lookup: "the_method", | ||
885 | detail: "fn the_method(&self)", | ||
886 | }, | ||
887 | ] | ||
888 | "#]], | ||
889 | ) | ||
890 | } | ||
891 | |||
892 | #[test] | ||
893 | fn inserts_parens_for_function_calls() { | ||
894 | mark::check!(inserts_parens_for_function_calls); | ||
895 | check_edit( | ||
896 | "no_args", | ||
897 | r#" | ||
898 | fn no_args() {} | ||
899 | fn main() { no_<|> } | ||
900 | "#, | ||
901 | r#" | ||
902 | fn no_args() {} | ||
903 | fn main() { no_args()$0 } | ||
904 | "#, | ||
905 | ); | ||
906 | |||
907 | check_edit( | ||
908 | "with_args", | ||
909 | r#" | ||
910 | fn with_args(x: i32, y: String) {} | ||
911 | fn main() { with_<|> } | ||
912 | "#, | ||
913 | r#" | ||
914 | fn with_args(x: i32, y: String) {} | ||
915 | fn main() { with_args(${1:x}, ${2:y})$0 } | ||
916 | "#, | ||
917 | ); | ||
918 | |||
919 | check_edit( | ||
920 | "foo", | ||
921 | r#" | ||
922 | struct S; | ||
923 | impl S { | ||
924 | fn foo(&self) {} | ||
925 | } | ||
926 | fn bar(s: &S) { s.f<|> } | ||
927 | "#, | ||
928 | r#" | ||
929 | struct S; | ||
930 | impl S { | ||
931 | fn foo(&self) {} | ||
932 | } | ||
933 | fn bar(s: &S) { s.foo()$0 } | ||
934 | "#, | ||
935 | ); | ||
936 | |||
937 | check_edit( | ||
938 | "foo", | ||
939 | r#" | ||
940 | struct S {} | ||
941 | impl S { | ||
942 | fn foo(&self, x: i32) {} | ||
943 | } | ||
944 | fn bar(s: &S) { | ||
945 | s.f<|> | ||
946 | } | ||
947 | "#, | ||
948 | r#" | ||
949 | struct S {} | ||
950 | impl S { | ||
951 | fn foo(&self, x: i32) {} | ||
952 | } | ||
953 | fn bar(s: &S) { | ||
954 | s.foo(${1:x})$0 | ||
955 | } | ||
956 | "#, | ||
957 | ); | ||
958 | } | ||
959 | |||
960 | #[test] | ||
961 | fn suppress_arg_snippets() { | ||
962 | mark::check!(suppress_arg_snippets); | ||
963 | check_edit_with_config( | ||
964 | CompletionConfig { add_call_argument_snippets: false, ..CompletionConfig::default() }, | ||
965 | "with_args", | ||
966 | r#" | ||
967 | fn with_args(x: i32, y: String) {} | ||
968 | fn main() { with_<|> } | ||
969 | "#, | ||
970 | r#" | ||
971 | fn with_args(x: i32, y: String) {} | ||
972 | fn main() { with_args($0) } | ||
973 | "#, | ||
974 | ); | ||
975 | } | ||
976 | |||
977 | #[test] | ||
978 | fn strips_underscores_from_args() { | ||
979 | check_edit( | ||
980 | "foo", | ||
981 | r#" | ||
982 | fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {} | ||
983 | fn main() { f<|> } | ||
984 | "#, | ||
985 | r#" | ||
986 | fn foo(_foo: i32, ___bar: bool, ho_ge_: String) {} | ||
987 | fn main() { foo(${1:foo}, ${2:bar}, ${3:ho_ge_})$0 } | ||
988 | "#, | ||
989 | ); | ||
990 | } | ||
991 | |||
992 | #[test] | ||
993 | fn insert_ref_when_matching_local_in_scope() { | ||
994 | check_edit( | ||
995 | "ref_arg", | ||
996 | r#" | ||
997 | struct Foo {} | ||
998 | fn ref_arg(x: &Foo) {} | ||
999 | fn main() { | ||
1000 | let x = Foo {}; | ||
1001 | ref_ar<|> | ||
1002 | } | ||
1003 | "#, | ||
1004 | r#" | ||
1005 | struct Foo {} | ||
1006 | fn ref_arg(x: &Foo) {} | ||
1007 | fn main() { | ||
1008 | let x = Foo {}; | ||
1009 | ref_arg(${1:&x})$0 | ||
1010 | } | ||
1011 | "#, | ||
1012 | ); | ||
1013 | } | ||
1014 | |||
1015 | #[test] | ||
1016 | fn insert_mut_ref_when_matching_local_in_scope() { | ||
1017 | check_edit( | ||
1018 | "ref_arg", | ||
1019 | r#" | ||
1020 | struct Foo {} | ||
1021 | fn ref_arg(x: &mut Foo) {} | ||
1022 | fn main() { | ||
1023 | let x = Foo {}; | ||
1024 | ref_ar<|> | ||
1025 | } | ||
1026 | "#, | ||
1027 | r#" | ||
1028 | struct Foo {} | ||
1029 | fn ref_arg(x: &mut Foo) {} | ||
1030 | fn main() { | ||
1031 | let x = Foo {}; | ||
1032 | ref_arg(${1:&mut x})$0 | ||
1033 | } | ||
1034 | "#, | ||
1035 | ); | ||
1036 | } | ||
1037 | |||
1038 | #[test] | ||
1039 | fn insert_ref_when_matching_local_in_scope_for_method() { | ||
1040 | check_edit( | ||
1041 | "apply_foo", | ||
1042 | r#" | ||
1043 | struct Foo {} | ||
1044 | struct Bar {} | ||
1045 | impl Bar { | ||
1046 | fn apply_foo(&self, x: &Foo) {} | ||
1047 | } | ||
1048 | |||
1049 | fn main() { | ||
1050 | let x = Foo {}; | ||
1051 | let y = Bar {}; | ||
1052 | y.<|> | ||
1053 | } | ||
1054 | "#, | ||
1055 | r#" | ||
1056 | struct Foo {} | ||
1057 | struct Bar {} | ||
1058 | impl Bar { | ||
1059 | fn apply_foo(&self, x: &Foo) {} | ||
1060 | } | ||
1061 | |||
1062 | fn main() { | ||
1063 | let x = Foo {}; | ||
1064 | let y = Bar {}; | ||
1065 | y.apply_foo(${1:&x})$0 | ||
1066 | } | ||
1067 | "#, | ||
1068 | ); | ||
1069 | } | ||
1070 | |||
1071 | #[test] | ||
1072 | fn trim_mut_keyword_in_func_completion() { | ||
1073 | check_edit( | ||
1074 | "take_mutably", | ||
1075 | r#" | ||
1076 | fn take_mutably(mut x: &i32) {} | ||
1077 | |||
1078 | fn main() { | ||
1079 | take_m<|> | ||
1080 | } | ||
1081 | "#, | ||
1082 | r#" | ||
1083 | fn take_mutably(mut x: &i32) {} | ||
1084 | |||
1085 | fn main() { | ||
1086 | take_mutably(${1:x})$0 | ||
1087 | } | ||
1088 | "#, | ||
1089 | ); | ||
1090 | } | ||
1091 | |||
1092 | #[test] | ||
1093 | fn inserts_parens_for_tuple_enums() { | ||
1094 | mark::check!(inserts_parens_for_tuple_enums); | ||
1095 | check_edit( | ||
1096 | "Some", | ||
1097 | r#" | ||
1098 | enum Option<T> { Some(T), None } | ||
1099 | use Option::*; | ||
1100 | fn main() -> Option<i32> { | ||
1101 | Som<|> | ||
1102 | } | ||
1103 | "#, | ||
1104 | r#" | ||
1105 | enum Option<T> { Some(T), None } | ||
1106 | use Option::*; | ||
1107 | fn main() -> Option<i32> { | ||
1108 | Some($0) | ||
1109 | } | ||
1110 | "#, | ||
1111 | ); | ||
1112 | check_edit( | ||
1113 | "Some", | ||
1114 | r#" | ||
1115 | enum Option<T> { Some(T), None } | ||
1116 | use Option::*; | ||
1117 | fn main(value: Option<i32>) { | ||
1118 | match value { | ||
1119 | Som<|> | ||
1120 | } | ||
1121 | } | ||
1122 | "#, | ||
1123 | r#" | ||
1124 | enum Option<T> { Some(T), None } | ||
1125 | use Option::*; | ||
1126 | fn main(value: Option<i32>) { | ||
1127 | match value { | ||
1128 | Some($0) | ||
1129 | } | ||
1130 | } | ||
1131 | "#, | ||
1132 | ); | ||
1133 | } | ||
1134 | |||
1135 | #[test] | ||
1136 | fn dont_duplicate_pattern_parens() { | ||
1137 | mark::check!(dont_duplicate_pattern_parens); | ||
1138 | check_edit( | ||
1139 | "Var", | ||
1140 | r#" | ||
1141 | enum E { Var(i32) } | ||
1142 | fn main() { | ||
1143 | match E::Var(92) { | ||
1144 | E::<|>(92) => (), | ||
1145 | } | ||
1146 | } | ||
1147 | "#, | ||
1148 | r#" | ||
1149 | enum E { Var(i32) } | ||
1150 | fn main() { | ||
1151 | match E::Var(92) { | ||
1152 | E::Var(92) => (), | ||
1153 | } | ||
1154 | } | ||
1155 | "#, | ||
1156 | ); | ||
1157 | } | ||
1158 | |||
1159 | #[test] | ||
1160 | fn no_call_parens_if_fn_ptr_needed() { | ||
1161 | mark::check!(no_call_parens_if_fn_ptr_needed); | ||
1162 | check_edit( | ||
1163 | "foo", | ||
1164 | r#" | ||
1165 | fn foo(foo: u8, bar: u8) {} | ||
1166 | struct ManualVtable { f: fn(u8, u8) } | ||
1167 | |||
1168 | fn main() -> ManualVtable { | ||
1169 | ManualVtable { f: f<|> } | ||
1170 | } | ||
1171 | "#, | ||
1172 | r#" | ||
1173 | fn foo(foo: u8, bar: u8) {} | ||
1174 | struct ManualVtable { f: fn(u8, u8) } | ||
1175 | |||
1176 | fn main() -> ManualVtable { | ||
1177 | ManualVtable { f: foo } | ||
1178 | } | ||
1179 | "#, | ||
1180 | ); | ||
1181 | } | ||
1182 | |||
1183 | #[test] | ||
1184 | fn no_parens_in_use_item() { | ||
1185 | mark::check!(no_parens_in_use_item); | ||
1186 | check_edit( | ||
1187 | "foo", | ||
1188 | r#" | ||
1189 | mod m { pub fn foo() {} } | ||
1190 | use crate::m::f<|>; | ||
1191 | "#, | ||
1192 | r#" | ||
1193 | mod m { pub fn foo() {} } | ||
1194 | use crate::m::foo; | ||
1195 | "#, | ||
1196 | ); | ||
1197 | } | ||
1198 | |||
1199 | #[test] | ||
1200 | fn no_parens_in_call() { | ||
1201 | check_edit( | ||
1202 | "foo", | ||
1203 | r#" | ||
1204 | fn foo(x: i32) {} | ||
1205 | fn main() { f<|>(); } | ||
1206 | "#, | ||
1207 | r#" | ||
1208 | fn foo(x: i32) {} | ||
1209 | fn main() { foo(); } | ||
1210 | "#, | ||
1211 | ); | ||
1212 | check_edit( | ||
1213 | "foo", | ||
1214 | r#" | ||
1215 | struct Foo; | ||
1216 | impl Foo { fn foo(&self){} } | ||
1217 | fn f(foo: &Foo) { foo.f<|>(); } | ||
1218 | "#, | ||
1219 | r#" | ||
1220 | struct Foo; | ||
1221 | impl Foo { fn foo(&self){} } | ||
1222 | fn f(foo: &Foo) { foo.foo(); } | ||
1223 | "#, | ||
1224 | ); | ||
1225 | } | ||
1226 | |||
1227 | #[test] | ||
1228 | fn inserts_angle_brackets_for_generics() { | ||
1229 | mark::check!(inserts_angle_brackets_for_generics); | ||
1230 | check_edit( | ||
1231 | "Vec", | ||
1232 | r#" | ||
1233 | struct Vec<T> {} | ||
1234 | fn foo(xs: Ve<|>) | ||
1235 | "#, | ||
1236 | r#" | ||
1237 | struct Vec<T> {} | ||
1238 | fn foo(xs: Vec<$0>) | ||
1239 | "#, | ||
1240 | ); | ||
1241 | check_edit( | ||
1242 | "Vec", | ||
1243 | r#" | ||
1244 | type Vec<T> = (T,); | ||
1245 | fn foo(xs: Ve<|>) | ||
1246 | "#, | ||
1247 | r#" | ||
1248 | type Vec<T> = (T,); | ||
1249 | fn foo(xs: Vec<$0>) | ||
1250 | "#, | ||
1251 | ); | ||
1252 | check_edit( | ||
1253 | "Vec", | ||
1254 | r#" | ||
1255 | struct Vec<T = i128> {} | ||
1256 | fn foo(xs: Ve<|>) | ||
1257 | "#, | ||
1258 | r#" | ||
1259 | struct Vec<T = i128> {} | ||
1260 | fn foo(xs: Vec) | ||
1261 | "#, | ||
1262 | ); | ||
1263 | check_edit( | ||
1264 | "Vec", | ||
1265 | r#" | ||
1266 | struct Vec<T> {} | ||
1267 | fn foo(xs: Ve<|><i128>) | ||
1268 | "#, | ||
1269 | r#" | ||
1270 | struct Vec<T> {} | ||
1271 | fn foo(xs: Vec<i128>) | ||
1272 | "#, | ||
1273 | ); | ||
1274 | } | ||
1275 | |||
1276 | #[test] | ||
1277 | fn dont_insert_macro_call_parens_unncessary() { | ||
1278 | mark::check!(dont_insert_macro_call_parens_unncessary); | ||
1279 | check_edit( | ||
1280 | "frobnicate!", | ||
1281 | r#" | ||
1282 | //- /main.rs crate:main deps:foo | ||
1283 | use foo::<|>; | ||
1284 | //- /foo/lib.rs crate:foo | ||
1285 | #[macro_export] | ||
1286 | macro_rules frobnicate { () => () } | ||
1287 | "#, | ||
1288 | r#" | ||
1289 | use foo::frobnicate; | ||
1290 | "#, | ||
1291 | ); | ||
1292 | |||
1293 | check_edit( | ||
1294 | "frobnicate!", | ||
1295 | r#" | ||
1296 | macro_rules frobnicate { () => () } | ||
1297 | fn main() { frob<|>!(); } | ||
1298 | "#, | ||
1299 | r#" | ||
1300 | macro_rules frobnicate { () => () } | ||
1301 | fn main() { frobnicate!(); } | ||
1302 | "#, | ||
1303 | ); | ||
1304 | } | ||
1305 | |||
1306 | #[test] | ||
1307 | fn active_param_score() { | ||
1308 | mark::check!(active_param_type_match); | ||
1309 | check_scores( | ||
1310 | r#" | ||
1311 | struct S { foo: i64, bar: u32, baz: u32 } | ||
1312 | fn test(bar: u32) { } | ||
1313 | fn foo(s: S) { test(s.<|>) } | ||
1314 | "#, | ||
1315 | expect![[r#" | ||
1316 | fd bar [type+name] | ||
1317 | fd baz [type] | ||
1318 | fd foo [] | ||
1319 | "#]], | ||
1320 | ); | ||
1321 | } | ||
1322 | |||
1323 | #[test] | ||
1324 | fn record_field_scores() { | ||
1325 | mark::check!(record_field_type_match); | ||
1326 | check_scores( | ||
1327 | r#" | ||
1328 | struct A { foo: i64, bar: u32, baz: u32 } | ||
1329 | struct B { x: (), y: f32, bar: u32 } | ||
1330 | fn foo(a: A) { B { bar: a.<|> }; } | ||
1331 | "#, | ||
1332 | expect![[r#" | ||
1333 | fd bar [type+name] | ||
1334 | fd baz [type] | ||
1335 | fd foo [] | ||
1336 | "#]], | ||
1337 | ) | ||
1338 | } | ||
1339 | |||
1340 | #[test] | ||
1341 | fn record_field_and_call_scores() { | ||
1342 | check_scores( | ||
1343 | r#" | ||
1344 | struct A { foo: i64, bar: u32, baz: u32 } | ||
1345 | struct B { x: (), y: f32, bar: u32 } | ||
1346 | fn f(foo: i64) { } | ||
1347 | fn foo(a: A) { B { bar: f(a.<|>) }; } | ||
1348 | "#, | ||
1349 | expect![[r#" | ||
1350 | fd foo [type+name] | ||
1351 | fd bar [] | ||
1352 | fd baz [] | ||
1353 | "#]], | ||
1354 | ); | ||
1355 | check_scores( | ||
1356 | r#" | ||
1357 | struct A { foo: i64, bar: u32, baz: u32 } | ||
1358 | struct B { x: (), y: f32, bar: u32 } | ||
1359 | fn f(foo: i64) { } | ||
1360 | fn foo(a: A) { f(B { bar: a.<|> }); } | ||
1361 | "#, | ||
1362 | expect![[r#" | ||
1363 | fd bar [type+name] | ||
1364 | fd baz [type] | ||
1365 | fd foo [] | ||
1366 | "#]], | ||
1367 | ); | ||
1368 | } | ||
1369 | |||
1370 | #[test] | ||
1371 | fn prioritize_exact_ref_match() { | ||
1372 | check_scores( | ||
1373 | r#" | ||
1374 | struct WorldSnapshot { _f: () }; | ||
1375 | fn go(world: &WorldSnapshot) { go(w<|>) } | ||
1376 | "#, | ||
1377 | expect![[r#" | ||
1378 | bn world [type+name] | ||
1379 | st WorldSnapshot [] | ||
1380 | fn go(…) [] | ||
1381 | "#]], | ||
1382 | ); | ||
1383 | } | ||
1384 | |||
1385 | #[test] | ||
1386 | fn too_many_arguments() { | ||
1387 | check_scores( | ||
1388 | r#" | ||
1389 | struct Foo; | ||
1390 | fn f(foo: &Foo) { f(foo, w<|>) } | ||
1391 | "#, | ||
1392 | expect![[r#" | ||
1393 | st Foo [] | ||
1394 | fn f(…) [] | ||
1395 | bn foo [] | ||
1396 | "#]], | ||
1397 | ); | ||
1398 | } | ||
1399 | |||
1400 | #[test] | ||
1401 | fn guesses_macro_braces() { | ||
1402 | check_edit( | ||
1403 | "vec!", | ||
1404 | r#" | ||
1405 | /// Creates a [`Vec`] containing the arguments. | ||
1406 | /// | ||
1407 | /// ``` | ||
1408 | /// let v = vec![1, 2, 3]; | ||
1409 | /// assert_eq!(v[0], 1); | ||
1410 | /// assert_eq!(v[1], 2); | ||
1411 | /// assert_eq!(v[2], 3); | ||
1412 | /// ``` | ||
1413 | macro_rules! vec { () => {} } | ||
1414 | |||
1415 | fn fn main() { v<|> } | ||
1416 | "#, | ||
1417 | r#" | ||
1418 | /// Creates a [`Vec`] containing the arguments. | ||
1419 | /// | ||
1420 | /// ``` | ||
1421 | /// let v = vec![1, 2, 3]; | ||
1422 | /// assert_eq!(v[0], 1); | ||
1423 | /// assert_eq!(v[1], 2); | ||
1424 | /// assert_eq!(v[2], 3); | ||
1425 | /// ``` | ||
1426 | macro_rules! vec { () => {} } | ||
1427 | |||
1428 | fn fn main() { vec![$0] } | ||
1429 | "#, | ||
1430 | ); | ||
1431 | |||
1432 | check_edit( | ||
1433 | "foo!", | ||
1434 | r#" | ||
1435 | /// Foo | ||
1436 | /// | ||
1437 | /// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`, | ||
1438 | /// call as `let _=foo! { hello world };` | ||
1439 | macro_rules! foo { () => {} } | ||
1440 | fn main() { <|> } | ||
1441 | "#, | ||
1442 | r#" | ||
1443 | /// Foo | ||
1444 | /// | ||
1445 | /// Don't call `fooo!()` `fooo!()`, or `_foo![]` `_foo![]`, | ||
1446 | /// call as `let _=foo! { hello world };` | ||
1447 | macro_rules! foo { () => {} } | ||
1448 | fn main() { foo! {$0} } | ||
1449 | "#, | ||
1450 | ) | ||
1451 | } | ||
1452 | } | ||
diff --git a/crates/completion/src/completions/attribute.rs b/crates/completion/src/completions/attribute.rs new file mode 100644 index 000000000..f3d669458 --- /dev/null +++ b/crates/completion/src/completions/attribute.rs | |||
@@ -0,0 +1,528 @@ | |||
1 | //! Completion for attributes | ||
2 | //! | ||
3 | //! This module uses a bit of static metadata to provide completions | ||
4 | //! for built-in attributes. | ||
5 | |||
6 | use rustc_hash::FxHashSet; | ||
7 | use syntax::{ast, AstNode, SyntaxKind}; | ||
8 | |||
9 | use crate::{ | ||
10 | context::CompletionContext, | ||
11 | generated_lint_completions::{CLIPPY_LINTS, FEATURES}, | ||
12 | item::{CompletionItem, CompletionItemKind, CompletionKind}, | ||
13 | Completions, | ||
14 | }; | ||
15 | |||
16 | pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | ||
17 | if ctx.mod_declaration_under_caret.is_some() { | ||
18 | return None; | ||
19 | } | ||
20 | |||
21 | let attribute = ctx.attribute_under_caret.as_ref()?; | ||
22 | match (attribute.path(), attribute.token_tree()) { | ||
23 | (Some(path), Some(token_tree)) if path.to_string() == "derive" => { | ||
24 | complete_derive(acc, ctx, token_tree) | ||
25 | } | ||
26 | (Some(path), Some(token_tree)) if path.to_string() == "feature" => { | ||
27 | complete_lint(acc, ctx, token_tree, FEATURES); | ||
28 | } | ||
29 | (Some(path), Some(token_tree)) | ||
30 | if ["allow", "warn", "deny", "forbid"] | ||
31 | .iter() | ||
32 | .any(|lint_level| lint_level == &path.to_string()) => | ||
33 | { | ||
34 | complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINT_COMPLETIONS); | ||
35 | complete_lint(acc, ctx, token_tree, CLIPPY_LINTS); | ||
36 | } | ||
37 | (_, Some(_token_tree)) => {} | ||
38 | _ => complete_attribute_start(acc, ctx, attribute), | ||
39 | } | ||
40 | Some(()) | ||
41 | } | ||
42 | |||
43 | fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) { | ||
44 | for attr_completion in ATTRIBUTES { | ||
45 | let mut item = CompletionItem::new( | ||
46 | CompletionKind::Attribute, | ||
47 | ctx.source_range(), | ||
48 | attr_completion.label, | ||
49 | ) | ||
50 | .kind(CompletionItemKind::Attribute); | ||
51 | |||
52 | if let Some(lookup) = attr_completion.lookup { | ||
53 | item = item.lookup_by(lookup); | ||
54 | } | ||
55 | |||
56 | match (attr_completion.snippet, ctx.config.snippet_cap) { | ||
57 | (Some(snippet), Some(cap)) => { | ||
58 | item = item.insert_snippet(cap, snippet); | ||
59 | } | ||
60 | _ => {} | ||
61 | } | ||
62 | |||
63 | if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner { | ||
64 | acc.add(item.build()); | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | struct AttrCompletion { | ||
70 | label: &'static str, | ||
71 | lookup: Option<&'static str>, | ||
72 | snippet: Option<&'static str>, | ||
73 | prefer_inner: bool, | ||
74 | } | ||
75 | |||
76 | impl AttrCompletion { | ||
77 | const fn prefer_inner(self) -> AttrCompletion { | ||
78 | AttrCompletion { prefer_inner: true, ..self } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | const fn attr( | ||
83 | label: &'static str, | ||
84 | lookup: Option<&'static str>, | ||
85 | snippet: Option<&'static str>, | ||
86 | ) -> AttrCompletion { | ||
87 | AttrCompletion { label, lookup, snippet, prefer_inner: false } | ||
88 | } | ||
89 | |||
90 | const ATTRIBUTES: &[AttrCompletion] = &[ | ||
91 | attr("allow(…)", Some("allow"), Some("allow(${0:lint})")), | ||
92 | attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")), | ||
93 | attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")), | ||
94 | attr("deny(…)", Some("deny"), Some("deny(${0:lint})")), | ||
95 | attr(r#"deprecated = "…""#, Some("deprecated"), Some(r#"deprecated = "${0:reason}""#)), | ||
96 | attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)), | ||
97 | attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)), | ||
98 | attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(), | ||
99 | attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")), | ||
100 | // FIXME: resolve through macro resolution? | ||
101 | attr("global_allocator", None, None).prefer_inner(), | ||
102 | attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)), | ||
103 | attr("inline(…)", Some("inline"), Some("inline(${0:lint})")), | ||
104 | attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)), | ||
105 | attr("link", None, None), | ||
106 | attr("macro_export", None, None), | ||
107 | attr("macro_use", None, None), | ||
108 | attr(r#"must_use = "…""#, Some("must_use"), Some(r#"must_use = "${0:reason}""#)), | ||
109 | attr("no_mangle", None, None), | ||
110 | attr("no_std", None, None).prefer_inner(), | ||
111 | attr("non_exhaustive", None, None), | ||
112 | attr("panic_handler", None, None).prefer_inner(), | ||
113 | attr("path = \"…\"", Some("path"), Some("path =\"${0:path}\"")), | ||
114 | attr("proc_macro", None, None), | ||
115 | attr("proc_macro_attribute", None, None), | ||
116 | attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")), | ||
117 | attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}")) | ||
118 | .prefer_inner(), | ||
119 | attr("repr(…)", Some("repr"), Some("repr(${0:C})")), | ||
120 | attr( | ||
121 | "should_panic(…)", | ||
122 | Some("should_panic"), | ||
123 | Some(r#"should_panic(expected = "${0:reason}")"#), | ||
124 | ), | ||
125 | attr( | ||
126 | r#"target_feature = "…""#, | ||
127 | Some("target_feature"), | ||
128 | Some("target_feature = \"${0:feature}\""), | ||
129 | ), | ||
130 | attr("test", None, None), | ||
131 | attr("used", None, None), | ||
132 | attr("warn(…)", Some("warn"), Some("warn(${0:lint})")), | ||
133 | attr( | ||
134 | r#"windows_subsystem = "…""#, | ||
135 | Some("windows_subsystem"), | ||
136 | Some(r#"windows_subsystem = "${0:subsystem}""#), | ||
137 | ) | ||
138 | .prefer_inner(), | ||
139 | ]; | ||
140 | |||
141 | fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input: ast::TokenTree) { | ||
142 | if let Ok(existing_derives) = parse_comma_sep_input(derive_input) { | ||
143 | for derive_completion in DEFAULT_DERIVE_COMPLETIONS | ||
144 | .into_iter() | ||
145 | .filter(|completion| !existing_derives.contains(completion.label)) | ||
146 | { | ||
147 | let mut label = derive_completion.label.to_owned(); | ||
148 | for dependency in derive_completion | ||
149 | .dependencies | ||
150 | .into_iter() | ||
151 | .filter(|&&dependency| !existing_derives.contains(dependency)) | ||
152 | { | ||
153 | label.push_str(", "); | ||
154 | label.push_str(dependency); | ||
155 | } | ||
156 | CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label) | ||
157 | .kind(CompletionItemKind::Attribute) | ||
158 | .add_to(acc) | ||
159 | } | ||
160 | |||
161 | for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) { | ||
162 | CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), custom_derive_name) | ||
163 | .kind(CompletionItemKind::Attribute) | ||
164 | .add_to(acc) | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | |||
169 | fn complete_lint( | ||
170 | acc: &mut Completions, | ||
171 | ctx: &CompletionContext, | ||
172 | derive_input: ast::TokenTree, | ||
173 | lints_completions: &[LintCompletion], | ||
174 | ) { | ||
175 | if let Ok(existing_lints) = parse_comma_sep_input(derive_input) { | ||
176 | for lint_completion in lints_completions | ||
177 | .into_iter() | ||
178 | .filter(|completion| !existing_lints.contains(completion.label)) | ||
179 | { | ||
180 | CompletionItem::new( | ||
181 | CompletionKind::Attribute, | ||
182 | ctx.source_range(), | ||
183 | lint_completion.label, | ||
184 | ) | ||
185 | .kind(CompletionItemKind::Attribute) | ||
186 | .detail(lint_completion.description) | ||
187 | .add_to(acc) | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | |||
192 | fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>, ()> { | ||
193 | match (derive_input.left_delimiter_token(), derive_input.right_delimiter_token()) { | ||
194 | (Some(left_paren), Some(right_paren)) | ||
195 | if left_paren.kind() == SyntaxKind::L_PAREN | ||
196 | && right_paren.kind() == SyntaxKind::R_PAREN => | ||
197 | { | ||
198 | let mut input_derives = FxHashSet::default(); | ||
199 | let mut current_derive = String::new(); | ||
200 | for token in derive_input | ||
201 | .syntax() | ||
202 | .children_with_tokens() | ||
203 | .filter_map(|token| token.into_token()) | ||
204 | .skip_while(|token| token != &left_paren) | ||
205 | .skip(1) | ||
206 | .take_while(|token| token != &right_paren) | ||
207 | { | ||
208 | if SyntaxKind::COMMA == token.kind() { | ||
209 | if !current_derive.is_empty() { | ||
210 | input_derives.insert(current_derive); | ||
211 | current_derive = String::new(); | ||
212 | } | ||
213 | } else { | ||
214 | current_derive.push_str(token.to_string().trim()); | ||
215 | } | ||
216 | } | ||
217 | |||
218 | if !current_derive.is_empty() { | ||
219 | input_derives.insert(current_derive); | ||
220 | } | ||
221 | Ok(input_derives) | ||
222 | } | ||
223 | _ => Err(()), | ||
224 | } | ||
225 | } | ||
226 | |||
227 | fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> { | ||
228 | let mut result = FxHashSet::default(); | ||
229 | ctx.scope.process_all_names(&mut |name, scope_def| { | ||
230 | if let hir::ScopeDef::MacroDef(mac) = scope_def { | ||
231 | if mac.is_derive_macro() { | ||
232 | result.insert(name.to_string()); | ||
233 | } | ||
234 | } | ||
235 | }); | ||
236 | result | ||
237 | } | ||
238 | |||
239 | struct DeriveCompletion { | ||
240 | label: &'static str, | ||
241 | dependencies: &'static [&'static str], | ||
242 | } | ||
243 | |||
244 | /// Standard Rust derives and the information about their dependencies | ||
245 | /// (the dependencies are needed so that the main derive don't break the compilation when added) | ||
246 | #[rustfmt::skip] | ||
247 | const DEFAULT_DERIVE_COMPLETIONS: &[DeriveCompletion] = &[ | ||
248 | DeriveCompletion { label: "Clone", dependencies: &[] }, | ||
249 | DeriveCompletion { label: "Copy", dependencies: &["Clone"] }, | ||
250 | DeriveCompletion { label: "Debug", dependencies: &[] }, | ||
251 | DeriveCompletion { label: "Default", dependencies: &[] }, | ||
252 | DeriveCompletion { label: "Hash", dependencies: &[] }, | ||
253 | DeriveCompletion { label: "PartialEq", dependencies: &[] }, | ||
254 | DeriveCompletion { label: "Eq", dependencies: &["PartialEq"] }, | ||
255 | DeriveCompletion { label: "PartialOrd", dependencies: &["PartialEq"] }, | ||
256 | DeriveCompletion { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] }, | ||
257 | ]; | ||
258 | |||
259 | pub(crate) struct LintCompletion { | ||
260 | pub(crate) label: &'static str, | ||
261 | pub(crate) description: &'static str, | ||
262 | } | ||
263 | |||
264 | #[rustfmt::skip] | ||
265 | const DEFAULT_LINT_COMPLETIONS: &[LintCompletion] = &[ | ||
266 | LintCompletion { label: "absolute_paths_not_starting_with_crate", description: r#"fully qualified paths that start with a module name instead of `crate`, `self`, or an extern crate name"# }, | ||
267 | LintCompletion { label: "anonymous_parameters", description: r#"detects anonymous parameters"# }, | ||
268 | LintCompletion { label: "box_pointers", description: r#"use of owned (Box type) heap memory"# }, | ||
269 | LintCompletion { label: "deprecated_in_future", description: r#"detects use of items that will be deprecated in a future version"# }, | ||
270 | LintCompletion { label: "elided_lifetimes_in_paths", description: r#"hidden lifetime parameters in types are deprecated"# }, | ||
271 | LintCompletion { label: "explicit_outlives_requirements", description: r#"outlives requirements can be inferred"# }, | ||
272 | LintCompletion { label: "indirect_structural_match", description: r#"pattern with const indirectly referencing non-structural-match type"# }, | ||
273 | LintCompletion { label: "keyword_idents", description: r#"detects edition keywords being used as an identifier"# }, | ||
274 | LintCompletion { label: "macro_use_extern_crate", description: r#"the `#[macro_use]` attribute is now deprecated in favor of using macros via the module system"# }, | ||
275 | LintCompletion { label: "meta_variable_misuse", description: r#"possible meta-variable misuse at macro definition"# }, | ||
276 | LintCompletion { label: "missing_copy_implementations", description: r#"detects potentially-forgotten implementations of `Copy`"# }, | ||
277 | LintCompletion { label: "missing_crate_level_docs", description: r#"detects crates with no crate-level documentation"# }, | ||
278 | LintCompletion { label: "missing_debug_implementations", description: r#"detects missing implementations of Debug"# }, | ||
279 | LintCompletion { label: "missing_docs", description: r#"detects missing documentation for public members"# }, | ||
280 | LintCompletion { label: "missing_doc_code_examples", description: r#"detects publicly-exported items without code samples in their documentation"# }, | ||
281 | LintCompletion { label: "non_ascii_idents", description: r#"detects non-ASCII identifiers"# }, | ||
282 | LintCompletion { label: "private_doc_tests", description: r#"detects code samples in docs of private items not documented by rustdoc"# }, | ||
283 | LintCompletion { label: "single_use_lifetimes", description: r#"detects lifetime parameters that are only used once"# }, | ||
284 | LintCompletion { label: "trivial_casts", description: r#"detects trivial casts which could be removed"# }, | ||
285 | LintCompletion { label: "trivial_numeric_casts", description: r#"detects trivial casts of numeric types which could be removed"# }, | ||
286 | LintCompletion { label: "unaligned_references", description: r#"detects unaligned references to fields of packed structs"# }, | ||
287 | LintCompletion { label: "unreachable_pub", description: r#"`pub` items not reachable from crate root"# }, | ||
288 | LintCompletion { label: "unsafe_code", description: r#"usage of `unsafe` code"# }, | ||
289 | LintCompletion { label: "unsafe_op_in_unsafe_fn", description: r#"unsafe operations in unsafe functions without an explicit unsafe block are deprecated"# }, | ||
290 | LintCompletion { label: "unstable_features", description: r#"enabling unstable features (deprecated. do not use)"# }, | ||
291 | LintCompletion { label: "unused_crate_dependencies", description: r#"crate dependencies that are never used"# }, | ||
292 | LintCompletion { label: "unused_extern_crates", description: r#"extern crates that are never used"# }, | ||
293 | LintCompletion { label: "unused_import_braces", description: r#"unnecessary braces around an imported item"# }, | ||
294 | LintCompletion { label: "unused_lifetimes", description: r#"detects lifetime parameters that are never used"# }, | ||
295 | LintCompletion { label: "unused_qualifications", description: r#"detects unnecessarily qualified names"# }, | ||
296 | LintCompletion { label: "unused_results", description: r#"unused result of an expression in a statement"# }, | ||
297 | LintCompletion { label: "variant_size_differences", description: r#"detects enums with widely varying variant sizes"# }, | ||
298 | LintCompletion { label: "array_into_iter", description: r#"detects calling `into_iter` on arrays"# }, | ||
299 | LintCompletion { label: "asm_sub_register", description: r#"using only a subset of a register for inline asm inputs"# }, | ||
300 | LintCompletion { label: "bare_trait_objects", description: r#"suggest using `dyn Trait` for trait objects"# }, | ||
301 | LintCompletion { label: "bindings_with_variant_name", description: r#"detects pattern bindings with the same name as one of the matched variants"# }, | ||
302 | LintCompletion { label: "cenum_impl_drop_cast", description: r#"a C-like enum implementing Drop is cast"# }, | ||
303 | LintCompletion { label: "clashing_extern_declarations", description: r#"detects when an extern fn has been declared with the same name but different types"# }, | ||
304 | LintCompletion { label: "coherence_leak_check", description: r#"distinct impls distinguished only by the leak-check code"# }, | ||
305 | LintCompletion { label: "confusable_idents", description: r#"detects visually confusable pairs between identifiers"# }, | ||
306 | LintCompletion { label: "dead_code", description: r#"detect unused, unexported items"# }, | ||
307 | LintCompletion { label: "deprecated", description: r#"detects use of deprecated items"# }, | ||
308 | LintCompletion { label: "ellipsis_inclusive_range_patterns", description: r#"`...` range patterns are deprecated"# }, | ||
309 | LintCompletion { label: "exported_private_dependencies", description: r#"public interface leaks type from a private dependency"# }, | ||
310 | LintCompletion { label: "illegal_floating_point_literal_pattern", description: r#"floating-point literals cannot be used in patterns"# }, | ||
311 | LintCompletion { label: "improper_ctypes", description: r#"proper use of libc types in foreign modules"# }, | ||
312 | LintCompletion { label: "improper_ctypes_definitions", description: r#"proper use of libc types in foreign item definitions"# }, | ||
313 | LintCompletion { label: "incomplete_features", description: r#"incomplete features that may function improperly in some or all cases"# }, | ||
314 | LintCompletion { label: "inline_no_sanitize", description: r#"detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`"# }, | ||
315 | LintCompletion { label: "intra_doc_link_resolution_failure", description: r#"failures in resolving intra-doc link targets"# }, | ||
316 | LintCompletion { label: "invalid_codeblock_attributes", description: r#"codeblock attribute looks a lot like a known one"# }, | ||
317 | LintCompletion { label: "invalid_value", description: r#"an invalid value is being created (such as a NULL reference)"# }, | ||
318 | LintCompletion { label: "irrefutable_let_patterns", description: r#"detects irrefutable patterns in if-let and while-let statements"# }, | ||
319 | LintCompletion { label: "late_bound_lifetime_arguments", description: r#"detects generic lifetime arguments in path segments with late bound lifetime parameters"# }, | ||
320 | LintCompletion { label: "mixed_script_confusables", description: r#"detects Unicode scripts whose mixed script confusables codepoints are solely used"# }, | ||
321 | LintCompletion { label: "mutable_borrow_reservation_conflict", description: r#"reservation of a two-phased borrow conflicts with other shared borrows"# }, | ||
322 | LintCompletion { label: "non_camel_case_types", description: r#"types, variants, traits and type parameters should have camel case names"# }, | ||
323 | LintCompletion { label: "non_shorthand_field_patterns", description: r#"using `Struct { x: x }` instead of `Struct { x }` in a pattern"# }, | ||
324 | LintCompletion { label: "non_snake_case", description: r#"variables, methods, functions, lifetime parameters and modules should have snake case names"# }, | ||
325 | LintCompletion { label: "non_upper_case_globals", description: r#"static constants should have uppercase identifiers"# }, | ||
326 | LintCompletion { label: "no_mangle_generic_items", description: r#"generic items must be mangled"# }, | ||
327 | LintCompletion { label: "overlapping_patterns", description: r#"detects overlapping patterns"# }, | ||
328 | LintCompletion { label: "path_statements", description: r#"path statements with no effect"# }, | ||
329 | LintCompletion { label: "private_in_public", description: r#"detect private items in public interfaces not caught by the old implementation"# }, | ||
330 | LintCompletion { label: "proc_macro_derive_resolution_fallback", description: r#"detects proc macro derives using inaccessible names from parent modules"# }, | ||
331 | LintCompletion { label: "redundant_semicolons", description: r#"detects unnecessary trailing semicolons"# }, | ||
332 | LintCompletion { label: "renamed_and_removed_lints", description: r#"lints that have been renamed or removed"# }, | ||
333 | LintCompletion { label: "safe_packed_borrows", description: r#"safe borrows of fields of packed structs were erroneously allowed"# }, | ||
334 | LintCompletion { label: "stable_features", description: r#"stable features found in `#[feature]` directive"# }, | ||
335 | LintCompletion { label: "trivial_bounds", description: r#"these bounds don't depend on an type parameters"# }, | ||
336 | LintCompletion { label: "type_alias_bounds", description: r#"bounds in type aliases are not enforced"# }, | ||
337 | LintCompletion { label: "tyvar_behind_raw_pointer", description: r#"raw pointer to an inference variable"# }, | ||
338 | LintCompletion { label: "uncommon_codepoints", description: r#"detects uncommon Unicode codepoints in identifiers"# }, | ||
339 | LintCompletion { label: "unconditional_recursion", description: r#"functions that cannot return without calling themselves"# }, | ||
340 | LintCompletion { label: "unknown_lints", description: r#"unrecognized lint attribute"# }, | ||
341 | LintCompletion { label: "unnameable_test_items", description: r#"detects an item that cannot be named being marked as `#[test_case]`"# }, | ||
342 | LintCompletion { label: "unreachable_code", description: r#"detects unreachable code paths"# }, | ||
343 | LintCompletion { label: "unreachable_patterns", description: r#"detects unreachable patterns"# }, | ||
344 | LintCompletion { label: "unstable_name_collisions", description: r#"detects name collision with an existing but unstable method"# }, | ||
345 | LintCompletion { label: "unused_allocation", description: r#"detects unnecessary allocations that can be eliminated"# }, | ||
346 | LintCompletion { label: "unused_assignments", description: r#"detect assignments that will never be read"# }, | ||
347 | LintCompletion { label: "unused_attributes", description: r#"detects attributes that were not used by the compiler"# }, | ||
348 | LintCompletion { label: "unused_braces", description: r#"unnecessary braces around an expression"# }, | ||
349 | LintCompletion { label: "unused_comparisons", description: r#"comparisons made useless by limits of the types involved"# }, | ||
350 | LintCompletion { label: "unused_doc_comments", description: r#"detects doc comments that aren't used by rustdoc"# }, | ||
351 | LintCompletion { label: "unused_features", description: r#"unused features found in crate-level `#[feature]` directives"# }, | ||
352 | LintCompletion { label: "unused_imports", description: r#"imports that are never used"# }, | ||
353 | LintCompletion { label: "unused_labels", description: r#"detects labels that are never used"# }, | ||
354 | LintCompletion { label: "unused_macros", description: r#"detects macros that were not used"# }, | ||
355 | LintCompletion { label: "unused_must_use", description: r#"unused result of a type flagged as `#[must_use]`"# }, | ||
356 | LintCompletion { label: "unused_mut", description: r#"detect mut variables which don't need to be mutable"# }, | ||
357 | LintCompletion { label: "unused_parens", description: r#"`if`, `match`, `while` and `return` do not need parentheses"# }, | ||
358 | LintCompletion { label: "unused_unsafe", description: r#"unnecessary use of an `unsafe` block"# }, | ||
359 | LintCompletion { label: "unused_variables", description: r#"detect variables which are not used in any way"# }, | ||
360 | LintCompletion { label: "warnings", description: r#"mass-change the level for lints which produce warnings"# }, | ||
361 | LintCompletion { label: "where_clauses_object_safety", description: r#"checks the object safety of where clauses"# }, | ||
362 | LintCompletion { label: "while_true", description: r#"suggest using `loop { }` instead of `while true { }`"# }, | ||
363 | LintCompletion { label: "ambiguous_associated_items", description: r#"ambiguous associated items"# }, | ||
364 | LintCompletion { label: "arithmetic_overflow", description: r#"arithmetic operation overflows"# }, | ||
365 | LintCompletion { label: "conflicting_repr_hints", description: r#"conflicts between `#[repr(..)]` hints that were previously accepted and used in practice"# }, | ||
366 | LintCompletion { label: "const_err", description: r#"constant evaluation detected erroneous expression"# }, | ||
367 | LintCompletion { label: "ill_formed_attribute_input", description: r#"ill-formed attribute inputs that were previously accepted and used in practice"# }, | ||
368 | LintCompletion { label: "incomplete_include", description: r#"trailing content in included file"# }, | ||
369 | LintCompletion { label: "invalid_type_param_default", description: r#"type parameter default erroneously allowed in invalid location"# }, | ||
370 | LintCompletion { label: "macro_expanded_macro_exports_accessed_by_absolute_paths", description: r#"macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths"# }, | ||
371 | LintCompletion { label: "missing_fragment_specifier", description: r#"detects missing fragment specifiers in unused `macro_rules!` patterns"# }, | ||
372 | LintCompletion { label: "mutable_transmutes", description: r#"mutating transmuted &mut T from &T may cause undefined behavior"# }, | ||
373 | LintCompletion { label: "no_mangle_const_items", description: r#"const items will not have their symbols exported"# }, | ||
374 | LintCompletion { label: "order_dependent_trait_objects", description: r#"trait-object types were treated as different depending on marker-trait order"# }, | ||
375 | LintCompletion { label: "overflowing_literals", description: r#"literal out of range for its type"# }, | ||
376 | LintCompletion { label: "patterns_in_fns_without_body", description: r#"patterns in functions without body were erroneously allowed"# }, | ||
377 | LintCompletion { label: "pub_use_of_private_extern_crate", description: r#"detect public re-exports of private extern crates"# }, | ||
378 | LintCompletion { label: "soft_unstable", description: r#"a feature gate that doesn't break dependent crates"# }, | ||
379 | LintCompletion { label: "unconditional_panic", description: r#"operation will cause a panic at runtime"# }, | ||
380 | LintCompletion { label: "unknown_crate_types", description: r#"unknown crate type found in `#[crate_type]` directive"# }, | ||
381 | ]; | ||
382 | |||
383 | #[cfg(test)] | ||
384 | mod tests { | ||
385 | use expect_test::{expect, Expect}; | ||
386 | |||
387 | use crate::{test_utils::completion_list, CompletionKind}; | ||
388 | |||
389 | fn check(ra_fixture: &str, expect: Expect) { | ||
390 | let actual = completion_list(ra_fixture, CompletionKind::Attribute); | ||
391 | expect.assert_eq(&actual); | ||
392 | } | ||
393 | |||
394 | #[test] | ||
395 | fn empty_derive_completion() { | ||
396 | check( | ||
397 | r#" | ||
398 | #[derive(<|>)] | ||
399 | struct Test {} | ||
400 | "#, | ||
401 | expect![[r#" | ||
402 | at Clone | ||
403 | at Copy, Clone | ||
404 | at Debug | ||
405 | at Default | ||
406 | at Eq, PartialEq | ||
407 | at Hash | ||
408 | at Ord, PartialOrd, Eq, PartialEq | ||
409 | at PartialEq | ||
410 | at PartialOrd, PartialEq | ||
411 | "#]], | ||
412 | ); | ||
413 | } | ||
414 | |||
415 | #[test] | ||
416 | fn no_completion_for_incorrect_derive() { | ||
417 | check( | ||
418 | r#" | ||
419 | #[derive{<|>)] | ||
420 | struct Test {} | ||
421 | "#, | ||
422 | expect![[r#""#]], | ||
423 | ) | ||
424 | } | ||
425 | |||
426 | #[test] | ||
427 | fn derive_with_input_completion() { | ||
428 | check( | ||
429 | r#" | ||
430 | #[derive(serde::Serialize, PartialEq, <|>)] | ||
431 | struct Test {} | ||
432 | "#, | ||
433 | expect![[r#" | ||
434 | at Clone | ||
435 | at Copy, Clone | ||
436 | at Debug | ||
437 | at Default | ||
438 | at Eq | ||
439 | at Hash | ||
440 | at Ord, PartialOrd, Eq | ||
441 | at PartialOrd | ||
442 | "#]], | ||
443 | ) | ||
444 | } | ||
445 | |||
446 | #[test] | ||
447 | fn test_attribute_completion() { | ||
448 | check( | ||
449 | r#"#[<|>]"#, | ||
450 | expect![[r#" | ||
451 | at allow(…) | ||
452 | at cfg(…) | ||
453 | at cfg_attr(…) | ||
454 | at deny(…) | ||
455 | at deprecated = "…" | ||
456 | at derive(…) | ||
457 | at doc = "…" | ||
458 | at forbid(…) | ||
459 | at ignore = "…" | ||
460 | at inline(…) | ||
461 | at link | ||
462 | at link_name = "…" | ||
463 | at macro_export | ||
464 | at macro_use | ||
465 | at must_use = "…" | ||
466 | at no_mangle | ||
467 | at non_exhaustive | ||
468 | at path = "…" | ||
469 | at proc_macro | ||
470 | at proc_macro_attribute | ||
471 | at proc_macro_derive(…) | ||
472 | at repr(…) | ||
473 | at should_panic(…) | ||
474 | at target_feature = "…" | ||
475 | at test | ||
476 | at used | ||
477 | at warn(…) | ||
478 | "#]], | ||
479 | ) | ||
480 | } | ||
481 | |||
482 | #[test] | ||
483 | fn test_attribute_completion_inside_nested_attr() { | ||
484 | check(r#"#[cfg(<|>)]"#, expect![[]]) | ||
485 | } | ||
486 | |||
487 | #[test] | ||
488 | fn test_inner_attribute_completion() { | ||
489 | check( | ||
490 | r"#![<|>]", | ||
491 | expect![[r#" | ||
492 | at allow(…) | ||
493 | at cfg(…) | ||
494 | at cfg_attr(…) | ||
495 | at deny(…) | ||
496 | at deprecated = "…" | ||
497 | at derive(…) | ||
498 | at doc = "…" | ||
499 | at feature(…) | ||
500 | at forbid(…) | ||
501 | at global_allocator | ||
502 | at ignore = "…" | ||
503 | at inline(…) | ||
504 | at link | ||
505 | at link_name = "…" | ||
506 | at macro_export | ||
507 | at macro_use | ||
508 | at must_use = "…" | ||
509 | at no_mangle | ||
510 | at no_std | ||
511 | at non_exhaustive | ||
512 | at panic_handler | ||
513 | at path = "…" | ||
514 | at proc_macro | ||
515 | at proc_macro_attribute | ||
516 | at proc_macro_derive(…) | ||
517 | at recursion_limit = … | ||
518 | at repr(…) | ||
519 | at should_panic(…) | ||
520 | at target_feature = "…" | ||
521 | at test | ||
522 | at used | ||
523 | at warn(…) | ||
524 | at windows_subsystem = "…" | ||
525 | "#]], | ||
526 | ); | ||
527 | } | ||
528 | } | ||
diff --git a/crates/completion/src/completions/dot.rs b/crates/completion/src/completions/dot.rs new file mode 100644 index 000000000..c9875045a --- /dev/null +++ b/crates/completion/src/completions/dot.rs | |||
@@ -0,0 +1,431 @@ | |||
1 | //! Completes references after dot (fields and method calls). | ||
2 | |||
3 | use hir::{HasVisibility, Type}; | ||
4 | use rustc_hash::FxHashSet; | ||
5 | use test_utils::mark; | ||
6 | |||
7 | use crate::{context::CompletionContext, Completions}; | ||
8 | |||
9 | /// Complete dot accesses, i.e. fields or methods. | ||
10 | pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | ||
11 | let dot_receiver = match &ctx.dot_receiver { | ||
12 | Some(expr) => expr, | ||
13 | _ => return, | ||
14 | }; | ||
15 | |||
16 | let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { | ||
17 | Some(ty) => ty, | ||
18 | _ => return, | ||
19 | }; | ||
20 | |||
21 | if ctx.is_call { | ||
22 | mark::hit!(test_no_struct_field_completion_for_method_call); | ||
23 | } else { | ||
24 | complete_fields(acc, ctx, &receiver_ty); | ||
25 | } | ||
26 | complete_methods(acc, ctx, &receiver_ty); | ||
27 | } | ||
28 | |||
29 | fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { | ||
30 | for receiver in receiver.autoderef(ctx.db) { | ||
31 | for (field, ty) in receiver.fields(ctx.db) { | ||
32 | if ctx.scope.module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) { | ||
33 | // Skip private field. FIXME: If the definition location of the | ||
34 | // field is editable, we should show the completion | ||
35 | continue; | ||
36 | } | ||
37 | acc.add_field(ctx, field, &ty); | ||
38 | } | ||
39 | for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { | ||
40 | // FIXME: Handle visibility | ||
41 | acc.add_tuple_field(ctx, i, &ty); | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { | ||
47 | if let Some(krate) = ctx.krate { | ||
48 | let mut seen_methods = FxHashSet::default(); | ||
49 | let traits_in_scope = ctx.scope.traits_in_scope(); | ||
50 | receiver.iterate_method_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, func| { | ||
51 | if func.self_param(ctx.db).is_some() | ||
52 | && ctx.scope.module().map_or(true, |m| func.is_visible_from(ctx.db, m)) | ||
53 | && seen_methods.insert(func.name(ctx.db)) | ||
54 | { | ||
55 | acc.add_function(ctx, func, None); | ||
56 | } | ||
57 | None::<()> | ||
58 | }); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | #[cfg(test)] | ||
63 | mod tests { | ||
64 | use expect_test::{expect, Expect}; | ||
65 | use test_utils::mark; | ||
66 | |||
67 | use crate::{test_utils::completion_list, CompletionKind}; | ||
68 | |||
69 | fn check(ra_fixture: &str, expect: Expect) { | ||
70 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
71 | expect.assert_eq(&actual); | ||
72 | } | ||
73 | |||
74 | #[test] | ||
75 | fn test_struct_field_and_method_completion() { | ||
76 | check( | ||
77 | r#" | ||
78 | struct S { foo: u32 } | ||
79 | impl S { | ||
80 | fn bar(&self) {} | ||
81 | } | ||
82 | fn foo(s: S) { s.<|> } | ||
83 | "#, | ||
84 | expect![[r#" | ||
85 | me bar() fn bar(&self) | ||
86 | fd foo u32 | ||
87 | "#]], | ||
88 | ); | ||
89 | } | ||
90 | |||
91 | #[test] | ||
92 | fn test_struct_field_completion_self() { | ||
93 | check( | ||
94 | r#" | ||
95 | struct S { the_field: (u32,) } | ||
96 | impl S { | ||
97 | fn foo(self) { self.<|> } | ||
98 | } | ||
99 | "#, | ||
100 | expect![[r#" | ||
101 | me foo() fn foo(self) | ||
102 | fd the_field (u32,) | ||
103 | "#]], | ||
104 | ) | ||
105 | } | ||
106 | |||
107 | #[test] | ||
108 | fn test_struct_field_completion_autoderef() { | ||
109 | check( | ||
110 | r#" | ||
111 | struct A { the_field: (u32, i32) } | ||
112 | impl A { | ||
113 | fn foo(&self) { self.<|> } | ||
114 | } | ||
115 | "#, | ||
116 | expect![[r#" | ||
117 | me foo() fn foo(&self) | ||
118 | fd the_field (u32, i32) | ||
119 | "#]], | ||
120 | ) | ||
121 | } | ||
122 | |||
123 | #[test] | ||
124 | fn test_no_struct_field_completion_for_method_call() { | ||
125 | mark::check!(test_no_struct_field_completion_for_method_call); | ||
126 | check( | ||
127 | r#" | ||
128 | struct A { the_field: u32 } | ||
129 | fn foo(a: A) { a.<|>() } | ||
130 | "#, | ||
131 | expect![[""]], | ||
132 | ); | ||
133 | } | ||
134 | |||
135 | #[test] | ||
136 | fn test_visibility_filtering() { | ||
137 | check( | ||
138 | r#" | ||
139 | mod inner { | ||
140 | pub struct A { | ||
141 | private_field: u32, | ||
142 | pub pub_field: u32, | ||
143 | pub(crate) crate_field: u32, | ||
144 | pub(crate) super_field: u32, | ||
145 | } | ||
146 | } | ||
147 | fn foo(a: inner::A) { a.<|> } | ||
148 | "#, | ||
149 | expect![[r#" | ||
150 | fd crate_field u32 | ||
151 | fd pub_field u32 | ||
152 | fd super_field u32 | ||
153 | "#]], | ||
154 | ); | ||
155 | |||
156 | check( | ||
157 | r#" | ||
158 | struct A {} | ||
159 | mod m { | ||
160 | impl super::A { | ||
161 | fn private_method(&self) {} | ||
162 | pub(crate) fn the_method(&self) {} | ||
163 | } | ||
164 | } | ||
165 | fn foo(a: A) { a.<|> } | ||
166 | "#, | ||
167 | expect![[r#" | ||
168 | me the_method() pub(crate) fn the_method(&self) | ||
169 | "#]], | ||
170 | ); | ||
171 | } | ||
172 | |||
173 | #[test] | ||
174 | fn test_union_field_completion() { | ||
175 | check( | ||
176 | r#" | ||
177 | union U { field: u8, other: u16 } | ||
178 | fn foo(u: U) { u.<|> } | ||
179 | "#, | ||
180 | expect![[r#" | ||
181 | fd field u8 | ||
182 | fd other u16 | ||
183 | "#]], | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | #[test] | ||
188 | fn test_method_completion_only_fitting_impls() { | ||
189 | check( | ||
190 | r#" | ||
191 | struct A<T> {} | ||
192 | impl A<u32> { | ||
193 | fn the_method(&self) {} | ||
194 | } | ||
195 | impl A<i32> { | ||
196 | fn the_other_method(&self) {} | ||
197 | } | ||
198 | fn foo(a: A<u32>) { a.<|> } | ||
199 | "#, | ||
200 | expect![[r#" | ||
201 | me the_method() fn the_method(&self) | ||
202 | "#]], | ||
203 | ) | ||
204 | } | ||
205 | |||
206 | #[test] | ||
207 | fn test_trait_method_completion() { | ||
208 | check( | ||
209 | r#" | ||
210 | struct A {} | ||
211 | trait Trait { fn the_method(&self); } | ||
212 | impl Trait for A {} | ||
213 | fn foo(a: A) { a.<|> } | ||
214 | "#, | ||
215 | expect![[r#" | ||
216 | me the_method() fn the_method(&self) | ||
217 | "#]], | ||
218 | ); | ||
219 | } | ||
220 | |||
221 | #[test] | ||
222 | fn test_trait_method_completion_deduplicated() { | ||
223 | check( | ||
224 | r" | ||
225 | struct A {} | ||
226 | trait Trait { fn the_method(&self); } | ||
227 | impl<T> Trait for T {} | ||
228 | fn foo(a: &A) { a.<|> } | ||
229 | ", | ||
230 | expect![[r#" | ||
231 | me the_method() fn the_method(&self) | ||
232 | "#]], | ||
233 | ); | ||
234 | } | ||
235 | |||
236 | #[test] | ||
237 | fn completes_trait_method_from_other_module() { | ||
238 | check( | ||
239 | r" | ||
240 | struct A {} | ||
241 | mod m { | ||
242 | pub trait Trait { fn the_method(&self); } | ||
243 | } | ||
244 | use m::Trait; | ||
245 | impl Trait for A {} | ||
246 | fn foo(a: A) { a.<|> } | ||
247 | ", | ||
248 | expect![[r#" | ||
249 | me the_method() fn the_method(&self) | ||
250 | "#]], | ||
251 | ); | ||
252 | } | ||
253 | |||
254 | #[test] | ||
255 | fn test_no_non_self_method() { | ||
256 | check( | ||
257 | r#" | ||
258 | struct A {} | ||
259 | impl A { | ||
260 | fn the_method() {} | ||
261 | } | ||
262 | fn foo(a: A) { | ||
263 | a.<|> | ||
264 | } | ||
265 | "#, | ||
266 | expect![[""]], | ||
267 | ); | ||
268 | } | ||
269 | |||
270 | #[test] | ||
271 | fn test_tuple_field_completion() { | ||
272 | check( | ||
273 | r#" | ||
274 | fn foo() { | ||
275 | let b = (0, 3.14); | ||
276 | b.<|> | ||
277 | } | ||
278 | "#, | ||
279 | expect![[r#" | ||
280 | fd 0 i32 | ||
281 | fd 1 f64 | ||
282 | "#]], | ||
283 | ) | ||
284 | } | ||
285 | |||
286 | #[test] | ||
287 | fn test_tuple_field_inference() { | ||
288 | check( | ||
289 | r#" | ||
290 | pub struct S; | ||
291 | impl S { pub fn blah(&self) {} } | ||
292 | |||
293 | struct T(S); | ||
294 | |||
295 | impl T { | ||
296 | fn foo(&self) { | ||
297 | // FIXME: This doesn't work without the trailing `a` as `0.` is a float | ||
298 | self.0.a<|> | ||
299 | } | ||
300 | } | ||
301 | "#, | ||
302 | expect![[r#" | ||
303 | me blah() pub fn blah(&self) | ||
304 | "#]], | ||
305 | ); | ||
306 | } | ||
307 | |||
308 | #[test] | ||
309 | fn test_completion_works_in_consts() { | ||
310 | check( | ||
311 | r#" | ||
312 | struct A { the_field: u32 } | ||
313 | const X: u32 = { | ||
314 | A { the_field: 92 }.<|> | ||
315 | }; | ||
316 | "#, | ||
317 | expect![[r#" | ||
318 | fd the_field u32 | ||
319 | "#]], | ||
320 | ); | ||
321 | } | ||
322 | |||
323 | #[test] | ||
324 | fn works_in_simple_macro_1() { | ||
325 | check( | ||
326 | r#" | ||
327 | macro_rules! m { ($e:expr) => { $e } } | ||
328 | struct A { the_field: u32 } | ||
329 | fn foo(a: A) { | ||
330 | m!(a.x<|>) | ||
331 | } | ||
332 | "#, | ||
333 | expect![[r#" | ||
334 | fd the_field u32 | ||
335 | "#]], | ||
336 | ); | ||
337 | } | ||
338 | |||
339 | #[test] | ||
340 | fn works_in_simple_macro_2() { | ||
341 | // this doesn't work yet because the macro doesn't expand without the token -- maybe it can be fixed with better recovery | ||
342 | check( | ||
343 | r#" | ||
344 | macro_rules! m { ($e:expr) => { $e } } | ||
345 | struct A { the_field: u32 } | ||
346 | fn foo(a: A) { | ||
347 | m!(a.<|>) | ||
348 | } | ||
349 | "#, | ||
350 | expect![[r#" | ||
351 | fd the_field u32 | ||
352 | "#]], | ||
353 | ); | ||
354 | } | ||
355 | |||
356 | #[test] | ||
357 | fn works_in_simple_macro_recursive_1() { | ||
358 | check( | ||
359 | r#" | ||
360 | macro_rules! m { ($e:expr) => { $e } } | ||
361 | struct A { the_field: u32 } | ||
362 | fn foo(a: A) { | ||
363 | m!(m!(m!(a.x<|>))) | ||
364 | } | ||
365 | "#, | ||
366 | expect![[r#" | ||
367 | fd the_field u32 | ||
368 | "#]], | ||
369 | ); | ||
370 | } | ||
371 | |||
372 | #[test] | ||
373 | fn macro_expansion_resilient() { | ||
374 | check( | ||
375 | r#" | ||
376 | macro_rules! dbg { | ||
377 | () => {}; | ||
378 | ($val:expr) => { | ||
379 | match $val { tmp => { tmp } } | ||
380 | }; | ||
381 | // Trailing comma with single argument is ignored | ||
382 | ($val:expr,) => { $crate::dbg!($val) }; | ||
383 | ($($val:expr),+ $(,)?) => { | ||
384 | ($($crate::dbg!($val)),+,) | ||
385 | }; | ||
386 | } | ||
387 | struct A { the_field: u32 } | ||
388 | fn foo(a: A) { | ||
389 | dbg!(a.<|>) | ||
390 | } | ||
391 | "#, | ||
392 | expect![[r#" | ||
393 | fd the_field u32 | ||
394 | "#]], | ||
395 | ); | ||
396 | } | ||
397 | |||
398 | #[test] | ||
399 | fn test_method_completion_issue_3547() { | ||
400 | check( | ||
401 | r#" | ||
402 | struct HashSet<T> {} | ||
403 | impl<T> HashSet<T> { | ||
404 | pub fn the_method(&self) {} | ||
405 | } | ||
406 | fn foo() { | ||
407 | let s: HashSet<_>; | ||
408 | s.<|> | ||
409 | } | ||
410 | "#, | ||
411 | expect![[r#" | ||
412 | me the_method() pub fn the_method(&self) | ||
413 | "#]], | ||
414 | ); | ||
415 | } | ||
416 | |||
417 | #[test] | ||
418 | fn completes_method_call_when_receiver_is_a_macro_call() { | ||
419 | check( | ||
420 | r#" | ||
421 | struct S; | ||
422 | impl S { fn foo(&self) {} } | ||
423 | macro_rules! make_s { () => { S }; } | ||
424 | fn main() { make_s!().f<|>; } | ||
425 | "#, | ||
426 | expect![[r#" | ||
427 | me foo() fn foo(&self) | ||
428 | "#]], | ||
429 | ) | ||
430 | } | ||
431 | } | ||
diff --git a/crates/completion/src/completions/fn_param.rs b/crates/completion/src/completions/fn_param.rs new file mode 100644 index 000000000..e777a53c1 --- /dev/null +++ b/crates/completion/src/completions/fn_param.rs | |||
@@ -0,0 +1,135 @@ | |||
1 | //! See `complete_fn_param`. | ||
2 | |||
3 | use rustc_hash::FxHashMap; | ||
4 | use syntax::{ | ||
5 | ast::{self, ModuleItemOwner}, | ||
6 | match_ast, AstNode, | ||
7 | }; | ||
8 | |||
9 | use crate::{CompletionContext, CompletionItem, CompletionKind, Completions}; | ||
10 | |||
11 | /// Complete repeated parameters, both name and type. For example, if all | ||
12 | /// functions in a file have a `spam: &mut Spam` parameter, a completion with | ||
13 | /// `spam: &mut Spam` insert text/label and `spam` lookup string will be | ||
14 | /// suggested. | ||
15 | pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) { | ||
16 | if !ctx.is_param { | ||
17 | return; | ||
18 | } | ||
19 | |||
20 | let mut params = FxHashMap::default(); | ||
21 | |||
22 | let me = ctx.token.ancestors().find_map(ast::Fn::cast); | ||
23 | let mut process_fn = |func: ast::Fn| { | ||
24 | if Some(&func) == me.as_ref() { | ||
25 | return; | ||
26 | } | ||
27 | func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| { | ||
28 | let text = param.syntax().text().to_string(); | ||
29 | params.entry(text).or_insert(param); | ||
30 | }) | ||
31 | }; | ||
32 | |||
33 | for node in ctx.token.parent().ancestors() { | ||
34 | match_ast! { | ||
35 | match node { | ||
36 | ast::SourceFile(it) => it.items().filter_map(|item| match item { | ||
37 | ast::Item::Fn(it) => Some(it), | ||
38 | _ => None, | ||
39 | }).for_each(&mut process_fn), | ||
40 | ast::ItemList(it) => it.items().filter_map(|item| match item { | ||
41 | ast::Item::Fn(it) => Some(it), | ||
42 | _ => None, | ||
43 | }).for_each(&mut process_fn), | ||
44 | ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item { | ||
45 | ast::AssocItem::Fn(it) => Some(it), | ||
46 | _ => None, | ||
47 | }).for_each(&mut process_fn), | ||
48 | _ => continue, | ||
49 | } | ||
50 | }; | ||
51 | } | ||
52 | |||
53 | params | ||
54 | .into_iter() | ||
55 | .filter_map(|(label, param)| { | ||
56 | let lookup = param.pat()?.syntax().text().to_string(); | ||
57 | Some((label, lookup)) | ||
58 | }) | ||
59 | .for_each(|(label, lookup)| { | ||
60 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) | ||
61 | .kind(crate::CompletionItemKind::Binding) | ||
62 | .lookup_by(lookup) | ||
63 | .add_to(acc) | ||
64 | }); | ||
65 | } | ||
66 | |||
67 | #[cfg(test)] | ||
68 | mod tests { | ||
69 | use expect_test::{expect, Expect}; | ||
70 | |||
71 | use crate::{test_utils::completion_list, CompletionKind}; | ||
72 | |||
73 | fn check(ra_fixture: &str, expect: Expect) { | ||
74 | let actual = completion_list(ra_fixture, CompletionKind::Magic); | ||
75 | expect.assert_eq(&actual); | ||
76 | } | ||
77 | |||
78 | #[test] | ||
79 | fn test_param_completion_last_param() { | ||
80 | check( | ||
81 | r#" | ||
82 | fn foo(file_id: FileId) {} | ||
83 | fn bar(file_id: FileId) {} | ||
84 | fn baz(file<|>) {} | ||
85 | "#, | ||
86 | expect![[r#" | ||
87 | bn file_id: FileId | ||
88 | "#]], | ||
89 | ); | ||
90 | } | ||
91 | |||
92 | #[test] | ||
93 | fn test_param_completion_nth_param() { | ||
94 | check( | ||
95 | r#" | ||
96 | fn foo(file_id: FileId) {} | ||
97 | fn baz(file<|>, x: i32) {} | ||
98 | "#, | ||
99 | expect![[r#" | ||
100 | bn file_id: FileId | ||
101 | "#]], | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | #[test] | ||
106 | fn test_param_completion_trait_param() { | ||
107 | check( | ||
108 | r#" | ||
109 | pub(crate) trait SourceRoot { | ||
110 | pub fn contains(&self, file_id: FileId) -> bool; | ||
111 | pub fn module_map(&self) -> &ModuleMap; | ||
112 | pub fn lines(&self, file_id: FileId) -> &LineIndex; | ||
113 | pub fn syntax(&self, file<|>) | ||
114 | } | ||
115 | "#, | ||
116 | expect![[r#" | ||
117 | bn file_id: FileId | ||
118 | "#]], | ||
119 | ); | ||
120 | } | ||
121 | |||
122 | #[test] | ||
123 | fn completes_param_in_inner_function() { | ||
124 | check( | ||
125 | r#" | ||
126 | fn outer(text: String) { | ||
127 | fn inner(<|>) | ||
128 | } | ||
129 | "#, | ||
130 | expect![[r#" | ||
131 | bn text: String | ||
132 | "#]], | ||
133 | ) | ||
134 | } | ||
135 | } | ||
diff --git a/crates/completion/src/completions/keyword.rs b/crates/completion/src/completions/keyword.rs new file mode 100644 index 000000000..c7df15900 --- /dev/null +++ b/crates/completion/src/completions/keyword.rs | |||
@@ -0,0 +1,566 @@ | |||
1 | //! Completes keywords. | ||
2 | |||
3 | use syntax::{ast, SyntaxKind}; | ||
4 | use test_utils::mark; | ||
5 | |||
6 | use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions}; | ||
7 | |||
8 | pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) { | ||
9 | // complete keyword "crate" in use stmt | ||
10 | let source_range = ctx.source_range(); | ||
11 | |||
12 | if ctx.use_item_syntax.is_some() { | ||
13 | if ctx.path_qual.is_none() { | ||
14 | CompletionItem::new(CompletionKind::Keyword, source_range, "crate::") | ||
15 | .kind(CompletionItemKind::Keyword) | ||
16 | .insert_text("crate::") | ||
17 | .add_to(acc); | ||
18 | } | ||
19 | CompletionItem::new(CompletionKind::Keyword, source_range, "self") | ||
20 | .kind(CompletionItemKind::Keyword) | ||
21 | .add_to(acc); | ||
22 | CompletionItem::new(CompletionKind::Keyword, source_range, "super::") | ||
23 | .kind(CompletionItemKind::Keyword) | ||
24 | .insert_text("super::") | ||
25 | .add_to(acc); | ||
26 | } | ||
27 | |||
28 | // Suggest .await syntax for types that implement Future trait | ||
29 | if let Some(receiver) = &ctx.dot_receiver { | ||
30 | if let Some(ty) = ctx.sema.type_of_expr(receiver) { | ||
31 | if ty.impls_future(ctx.db) { | ||
32 | CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await") | ||
33 | .kind(CompletionItemKind::Keyword) | ||
34 | .detail("expr.await") | ||
35 | .insert_text("await") | ||
36 | .add_to(acc); | ||
37 | } | ||
38 | }; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { | ||
43 | if ctx.token.kind() == SyntaxKind::COMMENT { | ||
44 | mark::hit!(no_keyword_completion_in_comments); | ||
45 | return; | ||
46 | } | ||
47 | |||
48 | let has_trait_or_impl_parent = ctx.has_impl_parent || ctx.has_trait_parent; | ||
49 | if ctx.trait_as_prev_sibling || ctx.impl_as_prev_sibling { | ||
50 | add_keyword(ctx, acc, "where", "where "); | ||
51 | return; | ||
52 | } | ||
53 | if ctx.unsafe_is_prev { | ||
54 | if ctx.has_item_list_or_source_file_parent || ctx.block_expr_parent { | ||
55 | add_keyword(ctx, acc, "fn", "fn $0() {}") | ||
56 | } | ||
57 | |||
58 | if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { | ||
59 | add_keyword(ctx, acc, "trait", "trait $0 {}"); | ||
60 | add_keyword(ctx, acc, "impl", "impl $0 {}"); | ||
61 | } | ||
62 | |||
63 | return; | ||
64 | } | ||
65 | if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent | ||
66 | { | ||
67 | add_keyword(ctx, acc, "fn", "fn $0() {}"); | ||
68 | } | ||
69 | if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { | ||
70 | add_keyword(ctx, acc, "use", "use "); | ||
71 | add_keyword(ctx, acc, "impl", "impl $0 {}"); | ||
72 | add_keyword(ctx, acc, "trait", "trait $0 {}"); | ||
73 | } | ||
74 | |||
75 | if ctx.has_item_list_or_source_file_parent { | ||
76 | add_keyword(ctx, acc, "enum", "enum $0 {}"); | ||
77 | add_keyword(ctx, acc, "struct", "struct $0"); | ||
78 | add_keyword(ctx, acc, "union", "union $0 {}"); | ||
79 | } | ||
80 | |||
81 | if ctx.is_expr { | ||
82 | add_keyword(ctx, acc, "match", "match $0 {}"); | ||
83 | add_keyword(ctx, acc, "while", "while $0 {}"); | ||
84 | add_keyword(ctx, acc, "loop", "loop {$0}"); | ||
85 | add_keyword(ctx, acc, "if", "if "); | ||
86 | add_keyword(ctx, acc, "if let", "if let "); | ||
87 | } | ||
88 | |||
89 | if ctx.if_is_prev || ctx.block_expr_parent { | ||
90 | add_keyword(ctx, acc, "let", "let "); | ||
91 | } | ||
92 | |||
93 | if ctx.after_if { | ||
94 | add_keyword(ctx, acc, "else", "else {$0}"); | ||
95 | add_keyword(ctx, acc, "else if", "else if $0 {}"); | ||
96 | } | ||
97 | if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { | ||
98 | add_keyword(ctx, acc, "mod", "mod $0 {}"); | ||
99 | } | ||
100 | if ctx.bind_pat_parent || ctx.ref_pat_parent { | ||
101 | add_keyword(ctx, acc, "mut", "mut "); | ||
102 | } | ||
103 | if ctx.has_item_list_or_source_file_parent || has_trait_or_impl_parent || ctx.block_expr_parent | ||
104 | { | ||
105 | add_keyword(ctx, acc, "const", "const "); | ||
106 | add_keyword(ctx, acc, "type", "type "); | ||
107 | } | ||
108 | if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { | ||
109 | add_keyword(ctx, acc, "static", "static "); | ||
110 | }; | ||
111 | if (ctx.has_item_list_or_source_file_parent) || ctx.block_expr_parent { | ||
112 | add_keyword(ctx, acc, "extern", "extern "); | ||
113 | } | ||
114 | if ctx.has_item_list_or_source_file_parent | ||
115 | || has_trait_or_impl_parent | ||
116 | || ctx.block_expr_parent | ||
117 | || ctx.is_match_arm | ||
118 | { | ||
119 | add_keyword(ctx, acc, "unsafe", "unsafe "); | ||
120 | } | ||
121 | if ctx.in_loop_body { | ||
122 | if ctx.can_be_stmt { | ||
123 | add_keyword(ctx, acc, "continue", "continue;"); | ||
124 | add_keyword(ctx, acc, "break", "break;"); | ||
125 | } else { | ||
126 | add_keyword(ctx, acc, "continue", "continue"); | ||
127 | add_keyword(ctx, acc, "break", "break"); | ||
128 | } | ||
129 | } | ||
130 | if ctx.has_item_list_or_source_file_parent || ctx.has_impl_parent | ctx.has_field_list_parent { | ||
131 | add_keyword(ctx, acc, "pub(crate)", "pub(crate) "); | ||
132 | add_keyword(ctx, acc, "pub", "pub "); | ||
133 | } | ||
134 | |||
135 | if !ctx.is_trivial_path { | ||
136 | return; | ||
137 | } | ||
138 | let fn_def = match &ctx.function_syntax { | ||
139 | Some(it) => it, | ||
140 | None => return, | ||
141 | }; | ||
142 | acc.add_all(complete_return(ctx, &fn_def, ctx.can_be_stmt)); | ||
143 | } | ||
144 | |||
145 | fn keyword(ctx: &CompletionContext, kw: &str, snippet: &str) -> CompletionItem { | ||
146 | let res = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) | ||
147 | .kind(CompletionItemKind::Keyword); | ||
148 | |||
149 | match ctx.config.snippet_cap { | ||
150 | Some(cap) => res.insert_snippet(cap, snippet), | ||
151 | _ => res.insert_text(if snippet.contains('$') { kw } else { snippet }), | ||
152 | } | ||
153 | .build() | ||
154 | } | ||
155 | |||
156 | fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { | ||
157 | acc.add(keyword(ctx, kw, snippet)); | ||
158 | } | ||
159 | |||
160 | fn complete_return( | ||
161 | ctx: &CompletionContext, | ||
162 | fn_def: &ast::Fn, | ||
163 | can_be_stmt: bool, | ||
164 | ) -> Option<CompletionItem> { | ||
165 | let snip = match (can_be_stmt, fn_def.ret_type().is_some()) { | ||
166 | (true, true) => "return $0;", | ||
167 | (true, false) => "return;", | ||
168 | (false, true) => "return $0", | ||
169 | (false, false) => "return", | ||
170 | }; | ||
171 | Some(keyword(ctx, "return", snip)) | ||
172 | } | ||
173 | |||
174 | #[cfg(test)] | ||
175 | mod tests { | ||
176 | use expect_test::{expect, Expect}; | ||
177 | |||
178 | use crate::{ | ||
179 | test_utils::{check_edit, completion_list}, | ||
180 | CompletionKind, | ||
181 | }; | ||
182 | use test_utils::mark; | ||
183 | |||
184 | fn check(ra_fixture: &str, expect: Expect) { | ||
185 | let actual = completion_list(ra_fixture, CompletionKind::Keyword); | ||
186 | expect.assert_eq(&actual) | ||
187 | } | ||
188 | |||
189 | #[test] | ||
190 | fn test_keywords_in_use_stmt() { | ||
191 | check( | ||
192 | r"use <|>", | ||
193 | expect![[r#" | ||
194 | kw crate:: | ||
195 | kw self | ||
196 | kw super:: | ||
197 | "#]], | ||
198 | ); | ||
199 | |||
200 | check( | ||
201 | r"use a::<|>", | ||
202 | expect![[r#" | ||
203 | kw self | ||
204 | kw super:: | ||
205 | "#]], | ||
206 | ); | ||
207 | |||
208 | check( | ||
209 | r"use a::{b, <|>}", | ||
210 | expect![[r#" | ||
211 | kw self | ||
212 | kw super:: | ||
213 | "#]], | ||
214 | ); | ||
215 | } | ||
216 | |||
217 | #[test] | ||
218 | fn test_keywords_at_source_file_level() { | ||
219 | check( | ||
220 | r"m<|>", | ||
221 | expect![[r#" | ||
222 | kw const | ||
223 | kw enum | ||
224 | kw extern | ||
225 | kw fn | ||
226 | kw impl | ||
227 | kw mod | ||
228 | kw pub | ||
229 | kw pub(crate) | ||
230 | kw static | ||
231 | kw struct | ||
232 | kw trait | ||
233 | kw type | ||
234 | kw union | ||
235 | kw unsafe | ||
236 | kw use | ||
237 | "#]], | ||
238 | ); | ||
239 | } | ||
240 | |||
241 | #[test] | ||
242 | fn test_keywords_in_function() { | ||
243 | check( | ||
244 | r"fn quux() { <|> }", | ||
245 | expect![[r#" | ||
246 | kw const | ||
247 | kw extern | ||
248 | kw fn | ||
249 | kw if | ||
250 | kw if let | ||
251 | kw impl | ||
252 | kw let | ||
253 | kw loop | ||
254 | kw match | ||
255 | kw mod | ||
256 | kw return | ||
257 | kw static | ||
258 | kw trait | ||
259 | kw type | ||
260 | kw unsafe | ||
261 | kw use | ||
262 | kw while | ||
263 | "#]], | ||
264 | ); | ||
265 | } | ||
266 | |||
267 | #[test] | ||
268 | fn test_keywords_inside_block() { | ||
269 | check( | ||
270 | r"fn quux() { if true { <|> } }", | ||
271 | expect![[r#" | ||
272 | kw const | ||
273 | kw extern | ||
274 | kw fn | ||
275 | kw if | ||
276 | kw if let | ||
277 | kw impl | ||
278 | kw let | ||
279 | kw loop | ||
280 | kw match | ||
281 | kw mod | ||
282 | kw return | ||
283 | kw static | ||
284 | kw trait | ||
285 | kw type | ||
286 | kw unsafe | ||
287 | kw use | ||
288 | kw while | ||
289 | "#]], | ||
290 | ); | ||
291 | } | ||
292 | |||
293 | #[test] | ||
294 | fn test_keywords_after_if() { | ||
295 | check( | ||
296 | r#"fn quux() { if true { () } <|> }"#, | ||
297 | expect![[r#" | ||
298 | kw const | ||
299 | kw else | ||
300 | kw else if | ||
301 | kw extern | ||
302 | kw fn | ||
303 | kw if | ||
304 | kw if let | ||
305 | kw impl | ||
306 | kw let | ||
307 | kw loop | ||
308 | kw match | ||
309 | kw mod | ||
310 | kw return | ||
311 | kw static | ||
312 | kw trait | ||
313 | kw type | ||
314 | kw unsafe | ||
315 | kw use | ||
316 | kw while | ||
317 | "#]], | ||
318 | ); | ||
319 | check_edit( | ||
320 | "else", | ||
321 | r#"fn quux() { if true { () } <|> }"#, | ||
322 | r#"fn quux() { if true { () } else {$0} }"#, | ||
323 | ); | ||
324 | } | ||
325 | |||
326 | #[test] | ||
327 | fn test_keywords_in_match_arm() { | ||
328 | check( | ||
329 | r#" | ||
330 | fn quux() -> i32 { | ||
331 | match () { () => <|> } | ||
332 | } | ||
333 | "#, | ||
334 | expect![[r#" | ||
335 | kw if | ||
336 | kw if let | ||
337 | kw loop | ||
338 | kw match | ||
339 | kw return | ||
340 | kw unsafe | ||
341 | kw while | ||
342 | "#]], | ||
343 | ); | ||
344 | } | ||
345 | |||
346 | #[test] | ||
347 | fn test_keywords_in_trait_def() { | ||
348 | check( | ||
349 | r"trait My { <|> }", | ||
350 | expect![[r#" | ||
351 | kw const | ||
352 | kw fn | ||
353 | kw type | ||
354 | kw unsafe | ||
355 | "#]], | ||
356 | ); | ||
357 | } | ||
358 | |||
359 | #[test] | ||
360 | fn test_keywords_in_impl_def() { | ||
361 | check( | ||
362 | r"impl My { <|> }", | ||
363 | expect![[r#" | ||
364 | kw const | ||
365 | kw fn | ||
366 | kw pub | ||
367 | kw pub(crate) | ||
368 | kw type | ||
369 | kw unsafe | ||
370 | "#]], | ||
371 | ); | ||
372 | } | ||
373 | |||
374 | #[test] | ||
375 | fn test_keywords_in_loop() { | ||
376 | check( | ||
377 | r"fn my() { loop { <|> } }", | ||
378 | expect![[r#" | ||
379 | kw break | ||
380 | kw const | ||
381 | kw continue | ||
382 | kw extern | ||
383 | kw fn | ||
384 | kw if | ||
385 | kw if let | ||
386 | kw impl | ||
387 | kw let | ||
388 | kw loop | ||
389 | kw match | ||
390 | kw mod | ||
391 | kw return | ||
392 | kw static | ||
393 | kw trait | ||
394 | kw type | ||
395 | kw unsafe | ||
396 | kw use | ||
397 | kw while | ||
398 | "#]], | ||
399 | ); | ||
400 | } | ||
401 | |||
402 | #[test] | ||
403 | fn test_keywords_after_unsafe_in_item_list() { | ||
404 | check( | ||
405 | r"unsafe <|>", | ||
406 | expect![[r#" | ||
407 | kw fn | ||
408 | kw impl | ||
409 | kw trait | ||
410 | "#]], | ||
411 | ); | ||
412 | } | ||
413 | |||
414 | #[test] | ||
415 | fn test_keywords_after_unsafe_in_block_expr() { | ||
416 | check( | ||
417 | r"fn my_fn() { unsafe <|> }", | ||
418 | expect![[r#" | ||
419 | kw fn | ||
420 | kw impl | ||
421 | kw trait | ||
422 | "#]], | ||
423 | ); | ||
424 | } | ||
425 | |||
426 | #[test] | ||
427 | fn test_mut_in_ref_and_in_fn_parameters_list() { | ||
428 | check( | ||
429 | r"fn my_fn(&<|>) {}", | ||
430 | expect![[r#" | ||
431 | kw mut | ||
432 | "#]], | ||
433 | ); | ||
434 | check( | ||
435 | r"fn my_fn(<|>) {}", | ||
436 | expect![[r#" | ||
437 | kw mut | ||
438 | "#]], | ||
439 | ); | ||
440 | check( | ||
441 | r"fn my_fn() { let &<|> }", | ||
442 | expect![[r#" | ||
443 | kw mut | ||
444 | "#]], | ||
445 | ); | ||
446 | } | ||
447 | |||
448 | #[test] | ||
449 | fn test_where_keyword() { | ||
450 | check( | ||
451 | r"trait A <|>", | ||
452 | expect![[r#" | ||
453 | kw where | ||
454 | "#]], | ||
455 | ); | ||
456 | check( | ||
457 | r"impl A <|>", | ||
458 | expect![[r#" | ||
459 | kw where | ||
460 | "#]], | ||
461 | ); | ||
462 | } | ||
463 | |||
464 | #[test] | ||
465 | fn no_keyword_completion_in_comments() { | ||
466 | mark::check!(no_keyword_completion_in_comments); | ||
467 | check( | ||
468 | r#" | ||
469 | fn test() { | ||
470 | let x = 2; // A comment<|> | ||
471 | } | ||
472 | "#, | ||
473 | expect![[""]], | ||
474 | ); | ||
475 | check( | ||
476 | r#" | ||
477 | /* | ||
478 | Some multi-line comment<|> | ||
479 | */ | ||
480 | "#, | ||
481 | expect![[""]], | ||
482 | ); | ||
483 | check( | ||
484 | r#" | ||
485 | /// Some doc comment | ||
486 | /// let test<|> = 1 | ||
487 | "#, | ||
488 | expect![[""]], | ||
489 | ); | ||
490 | } | ||
491 | |||
492 | #[test] | ||
493 | fn test_completion_await_impls_future() { | ||
494 | check( | ||
495 | r#" | ||
496 | //- /main.rs crate:main deps:std | ||
497 | use std::future::*; | ||
498 | struct A {} | ||
499 | impl Future for A {} | ||
500 | fn foo(a: A) { a.<|> } | ||
501 | |||
502 | //- /std/lib.rs crate:std | ||
503 | pub mod future { | ||
504 | #[lang = "future_trait"] | ||
505 | pub trait Future {} | ||
506 | } | ||
507 | "#, | ||
508 | expect![[r#" | ||
509 | kw await expr.await | ||
510 | "#]], | ||
511 | ); | ||
512 | |||
513 | check( | ||
514 | r#" | ||
515 | //- /main.rs crate:main deps:std | ||
516 | use std::future::*; | ||
517 | fn foo() { | ||
518 | let a = async {}; | ||
519 | a.<|> | ||
520 | } | ||
521 | |||
522 | //- /std/lib.rs crate:std | ||
523 | pub mod future { | ||
524 | #[lang = "future_trait"] | ||
525 | pub trait Future { | ||
526 | type Output; | ||
527 | } | ||
528 | } | ||
529 | "#, | ||
530 | expect![[r#" | ||
531 | kw await expr.await | ||
532 | "#]], | ||
533 | ) | ||
534 | } | ||
535 | |||
536 | #[test] | ||
537 | fn after_let() { | ||
538 | check( | ||
539 | r#"fn main() { let _ = <|> }"#, | ||
540 | expect![[r#" | ||
541 | kw if | ||
542 | kw if let | ||
543 | kw loop | ||
544 | kw match | ||
545 | kw return | ||
546 | kw while | ||
547 | "#]], | ||
548 | ) | ||
549 | } | ||
550 | |||
551 | #[test] | ||
552 | fn before_field() { | ||
553 | check( | ||
554 | r#" | ||
555 | struct Foo { | ||
556 | <|> | ||
557 | pub f: i32, | ||
558 | } | ||
559 | "#, | ||
560 | expect![[r#" | ||
561 | kw pub | ||
562 | kw pub(crate) | ||
563 | "#]], | ||
564 | ) | ||
565 | } | ||
566 | } | ||
diff --git a/crates/completion/src/completions/macro_in_item_position.rs b/crates/completion/src/completions/macro_in_item_position.rs new file mode 100644 index 000000000..82884a181 --- /dev/null +++ b/crates/completion/src/completions/macro_in_item_position.rs | |||
@@ -0,0 +1,41 @@ | |||
1 | //! Completes macro invocations used in item position. | ||
2 | |||
3 | use crate::{CompletionContext, Completions}; | ||
4 | |||
5 | pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) { | ||
6 | // Show only macros in top level. | ||
7 | if ctx.is_new_item { | ||
8 | ctx.scope.process_all_names(&mut |name, res| { | ||
9 | if let hir::ScopeDef::MacroDef(mac) = res { | ||
10 | acc.add_macro(ctx, Some(name.to_string()), mac); | ||
11 | } | ||
12 | }) | ||
13 | } | ||
14 | } | ||
15 | |||
16 | #[cfg(test)] | ||
17 | mod tests { | ||
18 | use expect_test::{expect, Expect}; | ||
19 | |||
20 | use crate::{test_utils::completion_list, CompletionKind}; | ||
21 | |||
22 | fn check(ra_fixture: &str, expect: Expect) { | ||
23 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
24 | expect.assert_eq(&actual) | ||
25 | } | ||
26 | |||
27 | #[test] | ||
28 | fn completes_macros_as_item() { | ||
29 | check( | ||
30 | r#" | ||
31 | macro_rules! foo { () => {} } | ||
32 | fn foo() {} | ||
33 | |||
34 | <|> | ||
35 | "#, | ||
36 | expect![[r#" | ||
37 | ma foo!(…) macro_rules! foo | ||
38 | "#]], | ||
39 | ) | ||
40 | } | ||
41 | } | ||
diff --git a/crates/completion/src/completions/mod_.rs b/crates/completion/src/completions/mod_.rs new file mode 100644 index 000000000..c96f84171 --- /dev/null +++ b/crates/completion/src/completions/mod_.rs | |||
@@ -0,0 +1,320 @@ | |||
1 | //! Completes mod declarations. | ||
2 | |||
3 | use hir::{Module, ModuleSource}; | ||
4 | use ide_db::base_db::{SourceDatabaseExt, VfsPath}; | ||
5 | use ide_db::RootDatabase; | ||
6 | use rustc_hash::FxHashSet; | ||
7 | |||
8 | use crate::{CompletionItem, CompletionItemKind}; | ||
9 | |||
10 | use crate::{context::CompletionContext, item::CompletionKind, Completions}; | ||
11 | |||
12 | /// Complete mod declaration, i.e. `mod <|> ;` | ||
13 | pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | ||
14 | let mod_under_caret = match &ctx.mod_declaration_under_caret { | ||
15 | Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None, | ||
16 | Some(mod_under_caret) => mod_under_caret, | ||
17 | None => return None, | ||
18 | }; | ||
19 | |||
20 | let _p = profile::span("completion::complete_mod"); | ||
21 | |||
22 | let current_module = ctx.scope.module()?; | ||
23 | |||
24 | let module_definition_file = | ||
25 | current_module.definition_source(ctx.db).file_id.original_file(ctx.db); | ||
26 | let source_root = ctx.db.source_root(ctx.db.file_source_root(module_definition_file)); | ||
27 | let directory_to_look_for_submodules = directory_to_look_for_submodules( | ||
28 | current_module, | ||
29 | ctx.db, | ||
30 | source_root.path_for_file(&module_definition_file)?, | ||
31 | )?; | ||
32 | |||
33 | let existing_mod_declarations = current_module | ||
34 | .children(ctx.db) | ||
35 | .filter_map(|module| Some(module.name(ctx.db)?.to_string())) | ||
36 | .collect::<FxHashSet<_>>(); | ||
37 | |||
38 | let module_declaration_file = | ||
39 | current_module.declaration_source(ctx.db).map(|module_declaration_source_file| { | ||
40 | module_declaration_source_file.file_id.original_file(ctx.db) | ||
41 | }); | ||
42 | |||
43 | source_root | ||
44 | .iter() | ||
45 | .filter(|submodule_candidate_file| submodule_candidate_file != &module_definition_file) | ||
46 | .filter(|submodule_candidate_file| { | ||
47 | Some(submodule_candidate_file) != module_declaration_file.as_ref() | ||
48 | }) | ||
49 | .filter_map(|submodule_file| { | ||
50 | let submodule_path = source_root.path_for_file(&submodule_file)?; | ||
51 | let directory_with_submodule = submodule_path.parent()?; | ||
52 | match submodule_path.name_and_extension()? { | ||
53 | ("lib", Some("rs")) | ("main", Some("rs")) => None, | ||
54 | ("mod", Some("rs")) => { | ||
55 | if directory_with_submodule.parent()? == directory_to_look_for_submodules { | ||
56 | match directory_with_submodule.name_and_extension()? { | ||
57 | (directory_name, None) => Some(directory_name.to_owned()), | ||
58 | _ => None, | ||
59 | } | ||
60 | } else { | ||
61 | None | ||
62 | } | ||
63 | } | ||
64 | (file_name, Some("rs")) | ||
65 | if directory_with_submodule == directory_to_look_for_submodules => | ||
66 | { | ||
67 | Some(file_name.to_owned()) | ||
68 | } | ||
69 | _ => None, | ||
70 | } | ||
71 | }) | ||
72 | .filter(|name| !existing_mod_declarations.contains(name)) | ||
73 | .for_each(|submodule_name| { | ||
74 | let mut label = submodule_name; | ||
75 | if mod_under_caret.semicolon_token().is_none() { | ||
76 | label.push(';') | ||
77 | } | ||
78 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label) | ||
79 | .kind(CompletionItemKind::Module) | ||
80 | .add_to(acc) | ||
81 | }); | ||
82 | |||
83 | Some(()) | ||
84 | } | ||
85 | |||
86 | fn directory_to_look_for_submodules( | ||
87 | module: Module, | ||
88 | db: &RootDatabase, | ||
89 | module_file_path: &VfsPath, | ||
90 | ) -> Option<VfsPath> { | ||
91 | let directory_with_module_path = module_file_path.parent()?; | ||
92 | let base_directory = match module_file_path.name_and_extension()? { | ||
93 | ("mod", Some("rs")) | ("lib", Some("rs")) | ("main", Some("rs")) => { | ||
94 | Some(directory_with_module_path) | ||
95 | } | ||
96 | (regular_rust_file_name, Some("rs")) => { | ||
97 | if matches!( | ||
98 | ( | ||
99 | directory_with_module_path | ||
100 | .parent() | ||
101 | .as_ref() | ||
102 | .and_then(|path| path.name_and_extension()), | ||
103 | directory_with_module_path.name_and_extension(), | ||
104 | ), | ||
105 | (Some(("src", None)), Some(("bin", None))) | ||
106 | ) { | ||
107 | // files in /src/bin/ can import each other directly | ||
108 | Some(directory_with_module_path) | ||
109 | } else { | ||
110 | directory_with_module_path.join(regular_rust_file_name) | ||
111 | } | ||
112 | } | ||
113 | _ => None, | ||
114 | }?; | ||
115 | |||
116 | let mut resulting_path = base_directory; | ||
117 | for module in module_chain_to_containing_module_file(module, db) { | ||
118 | if let Some(name) = module.name(db) { | ||
119 | resulting_path = resulting_path.join(&name.to_string())?; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | Some(resulting_path) | ||
124 | } | ||
125 | |||
126 | fn module_chain_to_containing_module_file( | ||
127 | current_module: Module, | ||
128 | db: &RootDatabase, | ||
129 | ) -> Vec<Module> { | ||
130 | let mut path = Vec::new(); | ||
131 | |||
132 | let mut current_module = Some(current_module); | ||
133 | while let Some(ModuleSource::Module(_)) = | ||
134 | current_module.map(|module| module.definition_source(db).value) | ||
135 | { | ||
136 | if let Some(module) = current_module { | ||
137 | path.insert(0, module); | ||
138 | current_module = module.parent(db); | ||
139 | } else { | ||
140 | current_module = None; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | path | ||
145 | } | ||
146 | |||
147 | #[cfg(test)] | ||
148 | mod tests { | ||
149 | use crate::{test_utils::completion_list, CompletionKind}; | ||
150 | use expect_test::{expect, Expect}; | ||
151 | |||
152 | fn check(ra_fixture: &str, expect: Expect) { | ||
153 | let actual = completion_list(ra_fixture, CompletionKind::Magic); | ||
154 | expect.assert_eq(&actual); | ||
155 | } | ||
156 | |||
157 | #[test] | ||
158 | fn lib_module_completion() { | ||
159 | check( | ||
160 | r#" | ||
161 | //- /lib.rs | ||
162 | mod <|> | ||
163 | //- /foo.rs | ||
164 | fn foo() {} | ||
165 | //- /foo/ignored_foo.rs | ||
166 | fn ignored_foo() {} | ||
167 | //- /bar/mod.rs | ||
168 | fn bar() {} | ||
169 | //- /bar/ignored_bar.rs | ||
170 | fn ignored_bar() {} | ||
171 | "#, | ||
172 | expect![[r#" | ||
173 | md bar; | ||
174 | md foo; | ||
175 | "#]], | ||
176 | ); | ||
177 | } | ||
178 | |||
179 | #[test] | ||
180 | fn no_module_completion_with_module_body() { | ||
181 | check( | ||
182 | r#" | ||
183 | //- /lib.rs | ||
184 | mod <|> { | ||
185 | |||
186 | } | ||
187 | //- /foo.rs | ||
188 | fn foo() {} | ||
189 | "#, | ||
190 | expect![[r#""#]], | ||
191 | ); | ||
192 | } | ||
193 | |||
194 | #[test] | ||
195 | fn main_module_completion() { | ||
196 | check( | ||
197 | r#" | ||
198 | //- /main.rs | ||
199 | mod <|> | ||
200 | //- /foo.rs | ||
201 | fn foo() {} | ||
202 | //- /foo/ignored_foo.rs | ||
203 | fn ignored_foo() {} | ||
204 | //- /bar/mod.rs | ||
205 | fn bar() {} | ||
206 | //- /bar/ignored_bar.rs | ||
207 | fn ignored_bar() {} | ||
208 | "#, | ||
209 | expect![[r#" | ||
210 | md bar; | ||
211 | md foo; | ||
212 | "#]], | ||
213 | ); | ||
214 | } | ||
215 | |||
216 | #[test] | ||
217 | fn main_test_module_completion() { | ||
218 | check( | ||
219 | r#" | ||
220 | //- /main.rs | ||
221 | mod tests { | ||
222 | mod <|>; | ||
223 | } | ||
224 | //- /tests/foo.rs | ||
225 | fn foo() {} | ||
226 | "#, | ||
227 | expect![[r#" | ||
228 | md foo | ||
229 | "#]], | ||
230 | ); | ||
231 | } | ||
232 | |||
233 | #[test] | ||
234 | fn directly_nested_module_completion() { | ||
235 | check( | ||
236 | r#" | ||
237 | //- /lib.rs | ||
238 | mod foo; | ||
239 | //- /foo.rs | ||
240 | mod <|>; | ||
241 | //- /foo/bar.rs | ||
242 | fn bar() {} | ||
243 | //- /foo/bar/ignored_bar.rs | ||
244 | fn ignored_bar() {} | ||
245 | //- /foo/baz/mod.rs | ||
246 | fn baz() {} | ||
247 | //- /foo/moar/ignored_moar.rs | ||
248 | fn ignored_moar() {} | ||
249 | "#, | ||
250 | expect![[r#" | ||
251 | md bar | ||
252 | md baz | ||
253 | "#]], | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn nested_in_source_module_completion() { | ||
259 | check( | ||
260 | r#" | ||
261 | //- /lib.rs | ||
262 | mod foo; | ||
263 | //- /foo.rs | ||
264 | mod bar { | ||
265 | mod <|> | ||
266 | } | ||
267 | //- /foo/bar/baz.rs | ||
268 | fn baz() {} | ||
269 | "#, | ||
270 | expect![[r#" | ||
271 | md baz; | ||
272 | "#]], | ||
273 | ); | ||
274 | } | ||
275 | |||
276 | // FIXME binary modules are not supported in tests properly | ||
277 | // Binary modules are a bit special, they allow importing the modules from `/src/bin` | ||
278 | // and that's why are good to test two things: | ||
279 | // * no cycles are allowed in mod declarations | ||
280 | // * no modules from the parent directory are proposed | ||
281 | // Unfortunately, binary modules support is in cargo not rustc, | ||
282 | // hence the test does not work now | ||
283 | // | ||
284 | // #[test] | ||
285 | // fn regular_bin_module_completion() { | ||
286 | // check( | ||
287 | // r#" | ||
288 | // //- /src/bin.rs | ||
289 | // fn main() {} | ||
290 | // //- /src/bin/foo.rs | ||
291 | // mod <|> | ||
292 | // //- /src/bin/bar.rs | ||
293 | // fn bar() {} | ||
294 | // //- /src/bin/bar/bar_ignored.rs | ||
295 | // fn bar_ignored() {} | ||
296 | // "#, | ||
297 | // expect![[r#" | ||
298 | // md bar; | ||
299 | // "#]],foo | ||
300 | // ); | ||
301 | // } | ||
302 | |||
303 | #[test] | ||
304 | fn already_declared_bin_module_completion_omitted() { | ||
305 | check( | ||
306 | r#" | ||
307 | //- /src/bin.rs crate:main | ||
308 | fn main() {} | ||
309 | //- /src/bin/foo.rs | ||
310 | mod <|> | ||
311 | //- /src/bin/bar.rs | ||
312 | mod foo; | ||
313 | fn bar() {} | ||
314 | //- /src/bin/bar/bar_ignored.rs | ||
315 | fn bar_ignored() {} | ||
316 | "#, | ||
317 | expect![[r#""#]], | ||
318 | ); | ||
319 | } | ||
320 | } | ||
diff --git a/crates/completion/src/completions/pattern.rs b/crates/completion/src/completions/pattern.rs new file mode 100644 index 000000000..7ab7f09fe --- /dev/null +++ b/crates/completion/src/completions/pattern.rs | |||
@@ -0,0 +1,88 @@ | |||
1 | //! Completes constats and paths in patterns. | ||
2 | |||
3 | use crate::{CompletionContext, Completions}; | ||
4 | |||
5 | /// Completes constats and paths in patterns. | ||
6 | pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { | ||
7 | if !ctx.is_pat_binding_or_const { | ||
8 | return; | ||
9 | } | ||
10 | if ctx.record_pat_syntax.is_some() { | ||
11 | return; | ||
12 | } | ||
13 | |||
14 | // FIXME: ideally, we should look at the type we are matching against and | ||
15 | // suggest variants + auto-imports | ||
16 | ctx.scope.process_all_names(&mut |name, res| { | ||
17 | match &res { | ||
18 | hir::ScopeDef::ModuleDef(def) => match def { | ||
19 | hir::ModuleDef::Adt(hir::Adt::Enum(..)) | ||
20 | | hir::ModuleDef::Adt(hir::Adt::Struct(..)) | ||
21 | | hir::ModuleDef::EnumVariant(..) | ||
22 | | hir::ModuleDef::Const(..) | ||
23 | | hir::ModuleDef::Module(..) => (), | ||
24 | _ => return, | ||
25 | }, | ||
26 | hir::ScopeDef::MacroDef(_) => (), | ||
27 | _ => return, | ||
28 | }; | ||
29 | |||
30 | acc.add_resolution(ctx, name.to_string(), &res) | ||
31 | }); | ||
32 | } | ||
33 | |||
34 | #[cfg(test)] | ||
35 | mod tests { | ||
36 | use expect_test::{expect, Expect}; | ||
37 | |||
38 | use crate::{test_utils::completion_list, CompletionKind}; | ||
39 | |||
40 | fn check(ra_fixture: &str, expect: Expect) { | ||
41 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
42 | expect.assert_eq(&actual) | ||
43 | } | ||
44 | |||
45 | #[test] | ||
46 | fn completes_enum_variants_and_modules() { | ||
47 | check( | ||
48 | r#" | ||
49 | enum E { X } | ||
50 | use self::E::X; | ||
51 | const Z: E = E::X; | ||
52 | mod m {} | ||
53 | |||
54 | static FOO: E = E::X; | ||
55 | struct Bar { f: u32 } | ||
56 | |||
57 | fn foo() { | ||
58 | match E::X { <|> } | ||
59 | } | ||
60 | "#, | ||
61 | expect![[r#" | ||
62 | st Bar | ||
63 | en E | ||
64 | ev X () | ||
65 | ct Z | ||
66 | md m | ||
67 | "#]], | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | #[test] | ||
72 | fn completes_in_simple_macro_call() { | ||
73 | check( | ||
74 | r#" | ||
75 | macro_rules! m { ($e:expr) => { $e } } | ||
76 | enum E { X } | ||
77 | |||
78 | fn foo() { | ||
79 | m!(match E::X { <|> }) | ||
80 | } | ||
81 | "#, | ||
82 | expect![[r#" | ||
83 | en E | ||
84 | ma m!(…) macro_rules! m | ||
85 | "#]], | ||
86 | ); | ||
87 | } | ||
88 | } | ||
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs new file mode 100644 index 000000000..348f017bd --- /dev/null +++ b/crates/completion/src/completions/postfix.rs | |||
@@ -0,0 +1,452 @@ | |||
1 | //! Postfix completions, like `Ok(10).ifl<|>` => `if let Ok() = Ok(10) { <|> }`. | ||
2 | |||
3 | mod format_like; | ||
4 | |||
5 | use ide_db::ty_filter::TryEnum; | ||
6 | use syntax::{ | ||
7 | ast::{self, AstNode, AstToken}, | ||
8 | TextRange, TextSize, | ||
9 | }; | ||
10 | use text_edit::TextEdit; | ||
11 | |||
12 | use self::format_like::add_format_like_completions; | ||
13 | use crate::{ | ||
14 | config::SnippetCap, | ||
15 | context::CompletionContext, | ||
16 | item::{Builder, CompletionKind}, | ||
17 | CompletionItem, CompletionItemKind, Completions, | ||
18 | }; | ||
19 | |||
20 | pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | ||
21 | if !ctx.config.enable_postfix_completions { | ||
22 | return; | ||
23 | } | ||
24 | |||
25 | let dot_receiver = match &ctx.dot_receiver { | ||
26 | Some(it) => it, | ||
27 | None => return, | ||
28 | }; | ||
29 | |||
30 | let receiver_text = | ||
31 | get_receiver_text(dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); | ||
32 | |||
33 | let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { | ||
34 | Some(it) => it, | ||
35 | None => return, | ||
36 | }; | ||
37 | |||
38 | let cap = match ctx.config.snippet_cap { | ||
39 | Some(it) => it, | ||
40 | None => return, | ||
41 | }; | ||
42 | let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty); | ||
43 | if let Some(try_enum) = &try_enum { | ||
44 | match try_enum { | ||
45 | TryEnum::Result => { | ||
46 | postfix_snippet( | ||
47 | ctx, | ||
48 | cap, | ||
49 | &dot_receiver, | ||
50 | "ifl", | ||
51 | "if let Ok {}", | ||
52 | &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), | ||
53 | ) | ||
54 | .add_to(acc); | ||
55 | |||
56 | postfix_snippet( | ||
57 | ctx, | ||
58 | cap, | ||
59 | &dot_receiver, | ||
60 | "while", | ||
61 | "while let Ok {}", | ||
62 | &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), | ||
63 | ) | ||
64 | .add_to(acc); | ||
65 | } | ||
66 | TryEnum::Option => { | ||
67 | postfix_snippet( | ||
68 | ctx, | ||
69 | cap, | ||
70 | &dot_receiver, | ||
71 | "ifl", | ||
72 | "if let Some {}", | ||
73 | &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), | ||
74 | ) | ||
75 | .add_to(acc); | ||
76 | |||
77 | postfix_snippet( | ||
78 | ctx, | ||
79 | cap, | ||
80 | &dot_receiver, | ||
81 | "while", | ||
82 | "while let Some {}", | ||
83 | &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), | ||
84 | ) | ||
85 | .add_to(acc); | ||
86 | } | ||
87 | } | ||
88 | } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { | ||
89 | postfix_snippet( | ||
90 | ctx, | ||
91 | cap, | ||
92 | &dot_receiver, | ||
93 | "if", | ||
94 | "if expr {}", | ||
95 | &format!("if {} {{\n $0\n}}", receiver_text), | ||
96 | ) | ||
97 | .add_to(acc); | ||
98 | postfix_snippet( | ||
99 | ctx, | ||
100 | cap, | ||
101 | &dot_receiver, | ||
102 | "while", | ||
103 | "while expr {}", | ||
104 | &format!("while {} {{\n $0\n}}", receiver_text), | ||
105 | ) | ||
106 | .add_to(acc); | ||
107 | postfix_snippet(ctx, cap, &dot_receiver, "not", "!expr", &format!("!{}", receiver_text)) | ||
108 | .add_to(acc); | ||
109 | } | ||
110 | |||
111 | postfix_snippet(ctx, cap, &dot_receiver, "ref", "&expr", &format!("&{}", receiver_text)) | ||
112 | .add_to(acc); | ||
113 | postfix_snippet( | ||
114 | ctx, | ||
115 | cap, | ||
116 | &dot_receiver, | ||
117 | "refm", | ||
118 | "&mut expr", | ||
119 | &format!("&mut {}", receiver_text), | ||
120 | ) | ||
121 | .add_to(acc); | ||
122 | |||
123 | // The rest of the postfix completions create an expression that moves an argument, | ||
124 | // so it's better to consider references now to avoid breaking the compilation | ||
125 | let dot_receiver = include_references(dot_receiver); | ||
126 | let receiver_text = | ||
127 | get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); | ||
128 | |||
129 | match try_enum { | ||
130 | Some(try_enum) => match try_enum { | ||
131 | TryEnum::Result => { | ||
132 | postfix_snippet( | ||
133 | ctx, | ||
134 | cap, | ||
135 | &dot_receiver, | ||
136 | "match", | ||
137 | "match expr {}", | ||
138 | &format!("match {} {{\n Ok(${{1:_}}) => {{$2}},\n Err(${{3:_}}) => {{$0}},\n}}", receiver_text), | ||
139 | ) | ||
140 | .add_to(acc); | ||
141 | } | ||
142 | TryEnum::Option => { | ||
143 | postfix_snippet( | ||
144 | ctx, | ||
145 | cap, | ||
146 | &dot_receiver, | ||
147 | "match", | ||
148 | "match expr {}", | ||
149 | &format!( | ||
150 | "match {} {{\n Some(${{1:_}}) => {{$2}},\n None => {{$0}},\n}}", | ||
151 | receiver_text | ||
152 | ), | ||
153 | ) | ||
154 | .add_to(acc); | ||
155 | } | ||
156 | }, | ||
157 | None => { | ||
158 | postfix_snippet( | ||
159 | ctx, | ||
160 | cap, | ||
161 | &dot_receiver, | ||
162 | "match", | ||
163 | "match expr {}", | ||
164 | &format!("match {} {{\n ${{1:_}} => {{$0}},\n}}", receiver_text), | ||
165 | ) | ||
166 | .add_to(acc); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | postfix_snippet( | ||
171 | ctx, | ||
172 | cap, | ||
173 | &dot_receiver, | ||
174 | "box", | ||
175 | "Box::new(expr)", | ||
176 | &format!("Box::new({})", receiver_text), | ||
177 | ) | ||
178 | .add_to(acc); | ||
179 | |||
180 | postfix_snippet(ctx, cap, &dot_receiver, "ok", "Ok(expr)", &format!("Ok({})", receiver_text)) | ||
181 | .add_to(acc); | ||
182 | |||
183 | postfix_snippet( | ||
184 | ctx, | ||
185 | cap, | ||
186 | &dot_receiver, | ||
187 | "dbg", | ||
188 | "dbg!(expr)", | ||
189 | &format!("dbg!({})", receiver_text), | ||
190 | ) | ||
191 | .add_to(acc); | ||
192 | |||
193 | postfix_snippet( | ||
194 | ctx, | ||
195 | cap, | ||
196 | &dot_receiver, | ||
197 | "dbgr", | ||
198 | "dbg!(&expr)", | ||
199 | &format!("dbg!(&{})", receiver_text), | ||
200 | ) | ||
201 | .add_to(acc); | ||
202 | |||
203 | postfix_snippet( | ||
204 | ctx, | ||
205 | cap, | ||
206 | &dot_receiver, | ||
207 | "call", | ||
208 | "function(expr)", | ||
209 | &format!("${{1}}({})", receiver_text), | ||
210 | ) | ||
211 | .add_to(acc); | ||
212 | |||
213 | if let ast::Expr::Literal(literal) = dot_receiver.clone() { | ||
214 | if let Some(literal_text) = ast::String::cast(literal.token()) { | ||
215 | add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | |||
220 | fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String { | ||
221 | if receiver_is_ambiguous_float_literal { | ||
222 | let text = receiver.syntax().text(); | ||
223 | let without_dot = ..text.len() - TextSize::of('.'); | ||
224 | text.slice(without_dot).to_string() | ||
225 | } else { | ||
226 | receiver.to_string() | ||
227 | } | ||
228 | } | ||
229 | |||
230 | fn include_references(initial_element: &ast::Expr) -> ast::Expr { | ||
231 | let mut resulting_element = initial_element.clone(); | ||
232 | while let Some(parent_ref_element) = | ||
233 | resulting_element.syntax().parent().and_then(ast::RefExpr::cast) | ||
234 | { | ||
235 | resulting_element = ast::Expr::from(parent_ref_element); | ||
236 | } | ||
237 | resulting_element | ||
238 | } | ||
239 | |||
240 | fn postfix_snippet( | ||
241 | ctx: &CompletionContext, | ||
242 | cap: SnippetCap, | ||
243 | receiver: &ast::Expr, | ||
244 | label: &str, | ||
245 | detail: &str, | ||
246 | snippet: &str, | ||
247 | ) -> Builder { | ||
248 | let edit = { | ||
249 | let receiver_syntax = receiver.syntax(); | ||
250 | let receiver_range = ctx.sema.original_range(receiver_syntax).range; | ||
251 | let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end()); | ||
252 | TextEdit::replace(delete_range, snippet.to_string()) | ||
253 | }; | ||
254 | CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label) | ||
255 | .detail(detail) | ||
256 | .kind(CompletionItemKind::Snippet) | ||
257 | .snippet_edit(cap, edit) | ||
258 | } | ||
259 | |||
260 | #[cfg(test)] | ||
261 | mod tests { | ||
262 | use expect_test::{expect, Expect}; | ||
263 | |||
264 | use crate::{ | ||
265 | test_utils::{check_edit, completion_list}, | ||
266 | CompletionKind, | ||
267 | }; | ||
268 | |||
269 | fn check(ra_fixture: &str, expect: Expect) { | ||
270 | let actual = completion_list(ra_fixture, CompletionKind::Postfix); | ||
271 | expect.assert_eq(&actual) | ||
272 | } | ||
273 | |||
274 | #[test] | ||
275 | fn postfix_completion_works_for_trivial_path_expression() { | ||
276 | check( | ||
277 | r#" | ||
278 | fn main() { | ||
279 | let bar = true; | ||
280 | bar.<|> | ||
281 | } | ||
282 | "#, | ||
283 | expect![[r#" | ||
284 | sn box Box::new(expr) | ||
285 | sn call function(expr) | ||
286 | sn dbg dbg!(expr) | ||
287 | sn dbgr dbg!(&expr) | ||
288 | sn if if expr {} | ||
289 | sn match match expr {} | ||
290 | sn not !expr | ||
291 | sn ok Ok(expr) | ||
292 | sn ref &expr | ||
293 | sn refm &mut expr | ||
294 | sn while while expr {} | ||
295 | "#]], | ||
296 | ); | ||
297 | } | ||
298 | |||
299 | #[test] | ||
300 | fn postfix_type_filtering() { | ||
301 | check( | ||
302 | r#" | ||
303 | fn main() { | ||
304 | let bar: u8 = 12; | ||
305 | bar.<|> | ||
306 | } | ||
307 | "#, | ||
308 | expect![[r#" | ||
309 | sn box Box::new(expr) | ||
310 | sn call function(expr) | ||
311 | sn dbg dbg!(expr) | ||
312 | sn dbgr dbg!(&expr) | ||
313 | sn match match expr {} | ||
314 | sn ok Ok(expr) | ||
315 | sn ref &expr | ||
316 | sn refm &mut expr | ||
317 | "#]], | ||
318 | ) | ||
319 | } | ||
320 | |||
321 | #[test] | ||
322 | fn option_iflet() { | ||
323 | check_edit( | ||
324 | "ifl", | ||
325 | r#" | ||
326 | enum Option<T> { Some(T), None } | ||
327 | |||
328 | fn main() { | ||
329 | let bar = Option::Some(true); | ||
330 | bar.<|> | ||
331 | } | ||
332 | "#, | ||
333 | r#" | ||
334 | enum Option<T> { Some(T), None } | ||
335 | |||
336 | fn main() { | ||
337 | let bar = Option::Some(true); | ||
338 | if let Some($1) = bar { | ||
339 | $0 | ||
340 | } | ||
341 | } | ||
342 | "#, | ||
343 | ); | ||
344 | } | ||
345 | |||
346 | #[test] | ||
347 | fn result_match() { | ||
348 | check_edit( | ||
349 | "match", | ||
350 | r#" | ||
351 | enum Result<T, E> { Ok(T), Err(E) } | ||
352 | |||
353 | fn main() { | ||
354 | let bar = Result::Ok(true); | ||
355 | bar.<|> | ||
356 | } | ||
357 | "#, | ||
358 | r#" | ||
359 | enum Result<T, E> { Ok(T), Err(E) } | ||
360 | |||
361 | fn main() { | ||
362 | let bar = Result::Ok(true); | ||
363 | match bar { | ||
364 | Ok(${1:_}) => {$2}, | ||
365 | Err(${3:_}) => {$0}, | ||
366 | } | ||
367 | } | ||
368 | "#, | ||
369 | ); | ||
370 | } | ||
371 | |||
372 | #[test] | ||
373 | fn postfix_completion_works_for_ambiguous_float_literal() { | ||
374 | check_edit("refm", r#"fn main() { 42.<|> }"#, r#"fn main() { &mut 42 }"#) | ||
375 | } | ||
376 | |||
377 | #[test] | ||
378 | fn works_in_simple_macro() { | ||
379 | check_edit( | ||
380 | "dbg", | ||
381 | r#" | ||
382 | macro_rules! m { ($e:expr) => { $e } } | ||
383 | fn main() { | ||
384 | let bar: u8 = 12; | ||
385 | m!(bar.d<|>) | ||
386 | } | ||
387 | "#, | ||
388 | r#" | ||
389 | macro_rules! m { ($e:expr) => { $e } } | ||
390 | fn main() { | ||
391 | let bar: u8 = 12; | ||
392 | m!(dbg!(bar)) | ||
393 | } | ||
394 | "#, | ||
395 | ); | ||
396 | } | ||
397 | |||
398 | #[test] | ||
399 | fn postfix_completion_for_references() { | ||
400 | check_edit("dbg", r#"fn main() { &&42.<|> }"#, r#"fn main() { dbg!(&&42) }"#); | ||
401 | check_edit("refm", r#"fn main() { &&42.<|> }"#, r#"fn main() { &&&mut 42 }"#); | ||
402 | } | ||
403 | |||
404 | #[test] | ||
405 | fn postfix_completion_for_format_like_strings() { | ||
406 | check_edit( | ||
407 | "fmt", | ||
408 | r#"fn main() { "{some_var:?}".<|> }"#, | ||
409 | r#"fn main() { format!("{:?}", some_var) }"#, | ||
410 | ); | ||
411 | check_edit( | ||
412 | "panic", | ||
413 | r#"fn main() { "Panic with {a}".<|> }"#, | ||
414 | r#"fn main() { panic!("Panic with {}", a) }"#, | ||
415 | ); | ||
416 | check_edit( | ||
417 | "println", | ||
418 | r#"fn main() { "{ 2+2 } { SomeStruct { val: 1, other: 32 } :?}".<|> }"#, | ||
419 | r#"fn main() { println!("{} {:?}", 2+2, SomeStruct { val: 1, other: 32 }) }"#, | ||
420 | ); | ||
421 | check_edit( | ||
422 | "loge", | ||
423 | r#"fn main() { "{2+2}".<|> }"#, | ||
424 | r#"fn main() { log::error!("{}", 2+2) }"#, | ||
425 | ); | ||
426 | check_edit( | ||
427 | "logt", | ||
428 | r#"fn main() { "{2+2}".<|> }"#, | ||
429 | r#"fn main() { log::trace!("{}", 2+2) }"#, | ||
430 | ); | ||
431 | check_edit( | ||
432 | "logd", | ||
433 | r#"fn main() { "{2+2}".<|> }"#, | ||
434 | r#"fn main() { log::debug!("{}", 2+2) }"#, | ||
435 | ); | ||
436 | check_edit( | ||
437 | "logi", | ||
438 | r#"fn main() { "{2+2}".<|> }"#, | ||
439 | r#"fn main() { log::info!("{}", 2+2) }"#, | ||
440 | ); | ||
441 | check_edit( | ||
442 | "logw", | ||
443 | r#"fn main() { "{2+2}".<|> }"#, | ||
444 | r#"fn main() { log::warn!("{}", 2+2) }"#, | ||
445 | ); | ||
446 | check_edit( | ||
447 | "loge", | ||
448 | r#"fn main() { "{2+2}".<|> }"#, | ||
449 | r#"fn main() { log::error!("{}", 2+2) }"#, | ||
450 | ); | ||
451 | } | ||
452 | } | ||
diff --git a/crates/completion/src/completions/postfix/format_like.rs b/crates/completion/src/completions/postfix/format_like.rs new file mode 100644 index 000000000..f35114ed1 --- /dev/null +++ b/crates/completion/src/completions/postfix/format_like.rs | |||
@@ -0,0 +1,279 @@ | |||
1 | // Feature: Format String Completion. | ||
2 | // | ||
3 | // `"Result {result} is {2 + 2}"` is expanded to the `"Result {} is {}", result, 2 + 2`. | ||
4 | // | ||
5 | // The following postfix snippets are available: | ||
6 | // | ||
7 | // - `format` -> `format!(...)` | ||
8 | // - `panic` -> `panic!(...)` | ||
9 | // - `println` -> `println!(...)` | ||
10 | // - `log`: | ||
11 | // + `logd` -> `log::debug!(...)` | ||
12 | // + `logt` -> `log::trace!(...)` | ||
13 | // + `logi` -> `log::info!(...)` | ||
14 | // + `logw` -> `log::warn!(...)` | ||
15 | // + `loge` -> `log::error!(...)` | ||
16 | |||
17 | use crate::{ | ||
18 | completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext, | ||
19 | Completions, | ||
20 | }; | ||
21 | use syntax::ast::{self, AstToken}; | ||
22 | |||
23 | /// Mapping ("postfix completion item" => "macro to use") | ||
24 | static KINDS: &[(&str, &str)] = &[ | ||
25 | ("fmt", "format!"), | ||
26 | ("panic", "panic!"), | ||
27 | ("println", "println!"), | ||
28 | ("eprintln", "eprintln!"), | ||
29 | ("logd", "log::debug!"), | ||
30 | ("logt", "log::trace!"), | ||
31 | ("logi", "log::info!"), | ||
32 | ("logw", "log::warn!"), | ||
33 | ("loge", "log::error!"), | ||
34 | ]; | ||
35 | |||
36 | pub(crate) fn add_format_like_completions( | ||
37 | acc: &mut Completions, | ||
38 | ctx: &CompletionContext, | ||
39 | dot_receiver: &ast::Expr, | ||
40 | cap: SnippetCap, | ||
41 | receiver_text: &ast::String, | ||
42 | ) { | ||
43 | let input = match string_literal_contents(receiver_text) { | ||
44 | // It's not a string literal, do not parse input. | ||
45 | Some(input) => input, | ||
46 | None => return, | ||
47 | }; | ||
48 | |||
49 | let mut parser = FormatStrParser::new(input); | ||
50 | |||
51 | if parser.parse().is_ok() { | ||
52 | for (label, macro_name) in KINDS { | ||
53 | let snippet = parser.into_suggestion(macro_name); | ||
54 | |||
55 | postfix_snippet(ctx, cap, &dot_receiver, label, macro_name, &snippet).add_to(acc); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | /// Checks whether provided item is a string literal. | ||
61 | fn string_literal_contents(item: &ast::String) -> Option<String> { | ||
62 | let item = item.text(); | ||
63 | if item.len() >= 2 && item.starts_with("\"") && item.ends_with("\"") { | ||
64 | return Some(item[1..item.len() - 1].to_owned()); | ||
65 | } | ||
66 | |||
67 | None | ||
68 | } | ||
69 | |||
70 | /// Parser for a format-like string. It is more allowing in terms of string contents, | ||
71 | /// as we expect variable placeholders to be filled with expressions. | ||
72 | #[derive(Debug)] | ||
73 | pub(crate) struct FormatStrParser { | ||
74 | input: String, | ||
75 | output: String, | ||
76 | extracted_expressions: Vec<String>, | ||
77 | state: State, | ||
78 | parsed: bool, | ||
79 | } | ||
80 | |||
81 | #[derive(Debug, Clone, Copy, PartialEq)] | ||
82 | enum State { | ||
83 | NotExpr, | ||
84 | MaybeExpr, | ||
85 | Expr, | ||
86 | MaybeIncorrect, | ||
87 | FormatOpts, | ||
88 | } | ||
89 | |||
90 | impl FormatStrParser { | ||
91 | pub fn new(input: String) -> Self { | ||
92 | Self { | ||
93 | input: input.into(), | ||
94 | output: String::new(), | ||
95 | extracted_expressions: Vec::new(), | ||
96 | state: State::NotExpr, | ||
97 | parsed: false, | ||
98 | } | ||
99 | } | ||
100 | |||
101 | pub fn parse(&mut self) -> Result<(), ()> { | ||
102 | let mut current_expr = String::new(); | ||
103 | |||
104 | let mut placeholder_id = 1; | ||
105 | |||
106 | // Count of open braces inside of an expression. | ||
107 | // We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g. | ||
108 | // "{MyStruct { val_a: 0, val_b: 1 }}". | ||
109 | let mut inexpr_open_count = 0; | ||
110 | |||
111 | for chr in self.input.chars() { | ||
112 | match (self.state, chr) { | ||
113 | (State::NotExpr, '{') => { | ||
114 | self.output.push(chr); | ||
115 | self.state = State::MaybeExpr; | ||
116 | } | ||
117 | (State::NotExpr, '}') => { | ||
118 | self.output.push(chr); | ||
119 | self.state = State::MaybeIncorrect; | ||
120 | } | ||
121 | (State::NotExpr, _) => { | ||
122 | self.output.push(chr); | ||
123 | } | ||
124 | (State::MaybeIncorrect, '}') => { | ||
125 | // It's okay, we met "}}". | ||
126 | self.output.push(chr); | ||
127 | self.state = State::NotExpr; | ||
128 | } | ||
129 | (State::MaybeIncorrect, _) => { | ||
130 | // Error in the string. | ||
131 | return Err(()); | ||
132 | } | ||
133 | (State::MaybeExpr, '{') => { | ||
134 | self.output.push(chr); | ||
135 | self.state = State::NotExpr; | ||
136 | } | ||
137 | (State::MaybeExpr, '}') => { | ||
138 | // This is an empty sequence '{}'. Replace it with placeholder. | ||
139 | self.output.push(chr); | ||
140 | self.extracted_expressions.push(format!("${}", placeholder_id)); | ||
141 | placeholder_id += 1; | ||
142 | self.state = State::NotExpr; | ||
143 | } | ||
144 | (State::MaybeExpr, _) => { | ||
145 | current_expr.push(chr); | ||
146 | self.state = State::Expr; | ||
147 | } | ||
148 | (State::Expr, '}') => { | ||
149 | if inexpr_open_count == 0 { | ||
150 | self.output.push(chr); | ||
151 | self.extracted_expressions.push(current_expr.trim().into()); | ||
152 | current_expr = String::new(); | ||
153 | self.state = State::NotExpr; | ||
154 | } else { | ||
155 | // We're closing one brace met before inside of the expression. | ||
156 | current_expr.push(chr); | ||
157 | inexpr_open_count -= 1; | ||
158 | } | ||
159 | } | ||
160 | (State::Expr, ':') => { | ||
161 | if inexpr_open_count == 0 { | ||
162 | // We're outside of braces, thus assume that it's a specifier, like "{Some(value):?}" | ||
163 | self.output.push(chr); | ||
164 | self.extracted_expressions.push(current_expr.trim().into()); | ||
165 | current_expr = String::new(); | ||
166 | self.state = State::FormatOpts; | ||
167 | } else { | ||
168 | // We're inside of braced expression, assume that it's a struct field name/value delimeter. | ||
169 | current_expr.push(chr); | ||
170 | } | ||
171 | } | ||
172 | (State::Expr, '{') => { | ||
173 | current_expr.push(chr); | ||
174 | inexpr_open_count += 1; | ||
175 | } | ||
176 | (State::Expr, _) => { | ||
177 | current_expr.push(chr); | ||
178 | } | ||
179 | (State::FormatOpts, '}') => { | ||
180 | self.output.push(chr); | ||
181 | self.state = State::NotExpr; | ||
182 | } | ||
183 | (State::FormatOpts, _) => { | ||
184 | self.output.push(chr); | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | |||
189 | if self.state != State::NotExpr { | ||
190 | return Err(()); | ||
191 | } | ||
192 | |||
193 | self.parsed = true; | ||
194 | Ok(()) | ||
195 | } | ||
196 | |||
197 | pub fn into_suggestion(&self, macro_name: &str) -> String { | ||
198 | assert!(self.parsed, "Attempt to get a suggestion from not parsed expression"); | ||
199 | |||
200 | let expressions_as_string = self.extracted_expressions.join(", "); | ||
201 | format!(r#"{}("{}", {})"#, macro_name, self.output, expressions_as_string) | ||
202 | } | ||
203 | } | ||
204 | |||
205 | #[cfg(test)] | ||
206 | mod tests { | ||
207 | use super::*; | ||
208 | use expect_test::{expect, Expect}; | ||
209 | |||
210 | fn check(input: &str, expect: &Expect) { | ||
211 | let mut parser = FormatStrParser::new((*input).to_owned()); | ||
212 | let outcome_repr = if parser.parse().is_ok() { | ||
213 | // Parsing should be OK, expected repr is "string; expr_1, expr_2". | ||
214 | if parser.extracted_expressions.is_empty() { | ||
215 | parser.output | ||
216 | } else { | ||
217 | format!("{}; {}", parser.output, parser.extracted_expressions.join(", ")) | ||
218 | } | ||
219 | } else { | ||
220 | // Parsing should fail, expected repr is "-". | ||
221 | "-".to_owned() | ||
222 | }; | ||
223 | |||
224 | expect.assert_eq(&outcome_repr); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
228 | fn format_str_parser() { | ||
229 | let test_vector = &[ | ||
230 | ("no expressions", expect![["no expressions"]]), | ||
231 | ("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]), | ||
232 | ("{expr:?}", expect![["{:?}; expr"]]), | ||
233 | ("{malformed", expect![["-"]]), | ||
234 | ("malformed}", expect![["-"]]), | ||
235 | ("{{correct", expect![["{{correct"]]), | ||
236 | ("correct}}", expect![["correct}}"]]), | ||
237 | ("{correct}}}", expect![["{}}}; correct"]]), | ||
238 | ("{correct}}}}}", expect![["{}}}}}; correct"]]), | ||
239 | ("{incorrect}}", expect![["-"]]), | ||
240 | ("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]), | ||
241 | ("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]), | ||
242 | ( | ||
243 | "{SomeStruct { val_a: 0, val_b: 1 }}", | ||
244 | expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]], | ||
245 | ), | ||
246 | ("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]), | ||
247 | ( | ||
248 | "{SomeStruct { val_a: 0, val_b: 1 }:?}", | ||
249 | expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]], | ||
250 | ), | ||
251 | ("{ 2 + 2 }", expect![["{}; 2 + 2"]]), | ||
252 | ]; | ||
253 | |||
254 | for (input, output) in test_vector { | ||
255 | check(input, output) | ||
256 | } | ||
257 | } | ||
258 | |||
259 | #[test] | ||
260 | fn test_into_suggestion() { | ||
261 | let test_vector = &[ | ||
262 | ("println!", "{}", r#"println!("{}", $1)"#), | ||
263 | ("eprintln!", "{}", r#"eprintln!("{}", $1)"#), | ||
264 | ( | ||
265 | "log::info!", | ||
266 | "{} {expr} {} {2 + 2}", | ||
267 | r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#, | ||
268 | ), | ||
269 | ("format!", "{expr:?}", r#"format!("{:?}", expr)"#), | ||
270 | ]; | ||
271 | |||
272 | for (kind, input, output) in test_vector { | ||
273 | let mut parser = FormatStrParser::new((*input).to_owned()); | ||
274 | parser.parse().expect("Parsing must succeed"); | ||
275 | |||
276 | assert_eq!(&parser.into_suggestion(*kind), output); | ||
277 | } | ||
278 | } | ||
279 | } | ||
diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs new file mode 100644 index 000000000..d9387054d --- /dev/null +++ b/crates/completion/src/completions/qualified_path.rs | |||
@@ -0,0 +1,755 @@ | |||
1 | //! Completion of paths, i.e. `some::prefix::<|>`. | ||
2 | |||
3 | use hir::{Adt, HasVisibility, PathResolution, ScopeDef}; | ||
4 | use rustc_hash::FxHashSet; | ||
5 | use syntax::AstNode; | ||
6 | use test_utils::mark; | ||
7 | |||
8 | use crate::{CompletionContext, Completions}; | ||
9 | |||
10 | pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { | ||
11 | let path = match &ctx.path_qual { | ||
12 | Some(path) => path.clone(), | ||
13 | None => return, | ||
14 | }; | ||
15 | |||
16 | if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() { | ||
17 | return; | ||
18 | } | ||
19 | |||
20 | let context_module = ctx.scope.module(); | ||
21 | |||
22 | let resolution = match ctx.sema.resolve_path(&path) { | ||
23 | Some(res) => res, | ||
24 | None => return, | ||
25 | }; | ||
26 | |||
27 | // Add associated types on type parameters and `Self`. | ||
28 | resolution.assoc_type_shorthand_candidates(ctx.db, |alias| { | ||
29 | acc.add_type_alias(ctx, alias); | ||
30 | None::<()> | ||
31 | }); | ||
32 | |||
33 | match resolution { | ||
34 | PathResolution::Def(hir::ModuleDef::Module(module)) => { | ||
35 | let module_scope = module.scope(ctx.db, context_module); | ||
36 | for (name, def) in module_scope { | ||
37 | if ctx.use_item_syntax.is_some() { | ||
38 | if let ScopeDef::Unknown = def { | ||
39 | if let Some(name_ref) = ctx.name_ref_syntax.as_ref() { | ||
40 | if name_ref.syntax().text() == name.to_string().as_str() { | ||
41 | // for `use self::foo<|>`, don't suggest `foo` as a completion | ||
42 | mark::hit!(dont_complete_current_use); | ||
43 | continue; | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | acc.add_resolution(ctx, name.to_string(), &def); | ||
50 | } | ||
51 | } | ||
52 | PathResolution::Def(def @ hir::ModuleDef::Adt(_)) | ||
53 | | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => { | ||
54 | if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { | ||
55 | for variant in e.variants(ctx.db) { | ||
56 | acc.add_enum_variant(ctx, variant, None); | ||
57 | } | ||
58 | } | ||
59 | let ty = match def { | ||
60 | hir::ModuleDef::Adt(adt) => adt.ty(ctx.db), | ||
61 | hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), | ||
62 | _ => unreachable!(), | ||
63 | }; | ||
64 | |||
65 | // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType. | ||
66 | // (where AssocType is defined on a trait, not an inherent impl) | ||
67 | |||
68 | let krate = ctx.krate; | ||
69 | if let Some(krate) = krate { | ||
70 | let traits_in_scope = ctx.scope.traits_in_scope(); | ||
71 | ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { | ||
72 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
73 | return None; | ||
74 | } | ||
75 | match item { | ||
76 | hir::AssocItem::Function(func) => { | ||
77 | acc.add_function(ctx, func, None); | ||
78 | } | ||
79 | hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), | ||
80 | hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
81 | } | ||
82 | None::<()> | ||
83 | }); | ||
84 | |||
85 | // Iterate assoc types separately | ||
86 | ty.iterate_assoc_items(ctx.db, krate, |item| { | ||
87 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
88 | return None; | ||
89 | } | ||
90 | match item { | ||
91 | hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {} | ||
92 | hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
93 | } | ||
94 | None::<()> | ||
95 | }); | ||
96 | } | ||
97 | } | ||
98 | PathResolution::Def(hir::ModuleDef::Trait(t)) => { | ||
99 | // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`. | ||
100 | for item in t.items(ctx.db) { | ||
101 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
102 | continue; | ||
103 | } | ||
104 | match item { | ||
105 | hir::AssocItem::Function(func) => { | ||
106 | acc.add_function(ctx, func, None); | ||
107 | } | ||
108 | hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), | ||
109 | hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | PathResolution::TypeParam(_) | PathResolution::SelfType(_) => { | ||
114 | if let Some(krate) = ctx.krate { | ||
115 | let ty = match resolution { | ||
116 | PathResolution::TypeParam(param) => param.ty(ctx.db), | ||
117 | PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db), | ||
118 | _ => return, | ||
119 | }; | ||
120 | |||
121 | let traits_in_scope = ctx.scope.traits_in_scope(); | ||
122 | let mut seen = FxHashSet::default(); | ||
123 | ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { | ||
124 | if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { | ||
125 | return None; | ||
126 | } | ||
127 | |||
128 | // We might iterate candidates of a trait multiple times here, so deduplicate | ||
129 | // them. | ||
130 | if seen.insert(item) { | ||
131 | match item { | ||
132 | hir::AssocItem::Function(func) => { | ||
133 | acc.add_function(ctx, func, None); | ||
134 | } | ||
135 | hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), | ||
136 | hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), | ||
137 | } | ||
138 | } | ||
139 | None::<()> | ||
140 | }); | ||
141 | } | ||
142 | } | ||
143 | _ => {} | ||
144 | } | ||
145 | } | ||
146 | |||
147 | #[cfg(test)] | ||
148 | mod tests { | ||
149 | use expect_test::{expect, Expect}; | ||
150 | use test_utils::mark; | ||
151 | |||
152 | use crate::{ | ||
153 | test_utils::{check_edit, completion_list}, | ||
154 | CompletionKind, | ||
155 | }; | ||
156 | |||
157 | fn check(ra_fixture: &str, expect: Expect) { | ||
158 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
159 | expect.assert_eq(&actual); | ||
160 | } | ||
161 | |||
162 | fn check_builtin(ra_fixture: &str, expect: Expect) { | ||
163 | let actual = completion_list(ra_fixture, CompletionKind::BuiltinType); | ||
164 | expect.assert_eq(&actual); | ||
165 | } | ||
166 | |||
167 | #[test] | ||
168 | fn dont_complete_current_use() { | ||
169 | mark::check!(dont_complete_current_use); | ||
170 | check(r#"use self::foo<|>;"#, expect![[""]]); | ||
171 | } | ||
172 | |||
173 | #[test] | ||
174 | fn dont_complete_current_use_in_braces_with_glob() { | ||
175 | check( | ||
176 | r#" | ||
177 | mod foo { pub struct S; } | ||
178 | use self::{foo::*, bar<|>}; | ||
179 | "#, | ||
180 | expect![[r#" | ||
181 | st S | ||
182 | md foo | ||
183 | "#]], | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | #[test] | ||
188 | fn dont_complete_primitive_in_use() { | ||
189 | check_builtin(r#"use self::<|>;"#, expect![[""]]); | ||
190 | } | ||
191 | |||
192 | #[test] | ||
193 | fn dont_complete_primitive_in_module_scope() { | ||
194 | check_builtin(r#"fn foo() { self::<|> }"#, expect![[""]]); | ||
195 | } | ||
196 | |||
197 | #[test] | ||
198 | fn completes_primitives() { | ||
199 | check_builtin( | ||
200 | r#"fn main() { let _: <|> = 92; }"#, | ||
201 | expect![[r#" | ||
202 | bt bool | ||
203 | bt char | ||
204 | bt f32 | ||
205 | bt f64 | ||
206 | bt i128 | ||
207 | bt i16 | ||
208 | bt i32 | ||
209 | bt i64 | ||
210 | bt i8 | ||
211 | bt isize | ||
212 | bt str | ||
213 | bt u128 | ||
214 | bt u16 | ||
215 | bt u32 | ||
216 | bt u64 | ||
217 | bt u8 | ||
218 | bt usize | ||
219 | "#]], | ||
220 | ); | ||
221 | } | ||
222 | |||
223 | #[test] | ||
224 | fn completes_mod_with_same_name_as_function() { | ||
225 | check( | ||
226 | r#" | ||
227 | use self::my::<|>; | ||
228 | |||
229 | mod my { pub struct Bar; } | ||
230 | fn my() {} | ||
231 | "#, | ||
232 | expect![[r#" | ||
233 | st Bar | ||
234 | "#]], | ||
235 | ); | ||
236 | } | ||
237 | |||
238 | #[test] | ||
239 | fn filters_visibility() { | ||
240 | check( | ||
241 | r#" | ||
242 | use self::my::<|>; | ||
243 | |||
244 | mod my { | ||
245 | struct Bar; | ||
246 | pub struct Foo; | ||
247 | pub use Bar as PublicBar; | ||
248 | } | ||
249 | "#, | ||
250 | expect![[r#" | ||
251 | st Foo | ||
252 | st PublicBar | ||
253 | "#]], | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn completes_use_item_starting_with_self() { | ||
259 | check( | ||
260 | r#" | ||
261 | use self::m::<|>; | ||
262 | |||
263 | mod m { pub struct Bar; } | ||
264 | "#, | ||
265 | expect![[r#" | ||
266 | st Bar | ||
267 | "#]], | ||
268 | ); | ||
269 | } | ||
270 | |||
271 | #[test] | ||
272 | fn completes_use_item_starting_with_crate() { | ||
273 | check( | ||
274 | r#" | ||
275 | //- /lib.rs | ||
276 | mod foo; | ||
277 | struct Spam; | ||
278 | //- /foo.rs | ||
279 | use crate::Sp<|> | ||
280 | "#, | ||
281 | expect![[r#" | ||
282 | st Spam | ||
283 | md foo | ||
284 | "#]], | ||
285 | ); | ||
286 | } | ||
287 | |||
288 | #[test] | ||
289 | fn completes_nested_use_tree() { | ||
290 | check( | ||
291 | r#" | ||
292 | //- /lib.rs | ||
293 | mod foo; | ||
294 | struct Spam; | ||
295 | //- /foo.rs | ||
296 | use crate::{Sp<|>}; | ||
297 | "#, | ||
298 | expect![[r#" | ||
299 | st Spam | ||
300 | md foo | ||
301 | "#]], | ||
302 | ); | ||
303 | } | ||
304 | |||
305 | #[test] | ||
306 | fn completes_deeply_nested_use_tree() { | ||
307 | check( | ||
308 | r#" | ||
309 | //- /lib.rs | ||
310 | mod foo; | ||
311 | pub mod bar { | ||
312 | pub mod baz { | ||
313 | pub struct Spam; | ||
314 | } | ||
315 | } | ||
316 | //- /foo.rs | ||
317 | use crate::{bar::{baz::Sp<|>}}; | ||
318 | "#, | ||
319 | expect![[r#" | ||
320 | st Spam | ||
321 | "#]], | ||
322 | ); | ||
323 | } | ||
324 | |||
325 | #[test] | ||
326 | fn completes_enum_variant() { | ||
327 | check( | ||
328 | r#" | ||
329 | enum E { Foo, Bar(i32) } | ||
330 | fn foo() { let _ = E::<|> } | ||
331 | "#, | ||
332 | expect![[r#" | ||
333 | ev Bar(…) (i32) | ||
334 | ev Foo () | ||
335 | "#]], | ||
336 | ); | ||
337 | } | ||
338 | |||
339 | #[test] | ||
340 | fn completes_struct_associated_items() { | ||
341 | check( | ||
342 | r#" | ||
343 | //- /lib.rs | ||
344 | struct S; | ||
345 | |||
346 | impl S { | ||
347 | fn a() {} | ||
348 | fn b(&self) {} | ||
349 | const C: i32 = 42; | ||
350 | type T = i32; | ||
351 | } | ||
352 | |||
353 | fn foo() { let _ = S::<|> } | ||
354 | "#, | ||
355 | expect![[r#" | ||
356 | ct C const C: i32 = 42; | ||
357 | ta T type T = i32; | ||
358 | fn a() fn a() | ||
359 | me b() fn b(&self) | ||
360 | "#]], | ||
361 | ); | ||
362 | } | ||
363 | |||
364 | #[test] | ||
365 | fn associated_item_visibility() { | ||
366 | check( | ||
367 | r#" | ||
368 | struct S; | ||
369 | |||
370 | mod m { | ||
371 | impl super::S { | ||
372 | pub(crate) fn public_method() { } | ||
373 | fn private_method() { } | ||
374 | pub(crate) type PublicType = u32; | ||
375 | type PrivateType = u32; | ||
376 | pub(crate) const PUBLIC_CONST: u32 = 1; | ||
377 | const PRIVATE_CONST: u32 = 1; | ||
378 | } | ||
379 | } | ||
380 | |||
381 | fn foo() { let _ = S::<|> } | ||
382 | "#, | ||
383 | expect![[r#" | ||
384 | ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1; | ||
385 | ta PublicType pub(crate) type PublicType = u32; | ||
386 | fn public_method() pub(crate) fn public_method() | ||
387 | "#]], | ||
388 | ); | ||
389 | } | ||
390 | |||
391 | #[test] | ||
392 | fn completes_enum_associated_method() { | ||
393 | check( | ||
394 | r#" | ||
395 | enum E {}; | ||
396 | impl E { fn m() { } } | ||
397 | |||
398 | fn foo() { let _ = E::<|> } | ||
399 | "#, | ||
400 | expect![[r#" | ||
401 | fn m() fn m() | ||
402 | "#]], | ||
403 | ); | ||
404 | } | ||
405 | |||
406 | #[test] | ||
407 | fn completes_union_associated_method() { | ||
408 | check( | ||
409 | r#" | ||
410 | union U {}; | ||
411 | impl U { fn m() { } } | ||
412 | |||
413 | fn foo() { let _ = U::<|> } | ||
414 | "#, | ||
415 | expect![[r#" | ||
416 | fn m() fn m() | ||
417 | "#]], | ||
418 | ); | ||
419 | } | ||
420 | |||
421 | #[test] | ||
422 | fn completes_use_paths_across_crates() { | ||
423 | check( | ||
424 | r#" | ||
425 | //- /main.rs crate:main deps:foo | ||
426 | use foo::<|>; | ||
427 | |||
428 | //- /foo/lib.rs crate:foo | ||
429 | pub mod bar { pub struct S; } | ||
430 | "#, | ||
431 | expect![[r#" | ||
432 | md bar | ||
433 | "#]], | ||
434 | ); | ||
435 | } | ||
436 | |||
437 | #[test] | ||
438 | fn completes_trait_associated_method_1() { | ||
439 | check( | ||
440 | r#" | ||
441 | trait Trait { fn m(); } | ||
442 | |||
443 | fn foo() { let _ = Trait::<|> } | ||
444 | "#, | ||
445 | expect![[r#" | ||
446 | fn m() fn m() | ||
447 | "#]], | ||
448 | ); | ||
449 | } | ||
450 | |||
451 | #[test] | ||
452 | fn completes_trait_associated_method_2() { | ||
453 | check( | ||
454 | r#" | ||
455 | trait Trait { fn m(); } | ||
456 | |||
457 | struct S; | ||
458 | impl Trait for S {} | ||
459 | |||
460 | fn foo() { let _ = S::<|> } | ||
461 | "#, | ||
462 | expect![[r#" | ||
463 | fn m() fn m() | ||
464 | "#]], | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
469 | fn completes_trait_associated_method_3() { | ||
470 | check( | ||
471 | r#" | ||
472 | trait Trait { fn m(); } | ||
473 | |||
474 | struct S; | ||
475 | impl Trait for S {} | ||
476 | |||
477 | fn foo() { let _ = <S as Trait>::<|> } | ||
478 | "#, | ||
479 | expect![[r#" | ||
480 | fn m() fn m() | ||
481 | "#]], | ||
482 | ); | ||
483 | } | ||
484 | |||
485 | #[test] | ||
486 | fn completes_ty_param_assoc_ty() { | ||
487 | check( | ||
488 | r#" | ||
489 | trait Super { | ||
490 | type Ty; | ||
491 | const CONST: u8; | ||
492 | fn func() {} | ||
493 | fn method(&self) {} | ||
494 | } | ||
495 | |||
496 | trait Sub: Super { | ||
497 | type SubTy; | ||
498 | const C2: (); | ||
499 | fn subfunc() {} | ||
500 | fn submethod(&self) {} | ||
501 | } | ||
502 | |||
503 | fn foo<T: Sub>() { T::<|> } | ||
504 | "#, | ||
505 | expect![[r#" | ||
506 | ct C2 const C2: (); | ||
507 | ct CONST const CONST: u8; | ||
508 | ta SubTy type SubTy; | ||
509 | ta Ty type Ty; | ||
510 | fn func() fn func() | ||
511 | me method() fn method(&self) | ||
512 | fn subfunc() fn subfunc() | ||
513 | me submethod() fn submethod(&self) | ||
514 | "#]], | ||
515 | ); | ||
516 | } | ||
517 | |||
518 | #[test] | ||
519 | fn completes_self_param_assoc_ty() { | ||
520 | check( | ||
521 | r#" | ||
522 | trait Super { | ||
523 | type Ty; | ||
524 | const CONST: u8 = 0; | ||
525 | fn func() {} | ||
526 | fn method(&self) {} | ||
527 | } | ||
528 | |||
529 | trait Sub: Super { | ||
530 | type SubTy; | ||
531 | const C2: () = (); | ||
532 | fn subfunc() {} | ||
533 | fn submethod(&self) {} | ||
534 | } | ||
535 | |||
536 | struct Wrap<T>(T); | ||
537 | impl<T> Super for Wrap<T> {} | ||
538 | impl<T> Sub for Wrap<T> { | ||
539 | fn subfunc() { | ||
540 | // Should be able to assume `Self: Sub + Super` | ||
541 | Self::<|> | ||
542 | } | ||
543 | } | ||
544 | "#, | ||
545 | expect![[r#" | ||
546 | ct C2 const C2: () = (); | ||
547 | ct CONST const CONST: u8 = 0; | ||
548 | ta SubTy type SubTy; | ||
549 | ta Ty type Ty; | ||
550 | fn func() fn func() | ||
551 | me method() fn method(&self) | ||
552 | fn subfunc() fn subfunc() | ||
553 | me submethod() fn submethod(&self) | ||
554 | "#]], | ||
555 | ); | ||
556 | } | ||
557 | |||
558 | #[test] | ||
559 | fn completes_type_alias() { | ||
560 | check( | ||
561 | r#" | ||
562 | struct S; | ||
563 | impl S { fn foo() {} } | ||
564 | type T = S; | ||
565 | impl T { fn bar() {} } | ||
566 | |||
567 | fn main() { T::<|>; } | ||
568 | "#, | ||
569 | expect![[r#" | ||
570 | fn bar() fn bar() | ||
571 | fn foo() fn foo() | ||
572 | "#]], | ||
573 | ); | ||
574 | } | ||
575 | |||
576 | #[test] | ||
577 | fn completes_qualified_macros() { | ||
578 | check( | ||
579 | r#" | ||
580 | #[macro_export] | ||
581 | macro_rules! foo { () => {} } | ||
582 | |||
583 | fn main() { let _ = crate::<|> } | ||
584 | "#, | ||
585 | expect![[r##" | ||
586 | ma foo!(…) #[macro_export] | ||
587 | macro_rules! foo | ||
588 | fn main() fn main() | ||
589 | "##]], | ||
590 | ); | ||
591 | } | ||
592 | |||
593 | #[test] | ||
594 | fn test_super_super_completion() { | ||
595 | check( | ||
596 | r#" | ||
597 | mod a { | ||
598 | const A: usize = 0; | ||
599 | mod b { | ||
600 | const B: usize = 0; | ||
601 | mod c { use super::super::<|> } | ||
602 | } | ||
603 | } | ||
604 | "#, | ||
605 | expect![[r#" | ||
606 | ct A | ||
607 | md b | ||
608 | "#]], | ||
609 | ); | ||
610 | } | ||
611 | |||
612 | #[test] | ||
613 | fn completes_reexported_items_under_correct_name() { | ||
614 | check( | ||
615 | r#" | ||
616 | fn foo() { self::m::<|> } | ||
617 | |||
618 | mod m { | ||
619 | pub use super::p::wrong_fn as right_fn; | ||
620 | pub use super::p::WRONG_CONST as RIGHT_CONST; | ||
621 | pub use super::p::WrongType as RightType; | ||
622 | } | ||
623 | mod p { | ||
624 | fn wrong_fn() {} | ||
625 | const WRONG_CONST: u32 = 1; | ||
626 | struct WrongType {}; | ||
627 | } | ||
628 | "#, | ||
629 | expect![[r#" | ||
630 | ct RIGHT_CONST | ||
631 | st RightType | ||
632 | fn right_fn() fn wrong_fn() | ||
633 | "#]], | ||
634 | ); | ||
635 | |||
636 | check_edit( | ||
637 | "RightType", | ||
638 | r#" | ||
639 | fn foo() { self::m::<|> } | ||
640 | |||
641 | mod m { | ||
642 | pub use super::p::wrong_fn as right_fn; | ||
643 | pub use super::p::WRONG_CONST as RIGHT_CONST; | ||
644 | pub use super::p::WrongType as RightType; | ||
645 | } | ||
646 | mod p { | ||
647 | fn wrong_fn() {} | ||
648 | const WRONG_CONST: u32 = 1; | ||
649 | struct WrongType {}; | ||
650 | } | ||
651 | "#, | ||
652 | r#" | ||
653 | fn foo() { self::m::RightType } | ||
654 | |||
655 | mod m { | ||
656 | pub use super::p::wrong_fn as right_fn; | ||
657 | pub use super::p::WRONG_CONST as RIGHT_CONST; | ||
658 | pub use super::p::WrongType as RightType; | ||
659 | } | ||
660 | mod p { | ||
661 | fn wrong_fn() {} | ||
662 | const WRONG_CONST: u32 = 1; | ||
663 | struct WrongType {}; | ||
664 | } | ||
665 | "#, | ||
666 | ); | ||
667 | } | ||
668 | |||
669 | #[test] | ||
670 | fn completes_in_simple_macro_call() { | ||
671 | check( | ||
672 | r#" | ||
673 | macro_rules! m { ($e:expr) => { $e } } | ||
674 | fn main() { m!(self::f<|>); } | ||
675 | fn foo() {} | ||
676 | "#, | ||
677 | expect![[r#" | ||
678 | fn foo() fn foo() | ||
679 | fn main() fn main() | ||
680 | "#]], | ||
681 | ); | ||
682 | } | ||
683 | |||
684 | #[test] | ||
685 | fn function_mod_share_name() { | ||
686 | check( | ||
687 | r#" | ||
688 | fn foo() { self::m::<|> } | ||
689 | |||
690 | mod m { | ||
691 | pub mod z {} | ||
692 | pub fn z() {} | ||
693 | } | ||
694 | "#, | ||
695 | expect![[r#" | ||
696 | md z | ||
697 | fn z() pub fn z() | ||
698 | "#]], | ||
699 | ); | ||
700 | } | ||
701 | |||
702 | #[test] | ||
703 | fn completes_hashmap_new() { | ||
704 | check( | ||
705 | r#" | ||
706 | struct RandomState; | ||
707 | struct HashMap<K, V, S = RandomState> {} | ||
708 | |||
709 | impl<K, V> HashMap<K, V, RandomState> { | ||
710 | pub fn new() -> HashMap<K, V, RandomState> { } | ||
711 | } | ||
712 | fn foo() { | ||
713 | HashMap::<|> | ||
714 | } | ||
715 | "#, | ||
716 | expect![[r#" | ||
717 | fn new() pub fn new() -> HashMap<K, V, RandomState> | ||
718 | "#]], | ||
719 | ); | ||
720 | } | ||
721 | |||
722 | #[test] | ||
723 | fn dont_complete_attr() { | ||
724 | check( | ||
725 | r#" | ||
726 | mod foo { pub struct Foo; } | ||
727 | #[foo::<|>] | ||
728 | fn f() {} | ||
729 | "#, | ||
730 | expect![[""]], | ||
731 | ); | ||
732 | } | ||
733 | |||
734 | #[test] | ||
735 | fn completes_function() { | ||
736 | check( | ||
737 | r#" | ||
738 | fn foo( | ||
739 | a: i32, | ||
740 | b: i32 | ||
741 | ) { | ||
742 | |||
743 | } | ||
744 | |||
745 | fn main() { | ||
746 | fo<|> | ||
747 | } | ||
748 | "#, | ||
749 | expect![[r#" | ||
750 | fn foo(…) fn foo(a: i32, b: i32) | ||
751 | fn main() fn main() | ||
752 | "#]], | ||
753 | ); | ||
754 | } | ||
755 | } | ||
diff --git a/crates/completion/src/completions/record.rs b/crates/completion/src/completions/record.rs new file mode 100644 index 000000000..0f611084b --- /dev/null +++ b/crates/completion/src/completions/record.rs | |||
@@ -0,0 +1,226 @@ | |||
1 | //! Complete fields in record literals and patterns. | ||
2 | use crate::{CompletionContext, Completions}; | ||
3 | |||
4 | pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | ||
5 | let missing_fields = match (ctx.record_pat_syntax.as_ref(), ctx.record_lit_syntax.as_ref()) { | ||
6 | (None, None) => return None, | ||
7 | (Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"), | ||
8 | (Some(record_pat), _) => ctx.sema.record_pattern_missing_fields(record_pat), | ||
9 | (_, Some(record_lit)) => ctx.sema.record_literal_missing_fields(record_lit), | ||
10 | }; | ||
11 | |||
12 | for (field, ty) in missing_fields { | ||
13 | acc.add_field(ctx, field, &ty) | ||
14 | } | ||
15 | |||
16 | Some(()) | ||
17 | } | ||
18 | |||
19 | #[cfg(test)] | ||
20 | mod tests { | ||
21 | use expect_test::{expect, Expect}; | ||
22 | |||
23 | use crate::{test_utils::completion_list, CompletionKind}; | ||
24 | |||
25 | fn check(ra_fixture: &str, expect: Expect) { | ||
26 | let actual = completion_list(ra_fixture, CompletionKind::Reference); | ||
27 | expect.assert_eq(&actual); | ||
28 | } | ||
29 | |||
30 | #[test] | ||
31 | fn test_record_pattern_field() { | ||
32 | check( | ||
33 | r#" | ||
34 | struct S { foo: u32 } | ||
35 | |||
36 | fn process(f: S) { | ||
37 | match f { | ||
38 | S { f<|>: 92 } => (), | ||
39 | } | ||
40 | } | ||
41 | "#, | ||
42 | expect![[r#" | ||
43 | fd foo u32 | ||
44 | "#]], | ||
45 | ); | ||
46 | } | ||
47 | |||
48 | #[test] | ||
49 | fn test_record_pattern_enum_variant() { | ||
50 | check( | ||
51 | r#" | ||
52 | enum E { S { foo: u32, bar: () } } | ||
53 | |||
54 | fn process(e: E) { | ||
55 | match e { | ||
56 | E::S { <|> } => (), | ||
57 | } | ||
58 | } | ||
59 | "#, | ||
60 | expect![[r#" | ||
61 | fd bar () | ||
62 | fd foo u32 | ||
63 | "#]], | ||
64 | ); | ||
65 | } | ||
66 | |||
67 | #[test] | ||
68 | fn test_record_pattern_field_in_simple_macro() { | ||
69 | check( | ||
70 | r" | ||
71 | macro_rules! m { ($e:expr) => { $e } } | ||
72 | struct S { foo: u32 } | ||
73 | |||
74 | fn process(f: S) { | ||
75 | m!(match f { | ||
76 | S { f<|>: 92 } => (), | ||
77 | }) | ||
78 | } | ||
79 | ", | ||
80 | expect![[r#" | ||
81 | fd foo u32 | ||
82 | "#]], | ||
83 | ); | ||
84 | } | ||
85 | |||
86 | #[test] | ||
87 | fn only_missing_fields_are_completed_in_destruct_pats() { | ||
88 | check( | ||
89 | r#" | ||
90 | struct S { | ||
91 | foo1: u32, foo2: u32, | ||
92 | bar: u32, baz: u32, | ||
93 | } | ||
94 | |||
95 | fn main() { | ||
96 | let s = S { | ||
97 | foo1: 1, foo2: 2, | ||
98 | bar: 3, baz: 4, | ||
99 | }; | ||
100 | if let S { foo1, foo2: a, <|> } = s {} | ||
101 | } | ||
102 | "#, | ||
103 | expect![[r#" | ||
104 | fd bar u32 | ||
105 | fd baz u32 | ||
106 | "#]], | ||
107 | ); | ||
108 | } | ||
109 | |||
110 | #[test] | ||
111 | fn test_record_literal_field() { | ||
112 | check( | ||
113 | r#" | ||
114 | struct A { the_field: u32 } | ||
115 | fn foo() { | ||
116 | A { the<|> } | ||
117 | } | ||
118 | "#, | ||
119 | expect![[r#" | ||
120 | fd the_field u32 | ||
121 | "#]], | ||
122 | ); | ||
123 | } | ||
124 | |||
125 | #[test] | ||
126 | fn test_record_literal_enum_variant() { | ||
127 | check( | ||
128 | r#" | ||
129 | enum E { A { a: u32 } } | ||
130 | fn foo() { | ||
131 | let _ = E::A { <|> } | ||
132 | } | ||
133 | "#, | ||
134 | expect![[r#" | ||
135 | fd a u32 | ||
136 | "#]], | ||
137 | ); | ||
138 | } | ||
139 | |||
140 | #[test] | ||
141 | fn test_record_literal_two_structs() { | ||
142 | check( | ||
143 | r#" | ||
144 | struct A { a: u32 } | ||
145 | struct B { b: u32 } | ||
146 | |||
147 | fn foo() { | ||
148 | let _: A = B { <|> } | ||
149 | } | ||
150 | "#, | ||
151 | expect![[r#" | ||
152 | fd b u32 | ||
153 | "#]], | ||
154 | ); | ||
155 | } | ||
156 | |||
157 | #[test] | ||
158 | fn test_record_literal_generic_struct() { | ||
159 | check( | ||
160 | r#" | ||
161 | struct A<T> { a: T } | ||
162 | |||
163 | fn foo() { | ||
164 | let _: A<u32> = A { <|> } | ||
165 | } | ||
166 | "#, | ||
167 | expect![[r#" | ||
168 | fd a u32 | ||
169 | "#]], | ||
170 | ); | ||
171 | } | ||
172 | |||
173 | #[test] | ||
174 | fn test_record_literal_field_in_simple_macro() { | ||
175 | check( | ||
176 | r#" | ||
177 | macro_rules! m { ($e:expr) => { $e } } | ||
178 | struct A { the_field: u32 } | ||
179 | fn foo() { | ||
180 | m!(A { the<|> }) | ||
181 | } | ||
182 | "#, | ||
183 | expect![[r#" | ||
184 | fd the_field u32 | ||
185 | "#]], | ||
186 | ); | ||
187 | } | ||
188 | |||
189 | #[test] | ||
190 | fn only_missing_fields_are_completed() { | ||
191 | check( | ||
192 | r#" | ||
193 | struct S { | ||
194 | foo1: u32, foo2: u32, | ||
195 | bar: u32, baz: u32, | ||
196 | } | ||
197 | |||
198 | fn main() { | ||
199 | let foo1 = 1; | ||
200 | let s = S { foo1, foo2: 5, <|> } | ||
201 | } | ||
202 | "#, | ||
203 | expect![[r#" | ||
204 | fd bar u32 | ||
205 | fd baz u32 | ||
206 | "#]], | ||
207 | ); | ||
208 | } | ||
209 | |||
210 | #[test] | ||
211 | fn completes_functional_update() { | ||
212 | check( | ||
213 | r#" | ||
214 | struct S { foo1: u32, foo2: u32 } | ||
215 | |||
216 | fn main() { | ||
217 | let foo1 = 1; | ||
218 | let s = S { foo1, <|> .. loop {} } | ||
219 | } | ||
220 | "#, | ||
221 | expect![[r#" | ||
222 | fd foo2 u32 | ||
223 | "#]], | ||
224 | ); | ||
225 | } | ||
226 | } | ||
diff --git a/crates/completion/src/completions/snippet.rs b/crates/completion/src/completions/snippet.rs new file mode 100644 index 000000000..6f0c00078 --- /dev/null +++ b/crates/completion/src/completions/snippet.rs | |||
@@ -0,0 +1,114 @@ | |||
1 | //! This file provides snippet completions, like `pd` => `eprintln!(...)`. | ||
2 | |||
3 | use crate::{ | ||
4 | config::SnippetCap, item::Builder, CompletionContext, CompletionItem, CompletionItemKind, | ||
5 | CompletionKind, Completions, | ||
6 | }; | ||
7 | |||
8 | fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder { | ||
9 | CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label) | ||
10 | .insert_snippet(cap, snippet) | ||
11 | .kind(CompletionItemKind::Snippet) | ||
12 | } | ||
13 | |||
14 | pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { | ||
15 | if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) { | ||
16 | return; | ||
17 | } | ||
18 | let cap = match ctx.config.snippet_cap { | ||
19 | Some(it) => it, | ||
20 | None => return, | ||
21 | }; | ||
22 | |||
23 | snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc); | ||
24 | snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc); | ||
25 | } | ||
26 | |||
27 | pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { | ||
28 | if !ctx.is_new_item { | ||
29 | return; | ||
30 | } | ||
31 | let cap = match ctx.config.snippet_cap { | ||
32 | Some(it) => it, | ||
33 | None => return, | ||
34 | }; | ||
35 | |||
36 | snippet( | ||
37 | ctx, | ||
38 | cap, | ||
39 | "tmod (Test module)", | ||
40 | "\ | ||
41 | #[cfg(test)] | ||
42 | mod tests { | ||
43 | use super::*; | ||
44 | |||
45 | #[test] | ||
46 | fn ${1:test_name}() { | ||
47 | $0 | ||
48 | } | ||
49 | }", | ||
50 | ) | ||
51 | .lookup_by("tmod") | ||
52 | .add_to(acc); | ||
53 | |||
54 | snippet( | ||
55 | ctx, | ||
56 | cap, | ||
57 | "tfn (Test function)", | ||
58 | "\ | ||
59 | #[test] | ||
60 | fn ${1:feature}() { | ||
61 | $0 | ||
62 | }", | ||
63 | ) | ||
64 | .lookup_by("tfn") | ||
65 | .add_to(acc); | ||
66 | |||
67 | snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc); | ||
68 | } | ||
69 | |||
70 | #[cfg(test)] | ||
71 | mod tests { | ||
72 | use expect_test::{expect, Expect}; | ||
73 | |||
74 | use crate::{test_utils::completion_list, CompletionKind}; | ||
75 | |||
76 | fn check(ra_fixture: &str, expect: Expect) { | ||
77 | let actual = completion_list(ra_fixture, CompletionKind::Snippet); | ||
78 | expect.assert_eq(&actual) | ||
79 | } | ||
80 | |||
81 | #[test] | ||
82 | fn completes_snippets_in_expressions() { | ||
83 | check( | ||
84 | r#"fn foo(x: i32) { <|> }"#, | ||
85 | expect![[r#" | ||
86 | sn pd | ||
87 | sn ppd | ||
88 | "#]], | ||
89 | ); | ||
90 | } | ||
91 | |||
92 | #[test] | ||
93 | fn should_not_complete_snippets_in_path() { | ||
94 | check(r#"fn foo(x: i32) { ::foo<|> }"#, expect![[""]]); | ||
95 | check(r#"fn foo(x: i32) { ::<|> }"#, expect![[""]]); | ||
96 | } | ||
97 | |||
98 | #[test] | ||
99 | fn completes_snippets_in_items() { | ||
100 | check( | ||
101 | r#" | ||
102 | #[cfg(test)] | ||
103 | mod tests { | ||
104 | <|> | ||
105 | } | ||
106 | "#, | ||
107 | expect![[r#" | ||
108 | sn macro_rules | ||
109 | sn tfn (Test function) | ||
110 | sn tmod (Test module) | ||
111 | "#]], | ||
112 | ) | ||
113 | } | ||
114 | } | ||
diff --git a/crates/completion/src/completions/trait_impl.rs b/crates/completion/src/completions/trait_impl.rs new file mode 100644 index 000000000..a14be9c73 --- /dev/null +++ b/crates/completion/src/completions/trait_impl.rs | |||
@@ -0,0 +1,736 @@ | |||
1 | //! Completion for associated items in a trait implementation. | ||
2 | //! | ||
3 | //! This module adds the completion items related to implementing associated | ||
4 | //! items within a `impl Trait for Struct` block. The current context node | ||
5 | //! must be within either a `FN`, `TYPE_ALIAS`, or `CONST` node | ||
6 | //! and an direct child of an `IMPL`. | ||
7 | //! | ||
8 | //! # Examples | ||
9 | //! | ||
10 | //! Considering the following trait `impl`: | ||
11 | //! | ||
12 | //! ```ignore | ||
13 | //! trait SomeTrait { | ||
14 | //! fn foo(); | ||
15 | //! } | ||
16 | //! | ||
17 | //! impl SomeTrait for () { | ||
18 | //! fn f<|> | ||
19 | //! } | ||
20 | //! ``` | ||
21 | //! | ||
22 | //! may result in the completion of the following method: | ||
23 | //! | ||
24 | //! ```ignore | ||
25 | //! # trait SomeTrait { | ||
26 | //! # fn foo(); | ||
27 | //! # } | ||
28 | //! | ||
29 | //! impl SomeTrait for () { | ||
30 | //! fn foo() {}<|> | ||
31 | //! } | ||
32 | //! ``` | ||
33 | |||
34 | use hir::{self, HasAttrs, HasSource}; | ||
35 | use ide_db::traits::get_missing_assoc_items; | ||
36 | use syntax::{ | ||
37 | ast::{self, edit, Impl}, | ||
38 | display::function_declaration, | ||
39 | AstNode, SyntaxKind, SyntaxNode, TextRange, T, | ||
40 | }; | ||
41 | use text_edit::TextEdit; | ||
42 | |||
43 | use crate::{ | ||
44 | CompletionContext, | ||
45 | CompletionItem, | ||
46 | CompletionItemKind, | ||
47 | CompletionKind, | ||
48 | Completions, | ||
49 | // display::function_declaration, | ||
50 | }; | ||
51 | |||
52 | #[derive(Debug, PartialEq, Eq)] | ||
53 | enum ImplCompletionKind { | ||
54 | All, | ||
55 | Fn, | ||
56 | TypeAlias, | ||
57 | Const, | ||
58 | } | ||
59 | |||
60 | pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { | ||
61 | if let Some((kind, trigger, impl_def)) = completion_match(ctx) { | ||
62 | get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item { | ||
63 | hir::AssocItem::Function(fn_item) | ||
64 | if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn => | ||
65 | { | ||
66 | add_function_impl(&trigger, acc, ctx, fn_item) | ||
67 | } | ||
68 | hir::AssocItem::TypeAlias(type_item) | ||
69 | if kind == ImplCompletionKind::All || kind == ImplCompletionKind::TypeAlias => | ||
70 | { | ||
71 | add_type_alias_impl(&trigger, acc, ctx, type_item) | ||
72 | } | ||
73 | hir::AssocItem::Const(const_item) | ||
74 | if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Const => | ||
75 | { | ||
76 | add_const_impl(&trigger, acc, ctx, const_item) | ||
77 | } | ||
78 | _ => {} | ||
79 | }); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> { | ||
84 | let mut token = ctx.token.clone(); | ||
85 | // For keywork without name like `impl .. { fn <|> }`, the current position is inside | ||
86 | // the whitespace token, which is outside `FN` syntax node. | ||
87 | // We need to follow the previous token in this case. | ||
88 | if token.kind() == SyntaxKind::WHITESPACE { | ||
89 | token = token.prev_token()?; | ||
90 | } | ||
91 | |||
92 | let impl_item_offset = match token.kind() { | ||
93 | // `impl .. { const <|> }` | ||
94 | // ERROR 0 | ||
95 | // CONST_KW <- * | ||
96 | SyntaxKind::CONST_KW => 0, | ||
97 | // `impl .. { fn/type <|> }` | ||
98 | // FN/TYPE_ALIAS 0 | ||
99 | // FN_KW <- * | ||
100 | SyntaxKind::FN_KW | SyntaxKind::TYPE_KW => 0, | ||
101 | // `impl .. { fn/type/const foo<|> }` | ||
102 | // FN/TYPE_ALIAS/CONST 1 | ||
103 | // NAME 0 | ||
104 | // IDENT <- * | ||
105 | SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME => 1, | ||
106 | // `impl .. { foo<|> }` | ||
107 | // MACRO_CALL 3 | ||
108 | // PATH 2 | ||
109 | // PATH_SEGMENT 1 | ||
110 | // NAME_REF 0 | ||
111 | // IDENT <- * | ||
112 | SyntaxKind::IDENT if token.parent().kind() == SyntaxKind::NAME_REF => 3, | ||
113 | _ => return None, | ||
114 | }; | ||
115 | |||
116 | let impl_item = token.ancestors().nth(impl_item_offset)?; | ||
117 | // Must directly belong to an impl block. | ||
118 | // IMPL | ||
119 | // ASSOC_ITEM_LIST | ||
120 | // <item> | ||
121 | let impl_def = ast::Impl::cast(impl_item.parent()?.parent()?)?; | ||
122 | let kind = match impl_item.kind() { | ||
123 | // `impl ... { const <|> fn/type/const }` | ||
124 | _ if token.kind() == SyntaxKind::CONST_KW => ImplCompletionKind::Const, | ||
125 | SyntaxKind::CONST | SyntaxKind::ERROR => ImplCompletionKind::Const, | ||
126 | SyntaxKind::TYPE_ALIAS => ImplCompletionKind::TypeAlias, | ||
127 | SyntaxKind::FN => ImplCompletionKind::Fn, | ||
128 | SyntaxKind::MACRO_CALL => ImplCompletionKind::All, | ||
129 | _ => return None, | ||
130 | }; | ||
131 | Some((kind, impl_item, impl_def)) | ||
132 | } | ||
133 | |||
134 | fn add_function_impl( | ||
135 | fn_def_node: &SyntaxNode, | ||
136 | acc: &mut Completions, | ||
137 | ctx: &CompletionContext, | ||
138 | func: hir::Function, | ||
139 | ) { | ||
140 | let fn_name = func.name(ctx.db).to_string(); | ||
141 | |||
142 | let label = if func.params(ctx.db).is_empty() { | ||
143 | format!("fn {}()", fn_name) | ||
144 | } else { | ||
145 | format!("fn {}(..)", fn_name) | ||
146 | }; | ||
147 | |||
148 | let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) | ||
149 | .lookup_by(fn_name) | ||
150 | .set_documentation(func.docs(ctx.db)); | ||
151 | |||
152 | let completion_kind = if func.self_param(ctx.db).is_some() { | ||
153 | CompletionItemKind::Method | ||
154 | } else { | ||
155 | CompletionItemKind::Function | ||
156 | }; | ||
157 | let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); | ||
158 | |||
159 | let function_decl = function_declaration(&func.source(ctx.db).value); | ||
160 | match ctx.config.snippet_cap { | ||
161 | Some(cap) => { | ||
162 | let snippet = format!("{} {{\n $0\n}}", function_decl); | ||
163 | builder.snippet_edit(cap, TextEdit::replace(range, snippet)) | ||
164 | } | ||
165 | None => { | ||
166 | let header = format!("{} {{", function_decl); | ||
167 | builder.text_edit(TextEdit::replace(range, header)) | ||
168 | } | ||
169 | } | ||
170 | .kind(completion_kind) | ||
171 | .add_to(acc); | ||
172 | } | ||
173 | |||
174 | fn add_type_alias_impl( | ||
175 | type_def_node: &SyntaxNode, | ||
176 | acc: &mut Completions, | ||
177 | ctx: &CompletionContext, | ||
178 | type_alias: hir::TypeAlias, | ||
179 | ) { | ||
180 | let alias_name = type_alias.name(ctx.db).to_string(); | ||
181 | |||
182 | let snippet = format!("type {} = ", alias_name); | ||
183 | |||
184 | let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end()); | ||
185 | |||
186 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) | ||
187 | .text_edit(TextEdit::replace(range, snippet)) | ||
188 | .lookup_by(alias_name) | ||
189 | .kind(CompletionItemKind::TypeAlias) | ||
190 | .set_documentation(type_alias.docs(ctx.db)) | ||
191 | .add_to(acc); | ||
192 | } | ||
193 | |||
194 | fn add_const_impl( | ||
195 | const_def_node: &SyntaxNode, | ||
196 | acc: &mut Completions, | ||
197 | ctx: &CompletionContext, | ||
198 | const_: hir::Const, | ||
199 | ) { | ||
200 | let const_name = const_.name(ctx.db).map(|n| n.to_string()); | ||
201 | |||
202 | if let Some(const_name) = const_name { | ||
203 | let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); | ||
204 | |||
205 | let range = TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); | ||
206 | |||
207 | CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) | ||
208 | .text_edit(TextEdit::replace(range, snippet)) | ||
209 | .lookup_by(const_name) | ||
210 | .kind(CompletionItemKind::Const) | ||
211 | .set_documentation(const_.docs(ctx.db)) | ||
212 | .add_to(acc); | ||
213 | } | ||
214 | } | ||
215 | |||
216 | fn make_const_compl_syntax(const_: &ast::Const) -> String { | ||
217 | let const_ = edit::remove_attrs_and_docs(const_); | ||
218 | |||
219 | let const_start = const_.syntax().text_range().start(); | ||
220 | let const_end = const_.syntax().text_range().end(); | ||
221 | |||
222 | let start = | ||
223 | const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start()); | ||
224 | |||
225 | let end = const_ | ||
226 | .syntax() | ||
227 | .children_with_tokens() | ||
228 | .find(|s| s.kind() == T![;] || s.kind() == T![=]) | ||
229 | .map_or(const_end, |f| f.text_range().start()); | ||
230 | |||
231 | let len = end - start; | ||
232 | let range = TextRange::new(0.into(), len); | ||
233 | |||
234 | let syntax = const_.syntax().text().slice(range).to_string(); | ||
235 | |||
236 | format!("{} = ", syntax.trim_end()) | ||
237 | } | ||
238 | |||
239 | #[cfg(test)] | ||
240 | mod tests { | ||
241 | use expect_test::{expect, Expect}; | ||
242 | |||
243 | use crate::{ | ||
244 | test_utils::{check_edit, completion_list}, | ||
245 | CompletionKind, | ||
246 | }; | ||
247 | |||
248 | fn check(ra_fixture: &str, expect: Expect) { | ||
249 | let actual = completion_list(ra_fixture, CompletionKind::Magic); | ||
250 | expect.assert_eq(&actual) | ||
251 | } | ||
252 | |||
253 | #[test] | ||
254 | fn name_ref_function_type_const() { | ||
255 | check( | ||
256 | r#" | ||
257 | trait Test { | ||
258 | type TestType; | ||
259 | const TEST_CONST: u16; | ||
260 | fn test(); | ||
261 | } | ||
262 | struct T; | ||
263 | |||
264 | impl Test for T { | ||
265 | t<|> | ||
266 | } | ||
267 | "#, | ||
268 | expect![[" | ||
269 | ct const TEST_CONST: u16 = \n\ | ||
270 | fn fn test() | ||
271 | ta type TestType = \n\ | ||
272 | "]], | ||
273 | ); | ||
274 | } | ||
275 | |||
276 | #[test] | ||
277 | fn no_completion_inside_fn() { | ||
278 | check( | ||
279 | r" | ||
280 | trait Test { fn test(); fn test2(); } | ||
281 | struct T; | ||
282 | |||
283 | impl Test for T { | ||
284 | fn test() { | ||
285 | t<|> | ||
286 | } | ||
287 | } | ||
288 | ", | ||
289 | expect![[""]], | ||
290 | ); | ||
291 | |||
292 | check( | ||
293 | r" | ||
294 | trait Test { fn test(); fn test2(); } | ||
295 | struct T; | ||
296 | |||
297 | impl Test for T { | ||
298 | fn test() { | ||
299 | fn t<|> | ||
300 | } | ||
301 | } | ||
302 | ", | ||
303 | expect![[""]], | ||
304 | ); | ||
305 | |||
306 | check( | ||
307 | r" | ||
308 | trait Test { fn test(); fn test2(); } | ||
309 | struct T; | ||
310 | |||
311 | impl Test for T { | ||
312 | fn test() { | ||
313 | fn <|> | ||
314 | } | ||
315 | } | ||
316 | ", | ||
317 | expect![[""]], | ||
318 | ); | ||
319 | |||
320 | // https://github.com/rust-analyzer/rust-analyzer/pull/5976#issuecomment-692332191 | ||
321 | check( | ||
322 | r" | ||
323 | trait Test { fn test(); fn test2(); } | ||
324 | struct T; | ||
325 | |||
326 | impl Test for T { | ||
327 | fn test() { | ||
328 | foo.<|> | ||
329 | } | ||
330 | } | ||
331 | ", | ||
332 | expect![[""]], | ||
333 | ); | ||
334 | |||
335 | check( | ||
336 | r" | ||
337 | trait Test { fn test(_: i32); fn test2(); } | ||
338 | struct T; | ||
339 | |||
340 | impl Test for T { | ||
341 | fn test(t<|>) | ||
342 | } | ||
343 | ", | ||
344 | expect![[""]], | ||
345 | ); | ||
346 | |||
347 | check( | ||
348 | r" | ||
349 | trait Test { fn test(_: fn()); fn test2(); } | ||
350 | struct T; | ||
351 | |||
352 | impl Test for T { | ||
353 | fn test(f: fn <|>) | ||
354 | } | ||
355 | ", | ||
356 | expect![[""]], | ||
357 | ); | ||
358 | } | ||
359 | |||
360 | #[test] | ||
361 | fn no_completion_inside_const() { | ||
362 | check( | ||
363 | r" | ||
364 | trait Test { const TEST: fn(); const TEST2: u32; type Test; fn test(); } | ||
365 | struct T; | ||
366 | |||
367 | impl Test for T { | ||
368 | const TEST: fn <|> | ||
369 | } | ||
370 | ", | ||
371 | expect![[""]], | ||
372 | ); | ||
373 | |||
374 | check( | ||
375 | r" | ||
376 | trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); } | ||
377 | struct T; | ||
378 | |||
379 | impl Test for T { | ||
380 | const TEST: T<|> | ||
381 | } | ||
382 | ", | ||
383 | expect![[""]], | ||
384 | ); | ||
385 | |||
386 | check( | ||
387 | r" | ||
388 | trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); } | ||
389 | struct T; | ||
390 | |||
391 | impl Test for T { | ||
392 | const TEST: u32 = f<|> | ||
393 | } | ||
394 | ", | ||
395 | expect![[""]], | ||
396 | ); | ||
397 | |||
398 | check( | ||
399 | r" | ||
400 | trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); } | ||
401 | struct T; | ||
402 | |||
403 | impl Test for T { | ||
404 | const TEST: u32 = { | ||
405 | t<|> | ||
406 | }; | ||
407 | } | ||
408 | ", | ||
409 | expect![[""]], | ||
410 | ); | ||
411 | |||
412 | check( | ||
413 | r" | ||
414 | trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); } | ||
415 | struct T; | ||
416 | |||
417 | impl Test for T { | ||
418 | const TEST: u32 = { | ||
419 | fn <|> | ||
420 | }; | ||
421 | } | ||
422 | ", | ||
423 | expect![[""]], | ||
424 | ); | ||
425 | |||
426 | check( | ||
427 | r" | ||
428 | trait Test { const TEST: u32; const TEST2: u32; type Test; fn test(); } | ||
429 | struct T; | ||
430 | |||
431 | impl Test for T { | ||
432 | const TEST: u32 = { | ||
433 | fn t<|> | ||
434 | }; | ||
435 | } | ||
436 | ", | ||
437 | expect![[""]], | ||
438 | ); | ||
439 | } | ||
440 | |||
441 | #[test] | ||
442 | fn no_completion_inside_type() { | ||
443 | check( | ||
444 | r" | ||
445 | trait Test { type Test; type Test2; fn test(); } | ||
446 | struct T; | ||
447 | |||
448 | impl Test for T { | ||
449 | type Test = T<|>; | ||
450 | } | ||
451 | ", | ||
452 | expect![[""]], | ||
453 | ); | ||
454 | |||
455 | check( | ||
456 | r" | ||
457 | trait Test { type Test; type Test2; fn test(); } | ||
458 | struct T; | ||
459 | |||
460 | impl Test for T { | ||
461 | type Test = fn <|>; | ||
462 | } | ||
463 | ", | ||
464 | expect![[""]], | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
469 | fn name_ref_single_function() { | ||
470 | check_edit( | ||
471 | "test", | ||
472 | r#" | ||
473 | trait Test { | ||
474 | fn test(); | ||
475 | } | ||
476 | struct T; | ||
477 | |||
478 | impl Test for T { | ||
479 | t<|> | ||
480 | } | ||
481 | "#, | ||
482 | r#" | ||
483 | trait Test { | ||
484 | fn test(); | ||
485 | } | ||
486 | struct T; | ||
487 | |||
488 | impl Test for T { | ||
489 | fn test() { | ||
490 | $0 | ||
491 | } | ||
492 | } | ||
493 | "#, | ||
494 | ); | ||
495 | } | ||
496 | |||
497 | #[test] | ||
498 | fn single_function() { | ||
499 | check_edit( | ||
500 | "test", | ||
501 | r#" | ||
502 | trait Test { | ||
503 | fn test(); | ||
504 | } | ||
505 | struct T; | ||
506 | |||
507 | impl Test for T { | ||
508 | fn t<|> | ||
509 | } | ||
510 | "#, | ||
511 | r#" | ||
512 | trait Test { | ||
513 | fn test(); | ||
514 | } | ||
515 | struct T; | ||
516 | |||
517 | impl Test for T { | ||
518 | fn test() { | ||
519 | $0 | ||
520 | } | ||
521 | } | ||
522 | "#, | ||
523 | ); | ||
524 | } | ||
525 | |||
526 | #[test] | ||
527 | fn hide_implemented_fn() { | ||
528 | check( | ||
529 | r#" | ||
530 | trait Test { | ||
531 | fn foo(); | ||
532 | fn foo_bar(); | ||
533 | } | ||
534 | struct T; | ||
535 | |||
536 | impl Test for T { | ||
537 | fn foo() {} | ||
538 | fn f<|> | ||
539 | } | ||
540 | "#, | ||
541 | expect![[r#" | ||
542 | fn fn foo_bar() | ||
543 | "#]], | ||
544 | ); | ||
545 | } | ||
546 | |||
547 | #[test] | ||
548 | fn generic_fn() { | ||
549 | check_edit( | ||
550 | "foo", | ||
551 | r#" | ||
552 | trait Test { | ||
553 | fn foo<T>(); | ||
554 | } | ||
555 | struct T; | ||
556 | |||
557 | impl Test for T { | ||
558 | fn f<|> | ||
559 | } | ||
560 | "#, | ||
561 | r#" | ||
562 | trait Test { | ||
563 | fn foo<T>(); | ||
564 | } | ||
565 | struct T; | ||
566 | |||
567 | impl Test for T { | ||
568 | fn foo<T>() { | ||
569 | $0 | ||
570 | } | ||
571 | } | ||
572 | "#, | ||
573 | ); | ||
574 | check_edit( | ||
575 | "foo", | ||
576 | r#" | ||
577 | trait Test { | ||
578 | fn foo<T>() where T: Into<String>; | ||
579 | } | ||
580 | struct T; | ||
581 | |||
582 | impl Test for T { | ||
583 | fn f<|> | ||
584 | } | ||
585 | "#, | ||
586 | r#" | ||
587 | trait Test { | ||
588 | fn foo<T>() where T: Into<String>; | ||
589 | } | ||
590 | struct T; | ||
591 | |||
592 | impl Test for T { | ||
593 | fn foo<T>() | ||
594 | where T: Into<String> { | ||
595 | $0 | ||
596 | } | ||
597 | } | ||
598 | "#, | ||
599 | ); | ||
600 | } | ||
601 | |||
602 | #[test] | ||
603 | fn associated_type() { | ||
604 | check_edit( | ||
605 | "SomeType", | ||
606 | r#" | ||
607 | trait Test { | ||
608 | type SomeType; | ||
609 | } | ||
610 | |||
611 | impl Test for () { | ||
612 | type S<|> | ||
613 | } | ||
614 | "#, | ||
615 | " | ||
616 | trait Test { | ||
617 | type SomeType; | ||
618 | } | ||
619 | |||
620 | impl Test for () { | ||
621 | type SomeType = \n\ | ||
622 | } | ||
623 | ", | ||
624 | ); | ||
625 | } | ||
626 | |||
627 | #[test] | ||
628 | fn associated_const() { | ||
629 | check_edit( | ||
630 | "SOME_CONST", | ||
631 | r#" | ||
632 | trait Test { | ||
633 | const SOME_CONST: u16; | ||
634 | } | ||
635 | |||
636 | impl Test for () { | ||
637 | const S<|> | ||
638 | } | ||
639 | "#, | ||
640 | " | ||
641 | trait Test { | ||
642 | const SOME_CONST: u16; | ||
643 | } | ||
644 | |||
645 | impl Test for () { | ||
646 | const SOME_CONST: u16 = \n\ | ||
647 | } | ||
648 | ", | ||
649 | ); | ||
650 | |||
651 | check_edit( | ||
652 | "SOME_CONST", | ||
653 | r#" | ||
654 | trait Test { | ||
655 | const SOME_CONST: u16 = 92; | ||
656 | } | ||
657 | |||
658 | impl Test for () { | ||
659 | const S<|> | ||
660 | } | ||
661 | "#, | ||
662 | " | ||
663 | trait Test { | ||
664 | const SOME_CONST: u16 = 92; | ||
665 | } | ||
666 | |||
667 | impl Test for () { | ||
668 | const SOME_CONST: u16 = \n\ | ||
669 | } | ||
670 | ", | ||
671 | ); | ||
672 | } | ||
673 | |||
674 | #[test] | ||
675 | fn complete_without_name() { | ||
676 | let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| { | ||
677 | println!( | ||
678 | "completion='{}', hint='{}', next_sibling='{}'", | ||
679 | completion, hint, next_sibling | ||
680 | ); | ||
681 | |||
682 | check_edit( | ||
683 | completion, | ||
684 | &format!( | ||
685 | r#" | ||
686 | trait Test {{ | ||
687 | type Foo; | ||
688 | const CONST: u16; | ||
689 | fn bar(); | ||
690 | }} | ||
691 | struct T; | ||
692 | |||
693 | impl Test for T {{ | ||
694 | {} | ||
695 | {} | ||
696 | }} | ||
697 | "#, | ||
698 | hint, next_sibling | ||
699 | ), | ||
700 | &format!( | ||
701 | r#" | ||
702 | trait Test {{ | ||
703 | type Foo; | ||
704 | const CONST: u16; | ||
705 | fn bar(); | ||
706 | }} | ||
707 | struct T; | ||
708 | |||
709 | impl Test for T {{ | ||
710 | {} | ||
711 | {} | ||
712 | }} | ||
713 | "#, | ||
714 | completed, next_sibling | ||
715 | ), | ||
716 | ) | ||
717 | }; | ||
718 | |||
719 | // Enumerate some possible next siblings. | ||
720 | for next_sibling in &[ | ||
721 | "", | ||
722 | "fn other_fn() {}", // `const <|> fn` -> `const fn` | ||
723 | "type OtherType = i32;", | ||