]> git.lizzy.rs Git - rust.git/blob - crates/completion/src/render.rs
More useful fn detail in completion
[rust.git] / crates / completion / src / render.rs
1 //! `render` module provides utilities for rendering completion suggestions
2 //! into code pieces that will be presented to user.
3
4 pub(crate) mod macro_;
5 pub(crate) mod function;
6 pub(crate) mod enum_variant;
7 pub(crate) mod const_;
8 pub(crate) mod pattern;
9 pub(crate) mod type_alias;
10
11 mod builder_ext;
12
13 use hir::{
14     AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
15 };
16 use ide_db::{helpers::SnippetCap, RootDatabase, SymbolKind};
17 use syntax::TextRange;
18 use test_utils::mark;
19
20 use crate::{
21     item::ImportEdit, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
22     CompletionScore,
23 };
24
25 use crate::render::{enum_variant::render_variant, function::render_fn, macro_::render_macro};
26
27 pub(crate) fn render_field<'a>(
28     ctx: RenderContext<'a>,
29     field: hir::Field,
30     ty: &Type,
31 ) -> CompletionItem {
32     Render::new(ctx).add_field(field, ty)
33 }
34
35 pub(crate) fn render_tuple_field<'a>(
36     ctx: RenderContext<'a>,
37     field: usize,
38     ty: &Type,
39 ) -> CompletionItem {
40     Render::new(ctx).add_tuple_field(field, ty)
41 }
42
43 pub(crate) fn render_resolution<'a>(
44     ctx: RenderContext<'a>,
45     local_name: String,
46     resolution: &ScopeDef,
47 ) -> Option<CompletionItem> {
48     Render::new(ctx).render_resolution(local_name, None, resolution)
49 }
50
51 pub(crate) fn render_resolution_with_import<'a>(
52     ctx: RenderContext<'a>,
53     import_edit: ImportEdit,
54     resolution: &ScopeDef,
55 ) -> Option<CompletionItem> {
56     let local_name = match resolution {
57         ScopeDef::ModuleDef(ModuleDef::Function(f)) => f.name(ctx.completion.db).to_string(),
58         ScopeDef::ModuleDef(ModuleDef::Const(c)) => c.name(ctx.completion.db)?.to_string(),
59         ScopeDef::ModuleDef(ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db).to_string(),
60         _ => import_edit.import_path.segments.last()?.to_string(),
61     };
62     Render::new(ctx).render_resolution(local_name, Some(import_edit), resolution).map(|mut item| {
63         item.completion_kind = CompletionKind::Magic;
64         item
65     })
66 }
67
68 /// Interface for data and methods required for items rendering.
69 #[derive(Debug)]
70 pub(crate) struct RenderContext<'a> {
71     completion: &'a CompletionContext<'a>,
72 }
73
74 impl<'a> RenderContext<'a> {
75     pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
76         RenderContext { completion }
77     }
78
79     fn snippet_cap(&self) -> Option<SnippetCap> {
80         self.completion.config.snippet_cap.clone()
81     }
82
83     fn db(&self) -> &'a RootDatabase {
84         &self.completion.db
85     }
86
87     fn source_range(&self) -> TextRange {
88         self.completion.source_range()
89     }
90
91     fn is_deprecated(&self, node: impl HasAttrs) -> bool {
92         let attrs = node.attrs(self.db());
93         attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists()
94     }
95
96     fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {
97         let db = self.db();
98         let assoc = match as_assoc_item.as_assoc_item(db) {
99             Some(assoc) => assoc,
100             None => return false,
101         };
102
103         let is_assoc_deprecated = match assoc {
104             hir::AssocItem::Function(it) => self.is_deprecated(it),
105             hir::AssocItem::Const(it) => self.is_deprecated(it),
106             hir::AssocItem::TypeAlias(it) => self.is_deprecated(it),
107         };
108         is_assoc_deprecated
109             || assoc.containing_trait(db).map(|trait_| self.is_deprecated(trait_)).unwrap_or(false)
110     }
111
112     fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
113         node.docs(self.db())
114     }
115
116     fn active_name_and_type(&self) -> Option<(String, Type)> {
117         if let Some(record_field) = &self.completion.record_field_syntax {
118             mark::hit!(record_field_type_match);
119             let (struct_field, _local) = self.completion.sema.resolve_record_field(record_field)?;
120             Some((struct_field.name(self.db()).to_string(), struct_field.signature_ty(self.db())))
121         } else if let Some(active_parameter) = &self.completion.active_parameter {
122             mark::hit!(active_param_type_match);
123             Some((active_parameter.name.clone(), active_parameter.ty.clone()))
124         } else {
125             None
126         }
127     }
128 }
129
130 /// Generic renderer for completion items.
131 #[derive(Debug)]
132 struct Render<'a> {
133     ctx: RenderContext<'a>,
134 }
135
136 impl<'a> Render<'a> {
137     fn new(ctx: RenderContext<'a>) -> Render<'a> {
138         Render { ctx }
139     }
140
141     fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
142         let is_deprecated = self.ctx.is_deprecated(field);
143         let name = field.name(self.ctx.db());
144         let mut item = CompletionItem::new(
145             CompletionKind::Reference,
146             self.ctx.source_range(),
147             name.to_string(),
148         )
149         .kind(SymbolKind::Field)
150         .detail(ty.display(self.ctx.db()).to_string())
151         .set_documentation(field.docs(self.ctx.db()))
152         .set_deprecated(is_deprecated);
153
154         if let Some(score) = compute_score(&self.ctx, &ty, &name.to_string()) {
155             item = item.set_score(score);
156         }
157
158         item.build()
159     }
160
161     fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
162         CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), field.to_string())
163             .kind(SymbolKind::Field)
164             .detail(ty.display(self.ctx.db()).to_string())
165             .build()
166     }
167
168     fn render_resolution(
169         self,
170         local_name: String,
171         import_to_add: Option<ImportEdit>,
172         resolution: &ScopeDef,
173     ) -> Option<CompletionItem> {
174         let _p = profile::span("render_resolution");
175         use hir::ModuleDef::*;
176
177         let completion_kind = match resolution {
178             ScopeDef::ModuleDef(BuiltinType(..)) => CompletionKind::BuiltinType,
179             _ => CompletionKind::Reference,
180         };
181
182         let kind = match resolution {
183             ScopeDef::ModuleDef(Function(func)) => {
184                 return render_fn(self.ctx, import_to_add, Some(local_name), *func);
185             }
186             ScopeDef::ModuleDef(Variant(_))
187                 if self.ctx.completion.is_pat_binding_or_const
188                     | self.ctx.completion.is_irrefutable_pat_binding =>
189             {
190                 CompletionItemKind::SymbolKind(SymbolKind::Variant)
191             }
192             ScopeDef::ModuleDef(Variant(var)) => {
193                 let item = render_variant(self.ctx, import_to_add, Some(local_name), *var, None);
194                 return Some(item);
195             }
196             ScopeDef::MacroDef(mac) => {
197                 let item = render_macro(self.ctx, import_to_add, local_name, *mac);
198                 return item;
199             }
200
201             ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
202             ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
203                 hir::Adt::Struct(_) => SymbolKind::Struct,
204                 // FIXME: add CompletionItemKind::Union
205                 hir::Adt::Union(_) => SymbolKind::Struct,
206                 hir::Adt::Enum(_) => SymbolKind::Enum,
207             }),
208             ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::SymbolKind(SymbolKind::Const),
209             ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::SymbolKind(SymbolKind::Static),
210             ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::SymbolKind(SymbolKind::Trait),
211             ScopeDef::ModuleDef(TypeAlias(..)) => {
212                 CompletionItemKind::SymbolKind(SymbolKind::TypeAlias)
213             }
214             ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
215             ScopeDef::GenericParam(param) => CompletionItemKind::SymbolKind(match param {
216                 hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
217                 hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
218                 hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
219             }),
220             ScopeDef::Local(..) => CompletionItemKind::SymbolKind(SymbolKind::Local),
221             ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => {
222                 CompletionItemKind::SymbolKind(SymbolKind::SelfParam)
223             }
224             ScopeDef::Unknown => {
225                 let item = CompletionItem::new(
226                     CompletionKind::Reference,
227                     self.ctx.source_range(),
228                     local_name,
229                 )
230                 .kind(CompletionItemKind::UnresolvedReference)
231                 .add_import(import_to_add)
232                 .build();
233                 return Some(item);
234             }
235         };
236
237         let mut item =
238             CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone());
239         if let ScopeDef::Local(local) = resolution {
240             let ty = local.ty(self.ctx.db());
241             if !ty.is_unknown() {
242                 item = item.detail(ty.display(self.ctx.db()).to_string());
243             }
244         };
245
246         let mut ref_match = None;
247         if let ScopeDef::Local(local) = resolution {
248             if let Some((active_name, active_type)) = self.ctx.active_name_and_type() {
249                 let ty = local.ty(self.ctx.db());
250                 if let Some(score) =
251                     compute_score_from_active(&active_type, &active_name, &ty, &local_name)
252                 {
253                     item = item.set_score(score);
254                 }
255                 ref_match = refed_type_matches(&active_type, &active_name, &ty, &local_name);
256             }
257         }
258
259         // Add `<>` for generic types
260         if self.ctx.completion.is_path_type
261             && !self.ctx.completion.has_type_args
262             && self.ctx.completion.config.add_call_parenthesis
263         {
264             if let Some(cap) = self.ctx.snippet_cap() {
265                 let has_non_default_type_params = match resolution {
266                     ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(self.ctx.db()),
267                     ScopeDef::ModuleDef(TypeAlias(it)) => {
268                         it.has_non_default_type_params(self.ctx.db())
269                     }
270                     _ => false,
271                 };
272                 if has_non_default_type_params {
273                     mark::hit!(inserts_angle_brackets_for_generics);
274                     item = item
275                         .lookup_by(local_name.clone())
276                         .label(format!("{}<…>", local_name))
277                         .insert_snippet(cap, format!("{}<$0>", local_name));
278                 }
279             }
280         }
281
282         Some(
283             item.kind(kind)
284                 .add_import(import_to_add)
285                 .set_ref_match(ref_match)
286                 .set_documentation(self.docs(resolution))
287                 .set_deprecated(self.is_deprecated(resolution))
288                 .build(),
289         )
290     }
291
292     fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
293         use hir::ModuleDef::*;
294         match resolution {
295             ScopeDef::ModuleDef(Module(it)) => it.docs(self.ctx.db()),
296             ScopeDef::ModuleDef(Adt(it)) => it.docs(self.ctx.db()),
297             ScopeDef::ModuleDef(Variant(it)) => it.docs(self.ctx.db()),
298             ScopeDef::ModuleDef(Const(it)) => it.docs(self.ctx.db()),
299             ScopeDef::ModuleDef(Static(it)) => it.docs(self.ctx.db()),
300             ScopeDef::ModuleDef(Trait(it)) => it.docs(self.ctx.db()),
301             ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(self.ctx.db()),
302             _ => None,
303         }
304     }
305
306     fn is_deprecated(&self, resolution: &ScopeDef) -> bool {
307         match resolution {
308             ScopeDef::ModuleDef(it) => self.ctx.is_deprecated_assoc_item(*it),
309             ScopeDef::MacroDef(it) => self.ctx.is_deprecated(*it),
310             ScopeDef::GenericParam(it) => self.ctx.is_deprecated(*it),
311             ScopeDef::AdtSelfType(it) => self.ctx.is_deprecated(*it),
312             _ => false,
313         }
314     }
315 }
316
317 fn compute_score_from_active(
318     active_type: &Type,
319     active_name: &str,
320     ty: &Type,
321     name: &str,
322 ) -> Option<CompletionScore> {
323     // Compute score
324     // For the same type
325     if active_type != ty {
326         return None;
327     }
328
329     let mut res = CompletionScore::TypeMatch;
330
331     // If same type + same name then go top position
332     if active_name == name {
333         res = CompletionScore::TypeAndNameMatch
334     }
335
336     Some(res)
337 }
338 fn refed_type_matches(
339     active_type: &Type,
340     active_name: &str,
341     ty: &Type,
342     name: &str,
343 ) -> Option<(Mutability, CompletionScore)> {
344     let derefed_active = active_type.remove_ref()?;
345     let score = compute_score_from_active(&derefed_active, &active_name, &ty, &name)?;
346     Some((
347         if active_type.is_mutable_reference() { Mutability::Mut } else { Mutability::Shared },
348         score,
349     ))
350 }
351
352 fn compute_score(ctx: &RenderContext, ty: &Type, name: &str) -> Option<CompletionScore> {
353     let (active_name, active_type) = ctx.active_name_and_type()?;
354     compute_score_from_active(&active_type, &active_name, ty, name)
355 }
356
357 #[cfg(test)]
358 mod tests {
359     use std::cmp::Reverse;
360
361     use expect_test::{expect, Expect};
362     use test_utils::mark;
363
364     use crate::{
365         test_utils::{check_edit, do_completion, get_all_items, TEST_CONFIG},
366         CompletionKind, CompletionScore,
367     };
368
369     fn check(ra_fixture: &str, expect: Expect) {
370         let actual = do_completion(ra_fixture, CompletionKind::Reference);
371         expect.assert_debug_eq(&actual);
372     }
373
374     fn check_scores(ra_fixture: &str, expect: Expect) {
375         fn display_score(score: Option<CompletionScore>) -> &'static str {
376             match score {
377                 Some(CompletionScore::TypeMatch) => "[type]",
378                 Some(CompletionScore::TypeAndNameMatch) => "[type+name]",
379                 None => "[]".into(),
380             }
381         }
382
383         let mut completions = get_all_items(TEST_CONFIG, ra_fixture);
384         completions.sort_by_key(|it| (Reverse(it.score()), it.label().to_string()));
385         let actual = completions
386             .into_iter()
387             .filter(|it| it.completion_kind == CompletionKind::Reference)
388             .map(|it| {
389                 let tag = it.kind().unwrap().tag();
390                 let score = display_score(it.score());
391                 format!("{} {} {}\n", tag, it.label(), score)
392             })
393             .collect::<String>();
394         expect.assert_eq(&actual);
395     }
396
397     #[test]
398     fn enum_detail_includes_record_fields() {
399         check(
400             r#"
401 enum Foo { Foo { x: i32, y: i32 } }
402
403 fn main() { Foo::Fo$0 }
404 "#,
405             expect![[r#"
406                 [
407                     CompletionItem {
408                         label: "Foo",
409                         source_range: 54..56,
410                         delete: 54..56,
411                         insert: "Foo",
412                         kind: SymbolKind(
413                             Variant,
414                         ),
415                         detail: "{ x: i32, y: i32 }",
416                     },
417                 ]
418             "#]],
419         );
420     }
421
422     #[test]
423     fn enum_detail_doesnt_include_tuple_fields() {
424         check(
425             r#"
426 enum Foo { Foo (i32, i32) }
427
428 fn main() { Foo::Fo$0 }
429 "#,
430             expect![[r#"
431                 [
432                     CompletionItem {
433                         label: "Foo(…)",
434                         source_range: 46..48,
435                         delete: 46..48,
436                         insert: "Foo($0)",
437                         kind: SymbolKind(
438                             Variant,
439                         ),
440                         lookup: "Foo",
441                         detail: "(i32, i32)",
442                         trigger_call_info: true,
443                     },
444                 ]
445             "#]],
446         );
447     }
448
449     #[test]
450     fn enum_detail_just_parentheses_for_unit() {
451         check(
452             r#"
453 enum Foo { Foo }
454
455 fn main() { Foo::Fo$0 }
456 "#,
457             expect![[r#"
458                 [
459                     CompletionItem {
460                         label: "Foo",
461                         source_range: 35..37,
462                         delete: 35..37,
463                         insert: "Foo",
464                         kind: SymbolKind(
465                             Variant,
466                         ),
467                         detail: "()",
468                     },
469                 ]
470             "#]],
471         );
472     }
473
474     #[test]
475     fn lookup_enums_by_two_qualifiers() {
476         check(
477             r#"
478 mod m {
479     pub enum Spam { Foo, Bar(i32) }
480 }
481 fn main() { let _: m::Spam = S$0 }
482 "#,
483             expect![[r#"
484                 [
485                     CompletionItem {
486                         label: "Spam::Bar(…)",
487                         source_range: 75..76,
488                         delete: 75..76,
489                         insert: "Spam::Bar($0)",
490                         kind: SymbolKind(
491                             Variant,
492                         ),
493                         lookup: "Spam::Bar",
494                         detail: "(i32)",
495                         trigger_call_info: true,
496                     },
497                     CompletionItem {
498                         label: "m",
499                         source_range: 75..76,
500                         delete: 75..76,
501                         insert: "m",
502                         kind: SymbolKind(
503                             Module,
504                         ),
505                     },
506                     CompletionItem {
507                         label: "m::Spam::Foo",
508                         source_range: 75..76,
509                         delete: 75..76,
510                         insert: "m::Spam::Foo",
511                         kind: SymbolKind(
512                             Variant,
513                         ),
514                         lookup: "Spam::Foo",
515                         detail: "()",
516                     },
517                     CompletionItem {
518                         label: "main()",
519                         source_range: 75..76,
520                         delete: 75..76,
521                         insert: "main()$0",
522                         kind: SymbolKind(
523                             Function,
524                         ),
525                         lookup: "main",
526                         detail: "-> ()",
527                     },
528                 ]
529             "#]],
530         )
531     }
532
533     #[test]
534     fn sets_deprecated_flag_in_items() {
535         check(
536             r#"
537 #[deprecated]
538 fn something_deprecated() {}
539 #[rustc_deprecated(since = "1.0.0")]
540 fn something_else_deprecated() {}
541
542 fn main() { som$0 }
543 "#,
544             expect![[r#"
545                 [
546                     CompletionItem {
547                         label: "main()",
548                         source_range: 127..130,
549                         delete: 127..130,
550                         insert: "main()$0",
551                         kind: SymbolKind(
552                             Function,
553                         ),
554                         lookup: "main",
555                         detail: "-> ()",
556                     },
557                     CompletionItem {
558                         label: "something_deprecated()",
559                         source_range: 127..130,
560                         delete: 127..130,
561                         insert: "something_deprecated()$0",
562                         kind: SymbolKind(
563                             Function,
564                         ),
565                         lookup: "something_deprecated",
566                         detail: "-> ()",
567                         deprecated: true,
568                     },
569                     CompletionItem {
570                         label: "something_else_deprecated()",
571                         source_range: 127..130,
572                         delete: 127..130,
573                         insert: "something_else_deprecated()$0",
574                         kind: SymbolKind(
575                             Function,
576                         ),
577                         lookup: "something_else_deprecated",
578                         detail: "-> ()",
579                         deprecated: true,
580                     },
581                 ]
582             "#]],
583         );
584
585         check(
586             r#"
587 struct A { #[deprecated] the_field: u32 }
588 fn foo() { A { the$0 } }
589 "#,
590             expect![[r#"
591                 [
592                     CompletionItem {
593                         label: "the_field",
594                         source_range: 57..60,
595                         delete: 57..60,
596                         insert: "the_field",
597                         kind: SymbolKind(
598                             Field,
599                         ),
600                         detail: "u32",
601                         deprecated: true,
602                     },
603                 ]
604             "#]],
605         );
606     }
607
608     #[test]
609     fn renders_docs() {
610         check(
611             r#"
612 struct S {
613     /// Field docs
614     foo:
615 }
616 impl S {
617     /// Method docs
618     fn bar(self) { self.$0 }
619 }"#,
620             expect![[r#"
621                 [
622                     CompletionItem {
623                         label: "bar()",
624                         source_range: 94..94,
625                         delete: 94..94,
626                         insert: "bar()$0",
627                         kind: Method,
628                         lookup: "bar",
629                         detail: "-> ()",
630                         documentation: Documentation(
631                             "Method docs",
632                         ),
633                     },
634                     CompletionItem {
635                         label: "foo",
636                         source_range: 94..94,
637                         delete: 94..94,
638                         insert: "foo",
639                         kind: SymbolKind(
640                             Field,
641                         ),
642                         detail: "{unknown}",
643                         documentation: Documentation(
644                             "Field docs",
645                         ),
646                     },
647                 ]
648             "#]],
649         );
650
651         check(
652             r#"
653 use self::my$0;
654
655 /// mod docs
656 mod my { }
657
658 /// enum docs
659 enum E {
660     /// variant docs
661     V
662 }
663 use self::E::*;
664 "#,
665             expect![[r#"
666                 [
667                     CompletionItem {
668                         label: "E",
669                         source_range: 10..12,
670                         delete: 10..12,
671                         insert: "E",
672                         kind: SymbolKind(
673                             Enum,
674                         ),
675                         documentation: Documentation(
676                             "enum docs",
677                         ),
678                     },
679                     CompletionItem {
680                         label: "V",
681                         source_range: 10..12,
682                         delete: 10..12,
683                         insert: "V",
684                         kind: SymbolKind(
685                             Variant,
686                         ),
687                         detail: "()",
688                         documentation: Documentation(
689                             "variant docs",
690                         ),
691                     },
692                     CompletionItem {
693                         label: "my",
694                         source_range: 10..12,
695                         delete: 10..12,
696                         insert: "my",
697                         kind: SymbolKind(
698                             Module,
699                         ),
700                         documentation: Documentation(
701                             "mod docs",
702                         ),
703                     },
704                 ]
705             "#]],
706         )
707     }
708
709     #[test]
710     fn dont_render_attrs() {
711         check(
712             r#"
713 struct S;
714 impl S {
715     #[inline]
716     fn the_method(&self) { }
717 }
718 fn foo(s: S) { s.$0 }
719 "#,
720             expect![[r#"
721                 [
722                     CompletionItem {
723                         label: "the_method()",
724                         source_range: 81..81,
725                         delete: 81..81,
726                         insert: "the_method()$0",
727                         kind: Method,
728                         lookup: "the_method",
729                         detail: "-> ()",
730                     },
731                 ]
732             "#]],
733         )
734     }
735
736     #[test]
737     fn no_call_parens_if_fn_ptr_needed() {
738         mark::check!(no_call_parens_if_fn_ptr_needed);
739         check_edit(
740             "foo",
741             r#"
742 fn foo(foo: u8, bar: u8) {}
743 struct ManualVtable { f: fn(u8, u8) }
744
745 fn main() -> ManualVtable {
746     ManualVtable { f: f$0 }
747 }
748 "#,
749             r#"
750 fn foo(foo: u8, bar: u8) {}
751 struct ManualVtable { f: fn(u8, u8) }
752
753 fn main() -> ManualVtable {
754     ManualVtable { f: foo }
755 }
756 "#,
757         );
758     }
759
760     #[test]
761     fn no_parens_in_use_item() {
762         mark::check!(no_parens_in_use_item);
763         check_edit(
764             "foo",
765             r#"
766 mod m { pub fn foo() {} }
767 use crate::m::f$0;
768 "#,
769             r#"
770 mod m { pub fn foo() {} }
771 use crate::m::foo;
772 "#,
773         );
774     }
775
776     #[test]
777     fn no_parens_in_call() {
778         check_edit(
779             "foo",
780             r#"
781 fn foo(x: i32) {}
782 fn main() { f$0(); }
783 "#,
784             r#"
785 fn foo(x: i32) {}
786 fn main() { foo(); }
787 "#,
788         );
789         check_edit(
790             "foo",
791             r#"
792 struct Foo;
793 impl Foo { fn foo(&self){} }
794 fn f(foo: &Foo) { foo.f$0(); }
795 "#,
796             r#"
797 struct Foo;
798 impl Foo { fn foo(&self){} }
799 fn f(foo: &Foo) { foo.foo(); }
800 "#,
801         );
802     }
803
804     #[test]
805     fn inserts_angle_brackets_for_generics() {
806         mark::check!(inserts_angle_brackets_for_generics);
807         check_edit(
808             "Vec",
809             r#"
810 struct Vec<T> {}
811 fn foo(xs: Ve$0)
812 "#,
813             r#"
814 struct Vec<T> {}
815 fn foo(xs: Vec<$0>)
816 "#,
817         );
818         check_edit(
819             "Vec",
820             r#"
821 type Vec<T> = (T,);
822 fn foo(xs: Ve$0)
823 "#,
824             r#"
825 type Vec<T> = (T,);
826 fn foo(xs: Vec<$0>)
827 "#,
828         );
829         check_edit(
830             "Vec",
831             r#"
832 struct Vec<T = i128> {}
833 fn foo(xs: Ve$0)
834 "#,
835             r#"
836 struct Vec<T = i128> {}
837 fn foo(xs: Vec)
838 "#,
839         );
840         check_edit(
841             "Vec",
842             r#"
843 struct Vec<T> {}
844 fn foo(xs: Ve$0<i128>)
845 "#,
846             r#"
847 struct Vec<T> {}
848 fn foo(xs: Vec<i128>)
849 "#,
850         );
851     }
852
853     #[test]
854     fn active_param_score() {
855         mark::check!(active_param_type_match);
856         check_scores(
857             r#"
858 struct S { foo: i64, bar: u32, baz: u32 }
859 fn test(bar: u32) { }
860 fn foo(s: S) { test(s.$0) }
861 "#,
862             expect![[r#"
863                 fd bar [type+name]
864                 fd baz [type]
865                 fd foo []
866             "#]],
867         );
868     }
869
870     #[test]
871     fn record_field_scores() {
872         mark::check!(record_field_type_match);
873         check_scores(
874             r#"
875 struct A { foo: i64, bar: u32, baz: u32 }
876 struct B { x: (), y: f32, bar: u32 }
877 fn foo(a: A) { B { bar: a.$0 }; }
878 "#,
879             expect![[r#"
880                 fd bar [type+name]
881                 fd baz [type]
882                 fd foo []
883             "#]],
884         )
885     }
886
887     #[test]
888     fn record_field_and_call_scores() {
889         check_scores(
890             r#"
891 struct A { foo: i64, bar: u32, baz: u32 }
892 struct B { x: (), y: f32, bar: u32 }
893 fn f(foo: i64) {  }
894 fn foo(a: A) { B { bar: f(a.$0) }; }
895 "#,
896             expect![[r#"
897                 fd foo [type+name]
898                 fd bar []
899                 fd baz []
900             "#]],
901         );
902         check_scores(
903             r#"
904 struct A { foo: i64, bar: u32, baz: u32 }
905 struct B { x: (), y: f32, bar: u32 }
906 fn f(foo: i64) {  }
907 fn foo(a: A) { f(B { bar: a.$0 }); }
908 "#,
909             expect![[r#"
910                 fd bar [type+name]
911                 fd baz [type]
912                 fd foo []
913             "#]],
914         );
915     }
916
917     #[test]
918     fn prioritize_exact_ref_match() {
919         check_scores(
920             r#"
921 struct WorldSnapshot { _f: () };
922 fn go(world: &WorldSnapshot) { go(w$0) }
923 "#,
924             expect![[r#"
925                 lc world [type+name]
926                 st WorldSnapshot []
927                 fn go(…) []
928             "#]],
929         );
930     }
931
932     #[test]
933     fn too_many_arguments() {
934         check_scores(
935             r#"
936 struct Foo;
937 fn f(foo: &Foo) { f(foo, w$0) }
938 "#,
939             expect![[r#"
940                 st Foo []
941                 fn f(…) []
942                 lc foo []
943             "#]],
944         );
945     }
946 }