]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/render.rs
Auto merge of #12455 - bitgaoshu:fix_12441, r=flodiebold
[rust.git] / crates / ide-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 const_;
7 pub(crate) mod pattern;
8 pub(crate) mod type_alias;
9 pub(crate) mod variant;
10 pub(crate) mod union_literal;
11 pub(crate) mod literal;
12
13 use hir::{AsAssocItem, HasAttrs, HirDisplay, ScopeDef};
14 use ide_db::{
15     helpers::item_name, imports::import_assets::LocatedImport, RootDatabase, SnippetCap, SymbolKind,
16 };
17 use syntax::{SmolStr, SyntaxKind, TextRange};
18
19 use crate::{
20     context::{PathCompletionCtx, PathKind},
21     item::{Builder, CompletionRelevanceTypeMatch},
22     render::{function::render_fn, literal::render_variant_lit, macro_::render_macro},
23     CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
24 };
25 /// Interface for data and methods required for items rendering.
26 #[derive(Debug, Clone)]
27 pub(crate) struct RenderContext<'a> {
28     completion: &'a CompletionContext<'a>,
29     is_private_editable: bool,
30     import_to_add: Option<LocatedImport>,
31 }
32
33 impl<'a> RenderContext<'a> {
34     pub(crate) fn new(completion: &'a CompletionContext<'a>) -> RenderContext<'a> {
35         RenderContext { completion, is_private_editable: false, import_to_add: None }
36     }
37
38     pub(crate) fn private_editable(mut self, private_editable: bool) -> Self {
39         self.is_private_editable = private_editable;
40         self
41     }
42
43     pub(crate) fn import_to_add(mut self, import_to_add: Option<LocatedImport>) -> Self {
44         self.import_to_add = import_to_add;
45         self
46     }
47
48     fn snippet_cap(&self) -> Option<SnippetCap> {
49         self.completion.config.snippet_cap
50     }
51
52     fn db(&self) -> &'a RootDatabase {
53         self.completion.db
54     }
55
56     fn source_range(&self) -> TextRange {
57         self.completion.source_range()
58     }
59
60     fn completion_relevance(&self) -> CompletionRelevance {
61         CompletionRelevance {
62             is_private_editable: self.is_private_editable,
63             requires_import: self.import_to_add.is_some(),
64             ..Default::default()
65         }
66     }
67
68     fn is_immediately_after_macro_bang(&self) -> bool {
69         self.completion.token.kind() == SyntaxKind::BANG
70             && self
71                 .completion
72                 .token
73                 .parent()
74                 .map_or(false, |it| it.kind() == SyntaxKind::MACRO_CALL)
75     }
76
77     pub(crate) fn path_is_call(&self) -> bool {
78         self.completion.path_context().map_or(false, |it| it.has_call_parens)
79     }
80
81     fn is_deprecated(&self, def: impl HasAttrs) -> bool {
82         let attrs = def.attrs(self.db());
83         attrs.by_key("deprecated").exists()
84     }
85
86     fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {
87         let db = self.db();
88         let assoc = match as_assoc_item.as_assoc_item(db) {
89             Some(assoc) => assoc,
90             None => return false,
91         };
92
93         let is_assoc_deprecated = match assoc {
94             hir::AssocItem::Function(it) => self.is_deprecated(it),
95             hir::AssocItem::Const(it) => self.is_deprecated(it),
96             hir::AssocItem::TypeAlias(it) => self.is_deprecated(it),
97         };
98         is_assoc_deprecated
99             || assoc
100                 .containing_trait_or_trait_impl(db)
101                 .map(|trait_| self.is_deprecated(trait_))
102                 .unwrap_or(false)
103     }
104
105     // FIXME: remove this
106     fn docs(&self, def: impl HasAttrs) -> Option<hir::Documentation> {
107         def.docs(self.db())
108     }
109 }
110
111 pub(crate) fn render_field(
112     ctx: RenderContext<'_>,
113     receiver: Option<hir::Name>,
114     field: hir::Field,
115     ty: &hir::Type,
116 ) -> CompletionItem {
117     let is_deprecated = ctx.is_deprecated(field);
118     let name = field.name(ctx.db()).to_smol_str();
119     let mut item = CompletionItem::new(
120         SymbolKind::Field,
121         ctx.source_range(),
122         receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name).into()),
123     );
124     item.set_relevance(CompletionRelevance {
125         type_match: compute_type_match(ctx.completion, ty),
126         exact_name_match: compute_exact_name_match(ctx.completion, name.as_str()),
127         ..CompletionRelevance::default()
128     });
129     item.detail(ty.display(ctx.db()).to_string())
130         .set_documentation(field.docs(ctx.db()))
131         .set_deprecated(is_deprecated)
132         .lookup_by(name.clone());
133     let is_keyword = SyntaxKind::from_keyword(name.as_str()).is_some();
134     if is_keyword && !matches!(name.as_str(), "self" | "crate" | "super" | "Self") {
135         item.insert_text(format!("r#{}", name));
136     }
137     if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) {
138         // FIXME
139         // For now we don't properly calculate the edits for ref match
140         // completions on struct fields, so we've disabled them. See #8058.
141     }
142     item.build()
143 }
144
145 pub(crate) fn render_tuple_field(
146     ctx: RenderContext<'_>,
147     receiver: Option<hir::Name>,
148     field: usize,
149     ty: &hir::Type,
150 ) -> CompletionItem {
151     let mut item = CompletionItem::new(
152         SymbolKind::Field,
153         ctx.source_range(),
154         receiver.map_or_else(|| field.to_string(), |receiver| format!("{}.{}", receiver, field)),
155     );
156     item.detail(ty.display(ctx.db()).to_string()).lookup_by(field.to_string());
157     item.build()
158 }
159
160 pub(crate) fn render_resolution(
161     ctx: RenderContext<'_>,
162     local_name: hir::Name,
163     resolution: ScopeDef,
164 ) -> Builder {
165     render_resolution_(ctx, local_name, None, resolution)
166 }
167
168 pub(crate) fn render_resolution_simple(
169     ctx: RenderContext<'_>,
170     local_name: hir::Name,
171     resolution: ScopeDef,
172 ) -> Builder {
173     render_resolution_simple_(ctx, local_name, None, resolution)
174 }
175
176 pub(crate) fn render_resolution_with_import(
177     ctx: RenderContext<'_>,
178     import_edit: LocatedImport,
179 ) -> Option<Builder> {
180     let resolution = ScopeDef::from(import_edit.original_item);
181     let local_name = match resolution {
182         ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
183         ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
184         ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db),
185         _ => item_name(ctx.db(), import_edit.original_item)?,
186     };
187     Some(render_resolution_(ctx, local_name, Some(import_edit), resolution))
188 }
189
190 pub(crate) fn render_type_inference(ty_string: String, ctx: &CompletionContext) -> CompletionItem {
191     let mut builder =
192         CompletionItem::new(CompletionItemKind::InferredType, ctx.source_range(), ty_string);
193     builder.set_relevance(CompletionRelevance { is_definite: true, ..Default::default() });
194     builder.build()
195 }
196
197 fn render_resolution_(
198     ctx: RenderContext<'_>,
199     local_name: hir::Name,
200     import_to_add: Option<LocatedImport>,
201     resolution: ScopeDef,
202 ) -> Builder {
203     let _p = profile::span("render_resolution");
204     use hir::ModuleDef::*;
205
206     match resolution {
207         ScopeDef::ModuleDef(Macro(mac)) => {
208             let ctx = ctx.import_to_add(import_to_add);
209             return render_macro(ctx, local_name, mac);
210         }
211         ScopeDef::ModuleDef(Function(func)) => {
212             let ctx = ctx.import_to_add(import_to_add);
213             return render_fn(ctx, Some(local_name), func);
214         }
215         ScopeDef::ModuleDef(Variant(var)) if ctx.completion.pattern_ctx.is_none() => {
216             let ctx = ctx.clone().import_to_add(import_to_add.clone());
217             if let Some(item) = render_variant_lit(ctx, Some(local_name.clone()), var, None) {
218                 return item;
219             }
220         }
221         _ => (),
222     }
223     render_resolution_simple_(ctx, local_name, import_to_add, resolution)
224 }
225
226 fn render_resolution_simple_(
227     ctx: RenderContext<'_>,
228     local_name: hir::Name,
229     import_to_add: Option<LocatedImport>,
230     resolution: ScopeDef,
231 ) -> Builder {
232     let _p = profile::span("render_resolution");
233     use hir::ModuleDef::*;
234
235     let db = ctx.db();
236     let ctx = ctx.import_to_add(import_to_add);
237     let kind = match resolution {
238         ScopeDef::Unknown => CompletionItemKind::UnresolvedReference,
239         ScopeDef::ModuleDef(Function(_)) => CompletionItemKind::SymbolKind(SymbolKind::Function),
240         ScopeDef::ModuleDef(Variant(_)) => CompletionItemKind::SymbolKind(SymbolKind::Variant),
241         ScopeDef::ModuleDef(Macro(_)) => CompletionItemKind::SymbolKind(SymbolKind::Macro),
242         ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::SymbolKind(SymbolKind::Module),
243         ScopeDef::ModuleDef(Adt(adt)) => CompletionItemKind::SymbolKind(match adt {
244             hir::Adt::Struct(_) => SymbolKind::Struct,
245             hir::Adt::Union(_) => SymbolKind::Union,
246             hir::Adt::Enum(_) => SymbolKind::Enum,
247         }),
248         ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::SymbolKind(SymbolKind::Const),
249         ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::SymbolKind(SymbolKind::Static),
250         ScopeDef::ModuleDef(Trait(..)) => CompletionItemKind::SymbolKind(SymbolKind::Trait),
251         ScopeDef::ModuleDef(TypeAlias(..)) => CompletionItemKind::SymbolKind(SymbolKind::TypeAlias),
252         ScopeDef::ModuleDef(BuiltinType(..)) => CompletionItemKind::BuiltinType,
253         ScopeDef::GenericParam(param) => CompletionItemKind::SymbolKind(match param {
254             hir::GenericParam::TypeParam(_) => SymbolKind::TypeParam,
255             hir::GenericParam::ConstParam(_) => SymbolKind::ConstParam,
256             hir::GenericParam::LifetimeParam(_) => SymbolKind::LifetimeParam,
257         }),
258         ScopeDef::Local(..) => CompletionItemKind::SymbolKind(SymbolKind::Local),
259         ScopeDef::Label(..) => CompletionItemKind::SymbolKind(SymbolKind::Label),
260         ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => {
261             CompletionItemKind::SymbolKind(SymbolKind::SelfParam)
262         }
263     };
264
265     let local_name = local_name.to_smol_str();
266     let mut item = CompletionItem::new(kind, ctx.source_range(), local_name.clone());
267     item.set_relevance(ctx.completion_relevance());
268     if let ScopeDef::Local(local) = resolution {
269         let ty = local.ty(db);
270         if !ty.is_unknown() {
271             item.detail(ty.display(db).to_string());
272         }
273
274         item.set_relevance(CompletionRelevance {
275             type_match: compute_type_match(ctx.completion, &ty),
276             exact_name_match: compute_exact_name_match(ctx.completion, &local_name),
277             is_local: true,
278             ..CompletionRelevance::default()
279         });
280
281         if let Some(ref_match) = compute_ref_match(ctx.completion, &ty) {
282             item.ref_match(ref_match);
283         }
284     };
285
286     // Add `<>` for generic types
287     let type_path_no_ty_args = matches!(
288         ctx.completion.path_context(),
289         Some(PathCompletionCtx { kind: PathKind::Type { .. }, has_type_args: false, .. })
290     ) && ctx.completion.config.callable.is_some();
291     if type_path_no_ty_args {
292         if let Some(cap) = ctx.snippet_cap() {
293             let has_non_default_type_params = match resolution {
294                 ScopeDef::ModuleDef(Adt(it)) => it.has_non_default_type_params(db),
295                 ScopeDef::ModuleDef(TypeAlias(it)) => it.has_non_default_type_params(db),
296                 _ => false,
297             };
298             if has_non_default_type_params {
299                 cov_mark::hit!(inserts_angle_brackets_for_generics);
300                 item.lookup_by(local_name.clone())
301                     .label(SmolStr::from_iter([&local_name, "<…>"]))
302                     .trigger_call_info()
303                     .insert_snippet(cap, format!("{}<$0>", local_name));
304             }
305         }
306     }
307     item.set_documentation(scope_def_docs(db, resolution))
308         .set_deprecated(scope_def_is_deprecated(&ctx, resolution));
309
310     if let Some(import_to_add) = ctx.import_to_add {
311         item.add_import(import_to_add);
312     }
313     item
314 }
315
316 fn scope_def_docs(db: &RootDatabase, resolution: ScopeDef) -> Option<hir::Documentation> {
317     use hir::ModuleDef::*;
318     match resolution {
319         ScopeDef::ModuleDef(Module(it)) => it.docs(db),
320         ScopeDef::ModuleDef(Adt(it)) => it.docs(db),
321         ScopeDef::ModuleDef(Variant(it)) => it.docs(db),
322         ScopeDef::ModuleDef(Const(it)) => it.docs(db),
323         ScopeDef::ModuleDef(Static(it)) => it.docs(db),
324         ScopeDef::ModuleDef(Trait(it)) => it.docs(db),
325         ScopeDef::ModuleDef(TypeAlias(it)) => it.docs(db),
326         _ => None,
327     }
328 }
329
330 fn scope_def_is_deprecated(ctx: &RenderContext<'_>, resolution: ScopeDef) -> bool {
331     match resolution {
332         ScopeDef::ModuleDef(it) => ctx.is_deprecated_assoc_item(it),
333         ScopeDef::GenericParam(it) => ctx.is_deprecated(it),
334         ScopeDef::AdtSelfType(it) => ctx.is_deprecated(it),
335         _ => false,
336     }
337 }
338
339 fn compute_type_match(
340     ctx: &CompletionContext,
341     completion_ty: &hir::Type,
342 ) -> Option<CompletionRelevanceTypeMatch> {
343     let expected_type = ctx.expected_type.as_ref()?;
344
345     // We don't ever consider unit type to be an exact type match, since
346     // nearly always this is not meaningful to the user.
347     if expected_type.is_unit() {
348         return None;
349     }
350
351     if completion_ty == expected_type {
352         Some(CompletionRelevanceTypeMatch::Exact)
353     } else if expected_type.could_unify_with(ctx.db, completion_ty) {
354         Some(CompletionRelevanceTypeMatch::CouldUnify)
355     } else {
356         None
357     }
358 }
359
360 fn compute_exact_name_match(ctx: &CompletionContext, completion_name: &str) -> bool {
361     ctx.expected_name.as_ref().map_or(false, |name| name.text() == completion_name)
362 }
363
364 fn compute_ref_match(
365     ctx: &CompletionContext,
366     completion_ty: &hir::Type,
367 ) -> Option<hir::Mutability> {
368     let expected_type = ctx.expected_type.as_ref()?;
369     if completion_ty != expected_type {
370         let expected_type_without_ref = expected_type.remove_ref()?;
371         if completion_ty.autoderef(ctx.db).any(|deref_ty| deref_ty == expected_type_without_ref) {
372             cov_mark::hit!(suggest_ref);
373             let mutability = if expected_type.is_mutable_reference() {
374                 hir::Mutability::Mut
375             } else {
376                 hir::Mutability::Shared
377             };
378             return Some(mutability);
379         };
380     }
381     None
382 }
383
384 #[cfg(test)]
385 mod tests {
386     use std::cmp;
387
388     use expect_test::{expect, Expect};
389     use ide_db::SymbolKind;
390     use itertools::Itertools;
391
392     use crate::{
393         item::CompletionRelevanceTypeMatch,
394         tests::{check_edit, do_completion, get_all_items, TEST_CONFIG},
395         CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch,
396     };
397
398     #[track_caller]
399     fn check(ra_fixture: &str, kind: impl Into<CompletionItemKind>, expect: Expect) {
400         let actual = do_completion(ra_fixture, kind.into());
401         expect.assert_debug_eq(&actual);
402     }
403
404     #[track_caller]
405     fn check_kinds(ra_fixture: &str, kinds: &[CompletionItemKind], expect: Expect) {
406         let actual: Vec<_> =
407             kinds.iter().flat_map(|&kind| do_completion(ra_fixture, kind)).collect();
408         expect.assert_debug_eq(&actual);
409     }
410
411     #[track_caller]
412     fn check_relevance_for_kinds(ra_fixture: &str, kinds: &[CompletionItemKind], expect: Expect) {
413         let mut actual = get_all_items(TEST_CONFIG, ra_fixture, None);
414         actual.retain(|it| kinds.contains(&it.kind()));
415         actual.sort_by_key(|it| cmp::Reverse(it.relevance().score()));
416         check_relevance_(actual, expect);
417     }
418
419     #[track_caller]
420     fn check_relevance(ra_fixture: &str, expect: Expect) {
421         let mut actual = get_all_items(TEST_CONFIG, ra_fixture, None);
422         actual.retain(|it| it.kind() != CompletionItemKind::Snippet);
423         actual.retain(|it| it.kind() != CompletionItemKind::Keyword);
424         actual.retain(|it| it.kind() != CompletionItemKind::BuiltinType);
425         actual.sort_by_key(|it| cmp::Reverse(it.relevance().score()));
426         check_relevance_(actual, expect);
427     }
428
429     #[track_caller]
430     fn check_relevance_(actual: Vec<CompletionItem>, expect: Expect) {
431         let actual = actual
432             .into_iter()
433             .flat_map(|it| {
434                 let mut items = vec![];
435
436                 let tag = it.kind().tag();
437                 let relevance = display_relevance(it.relevance());
438                 items.push(format!("{} {} {}\n", tag, it.label(), relevance));
439
440                 if let Some((mutability, relevance)) = it.ref_match() {
441                     let label = format!("&{}{}", mutability.as_keyword_for_ref(), it.label());
442                     let relevance = display_relevance(relevance);
443
444                     items.push(format!("{} {} {}\n", tag, label, relevance));
445                 }
446
447                 items
448             })
449             .collect::<String>();
450
451         expect.assert_eq(&actual);
452
453         fn display_relevance(relevance: CompletionRelevance) -> String {
454             let relevance_factors = vec![
455                 (relevance.type_match == Some(CompletionRelevanceTypeMatch::Exact), "type"),
456                 (
457                     relevance.type_match == Some(CompletionRelevanceTypeMatch::CouldUnify),
458                     "type_could_unify",
459                 ),
460                 (relevance.exact_name_match, "name"),
461                 (relevance.is_local, "local"),
462                 (
463                     relevance.postfix_match == Some(CompletionRelevancePostfixMatch::Exact),
464                     "snippet",
465                 ),
466                 (relevance.is_op_method, "op_method"),
467                 (relevance.requires_import, "requires_import"),
468             ]
469             .into_iter()
470             .filter_map(|(cond, desc)| if cond { Some(desc) } else { None })
471             .join("+");
472
473             format!("[{}]", relevance_factors)
474         }
475     }
476
477     #[test]
478     fn enum_detail_includes_record_fields() {
479         check(
480             r#"
481 enum Foo { Foo { x: i32, y: i32 } }
482
483 fn main() { Foo::Fo$0 }
484 "#,
485             SymbolKind::Variant,
486             expect![[r#"
487                 [
488                     CompletionItem {
489                         label: "Foo {…}",
490                         source_range: 54..56,
491                         delete: 54..56,
492                         insert: "Foo { x: ${1:()}, y: ${2:()} }$0",
493                         kind: SymbolKind(
494                             Variant,
495                         ),
496                         detail: "Foo { x: i32, y: i32 }",
497                     },
498                 ]
499             "#]],
500         );
501     }
502
503     #[test]
504     fn enum_detail_includes_tuple_fields() {
505         check(
506             r#"
507 enum Foo { Foo (i32, i32) }
508
509 fn main() { Foo::Fo$0 }
510 "#,
511             SymbolKind::Variant,
512             expect![[r#"
513                 [
514                     CompletionItem {
515                         label: "Foo(…)",
516                         source_range: 46..48,
517                         delete: 46..48,
518                         insert: "Foo(${1:()}, ${2:()})$0",
519                         kind: SymbolKind(
520                             Variant,
521                         ),
522                         detail: "Foo(i32, i32)",
523                     },
524                 ]
525             "#]],
526         );
527     }
528
529     #[test]
530     fn fn_detail_includes_args_and_return_type() {
531         check(
532             r#"
533 fn foo<T>(a: u32, b: u32, t: T) -> (u32, T) { (a, t) }
534
535 fn main() { fo$0 }
536 "#,
537             SymbolKind::Function,
538             expect![[r#"
539                 [
540                     CompletionItem {
541                         label: "foo(…)",
542                         source_range: 68..70,
543                         delete: 68..70,
544                         insert: "foo(${1:a}, ${2:b}, ${3:t})$0",
545                         kind: SymbolKind(
546                             Function,
547                         ),
548                         lookup: "foo",
549                         detail: "fn(u32, u32, T) -> (u32, T)",
550                         trigger_call_info: true,
551                     },
552                     CompletionItem {
553                         label: "main()",
554                         source_range: 68..70,
555                         delete: 68..70,
556                         insert: "main()$0",
557                         kind: SymbolKind(
558                             Function,
559                         ),
560                         lookup: "main",
561                         detail: "fn()",
562                     },
563                 ]
564             "#]],
565         );
566     }
567
568     #[test]
569     fn enum_detail_just_name_for_unit() {
570         check(
571             r#"
572 enum Foo { Foo }
573
574 fn main() { Foo::Fo$0 }
575 "#,
576             SymbolKind::Variant,
577             expect![[r#"
578                 [
579                     CompletionItem {
580                         label: "Foo",
581                         source_range: 35..37,
582                         delete: 35..37,
583                         insert: "Foo$0",
584                         kind: SymbolKind(
585                             Variant,
586                         ),
587                         detail: "Foo",
588                     },
589                 ]
590             "#]],
591         );
592     }
593
594     #[test]
595     fn lookup_enums_by_two_qualifiers() {
596         check_kinds(
597             r#"
598 mod m {
599     pub enum Spam { Foo, Bar(i32) }
600 }
601 fn main() { let _: m::Spam = S$0 }
602 "#,
603             &[
604                 CompletionItemKind::SymbolKind(SymbolKind::Function),
605                 CompletionItemKind::SymbolKind(SymbolKind::Module),
606                 CompletionItemKind::SymbolKind(SymbolKind::Variant),
607             ],
608             expect![[r#"
609                 [
610                     CompletionItem {
611                         label: "main()",
612                         source_range: 75..76,
613                         delete: 75..76,
614                         insert: "main()$0",
615                         kind: SymbolKind(
616                             Function,
617                         ),
618                         lookup: "main",
619                         detail: "fn()",
620                     },
621                     CompletionItem {
622                         label: "m",
623                         source_range: 75..76,
624                         delete: 75..76,
625                         insert: "m",
626                         kind: SymbolKind(
627                             Module,
628                         ),
629                     },
630                     CompletionItem {
631                         label: "m::Spam::Bar(…)",
632                         source_range: 75..76,
633                         delete: 75..76,
634                         insert: "m::Spam::Bar(${1:()})$0",
635                         kind: SymbolKind(
636                             Variant,
637                         ),
638                         lookup: "Spam::Bar(…)",
639                         detail: "m::Spam::Bar(i32)",
640                         relevance: CompletionRelevance {
641                             exact_name_match: false,
642                             type_match: Some(
643                                 Exact,
644                             ),
645                             is_local: false,
646                             is_item_from_trait: false,
647                             is_name_already_imported: false,
648                             requires_import: false,
649                             is_op_method: false,
650                             is_private_editable: false,
651                             postfix_match: None,
652                             is_definite: false,
653                         },
654                     },
655                     CompletionItem {
656                         label: "m::Spam::Foo",
657                         source_range: 75..76,
658                         delete: 75..76,
659                         insert: "m::Spam::Foo$0",
660                         kind: SymbolKind(
661                             Variant,
662                         ),
663                         lookup: "Spam::Foo",
664                         detail: "m::Spam::Foo",
665                         relevance: CompletionRelevance {
666                             exact_name_match: false,
667                             type_match: Some(
668                                 Exact,
669                             ),
670                             is_local: false,
671                             is_item_from_trait: false,
672                             is_name_already_imported: false,
673                             requires_import: false,
674                             is_op_method: false,
675                             is_private_editable: false,
676                             postfix_match: None,
677                             is_definite: false,
678                         },
679                     },
680                 ]
681             "#]],
682         )
683     }
684
685     #[test]
686     fn sets_deprecated_flag_in_items() {
687         check(
688             r#"
689 #[deprecated]
690 fn something_deprecated() {}
691
692 fn main() { som$0 }
693 "#,
694             SymbolKind::Function,
695             expect![[r#"
696                 [
697                     CompletionItem {
698                         label: "main()",
699                         source_range: 56..59,
700                         delete: 56..59,
701                         insert: "main()$0",
702                         kind: SymbolKind(
703                             Function,
704                         ),
705                         lookup: "main",
706                         detail: "fn()",
707                     },
708                     CompletionItem {
709                         label: "something_deprecated()",
710                         source_range: 56..59,
711                         delete: 56..59,
712                         insert: "something_deprecated()$0",
713                         kind: SymbolKind(
714                             Function,
715                         ),
716                         lookup: "something_deprecated",
717                         detail: "fn()",
718                         deprecated: true,
719                     },
720                 ]
721             "#]],
722         );
723
724         check(
725             r#"
726 struct A { #[deprecated] the_field: u32 }
727 fn foo() { A { the$0 } }
728 "#,
729             SymbolKind::Field,
730             expect![[r#"
731                 [
732                     CompletionItem {
733                         label: "the_field",
734                         source_range: 57..60,
735                         delete: 57..60,
736                         insert: "the_field",
737                         kind: SymbolKind(
738                             Field,
739                         ),
740                         detail: "u32",
741                         deprecated: true,
742                         relevance: CompletionRelevance {
743                             exact_name_match: false,
744                             type_match: Some(
745                                 CouldUnify,
746                             ),
747                             is_local: false,
748                             is_item_from_trait: false,
749                             is_name_already_imported: false,
750                             requires_import: false,
751                             is_op_method: false,
752                             is_private_editable: false,
753                             postfix_match: None,
754                             is_definite: false,
755                         },
756                     },
757                 ]
758             "#]],
759         );
760     }
761
762     #[test]
763     fn renders_docs() {
764         check_kinds(
765             r#"
766 struct S {
767     /// Field docs
768     foo:
769 }
770 impl S {
771     /// Method docs
772     fn bar(self) { self.$0 }
773 }"#,
774             &[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
775             expect![[r#"
776                 [
777                     CompletionItem {
778                         label: "bar()",
779                         source_range: 94..94,
780                         delete: 94..94,
781                         insert: "bar()$0",
782                         kind: Method,
783                         lookup: "bar",
784                         detail: "fn(self)",
785                         documentation: Documentation(
786                             "Method docs",
787                         ),
788                     },
789                     CompletionItem {
790                         label: "foo",
791                         source_range: 94..94,
792                         delete: 94..94,
793                         insert: "foo",
794                         kind: SymbolKind(
795                             Field,
796                         ),
797                         detail: "{unknown}",
798                         documentation: Documentation(
799                             "Field docs",
800                         ),
801                     },
802                 ]
803             "#]],
804         );
805
806         check_kinds(
807             r#"
808 use self::my$0;
809
810 /// mod docs
811 mod my { }
812
813 /// enum docs
814 enum E {
815     /// variant docs
816     V
817 }
818 use self::E::*;
819 "#,
820             &[
821                 CompletionItemKind::SymbolKind(SymbolKind::Module),
822                 CompletionItemKind::SymbolKind(SymbolKind::Variant),
823                 CompletionItemKind::SymbolKind(SymbolKind::Enum),
824             ],
825             expect![[r#"
826                 [
827                     CompletionItem {
828                         label: "my",
829                         source_range: 10..12,
830                         delete: 10..12,
831                         insert: "my",
832                         kind: SymbolKind(
833                             Module,
834                         ),
835                         documentation: Documentation(
836                             "mod docs",
837                         ),
838                     },
839                     CompletionItem {
840                         label: "V",
841                         source_range: 10..12,
842                         delete: 10..12,
843                         insert: "V$0",
844                         kind: SymbolKind(
845                             Variant,
846                         ),
847                         detail: "V",
848                         documentation: Documentation(
849                             "variant docs",
850                         ),
851                     },
852                     CompletionItem {
853                         label: "E",
854                         source_range: 10..12,
855                         delete: 10..12,
856                         insert: "E",
857                         kind: SymbolKind(
858                             Enum,
859                         ),
860                         documentation: Documentation(
861                             "enum docs",
862                         ),
863                     },
864                 ]
865             "#]],
866         )
867     }
868
869     #[test]
870     fn dont_render_attrs() {
871         check(
872             r#"
873 struct S;
874 impl S {
875     #[inline]
876     fn the_method(&self) { }
877 }
878 fn foo(s: S) { s.$0 }
879 "#,
880             CompletionItemKind::Method,
881             expect![[r#"
882                 [
883                     CompletionItem {
884                         label: "the_method()",
885                         source_range: 81..81,
886                         delete: 81..81,
887                         insert: "the_method()$0",
888                         kind: Method,
889                         lookup: "the_method",
890                         detail: "fn(&self)",
891                     },
892                 ]
893             "#]],
894         )
895     }
896
897     #[test]
898     fn no_call_parens_if_fn_ptr_needed() {
899         cov_mark::check!(no_call_parens_if_fn_ptr_needed);
900         check_edit(
901             "foo",
902             r#"
903 fn foo(foo: u8, bar: u8) {}
904 struct ManualVtable { f: fn(u8, u8) }
905
906 fn main() -> ManualVtable {
907     ManualVtable { f: f$0 }
908 }
909 "#,
910             r#"
911 fn foo(foo: u8, bar: u8) {}
912 struct ManualVtable { f: fn(u8, u8) }
913
914 fn main() -> ManualVtable {
915     ManualVtable { f: foo }
916 }
917 "#,
918         );
919         check_edit(
920             "type",
921             r#"
922 struct RawIdentTable { r#type: u32 }
923
924 fn main() -> RawIdentTable {
925     RawIdentTable { t$0: 42 }
926 }
927 "#,
928             r#"
929 struct RawIdentTable { r#type: u32 }
930
931 fn main() -> RawIdentTable {
932     RawIdentTable { r#type: 42 }
933 }
934 "#,
935         );
936     }
937
938     #[test]
939     fn no_parens_in_use_item() {
940         cov_mark::check!(no_parens_in_use_item);
941         check_edit(
942             "foo",
943             r#"
944 mod m { pub fn foo() {} }
945 use crate::m::f$0;
946 "#,
947             r#"
948 mod m { pub fn foo() {} }
949 use crate::m::foo;
950 "#,
951         );
952     }
953
954     #[test]
955     fn no_parens_in_call() {
956         check_edit(
957             "foo",
958             r#"
959 fn foo(x: i32) {}
960 fn main() { f$0(); }
961 "#,
962             r#"
963 fn foo(x: i32) {}
964 fn main() { foo(); }
965 "#,
966         );
967         check_edit(
968             "foo",
969             r#"
970 struct Foo;
971 impl Foo { fn foo(&self){} }
972 fn f(foo: &Foo) { foo.f$0(); }
973 "#,
974             r#"
975 struct Foo;
976 impl Foo { fn foo(&self){} }
977 fn f(foo: &Foo) { foo.foo(); }
978 "#,
979         );
980     }
981
982     #[test]
983     fn inserts_angle_brackets_for_generics() {
984         cov_mark::check!(inserts_angle_brackets_for_generics);
985         check_edit(
986             "Vec",
987             r#"
988 struct Vec<T> {}
989 fn foo(xs: Ve$0)
990 "#,
991             r#"
992 struct Vec<T> {}
993 fn foo(xs: Vec<$0>)
994 "#,
995         );
996         check_edit(
997             "Vec",
998             r#"
999 type Vec<T> = (T,);
1000 fn foo(xs: Ve$0)
1001 "#,
1002             r#"
1003 type Vec<T> = (T,);
1004 fn foo(xs: Vec<$0>)
1005 "#,
1006         );
1007         check_edit(
1008             "Vec",
1009             r#"
1010 struct Vec<T = i128> {}
1011 fn foo(xs: Ve$0)
1012 "#,
1013             r#"
1014 struct Vec<T = i128> {}
1015 fn foo(xs: Vec)
1016 "#,
1017         );
1018         check_edit(
1019             "Vec",
1020             r#"
1021 struct Vec<T> {}
1022 fn foo(xs: Ve$0<i128>)
1023 "#,
1024             r#"
1025 struct Vec<T> {}
1026 fn foo(xs: Vec<i128>)
1027 "#,
1028         );
1029     }
1030
1031     #[test]
1032     fn active_param_relevance() {
1033         check_relevance(
1034             r#"
1035 struct S { foo: i64, bar: u32, baz: u32 }
1036 fn test(bar: u32) { }
1037 fn foo(s: S) { test(s.$0) }
1038 "#,
1039             expect![[r#"
1040                 fd bar [type+name]
1041                 fd baz [type]
1042                 fd foo []
1043             "#]],
1044         );
1045     }
1046
1047     #[test]
1048     fn record_field_relevances() {
1049         check_relevance(
1050             r#"
1051 struct A { foo: i64, bar: u32, baz: u32 }
1052 struct B { x: (), y: f32, bar: u32 }
1053 fn foo(a: A) { B { bar: a.$0 }; }
1054 "#,
1055             expect![[r#"
1056                 fd bar [type+name]
1057                 fd baz [type]
1058                 fd foo []
1059             "#]],
1060         )
1061     }
1062
1063     #[test]
1064     fn record_field_and_call_relevances() {
1065         check_relevance(
1066             r#"
1067 struct A { foo: i64, bar: u32, baz: u32 }
1068 struct B { x: (), y: f32, bar: u32 }
1069 fn f(foo: i64) {  }
1070 fn foo(a: A) { B { bar: f(a.$0) }; }
1071 "#,
1072             expect![[r#"
1073                 fd foo [type+name]
1074                 fd bar []
1075                 fd baz []
1076             "#]],
1077         );
1078         check_relevance(
1079             r#"
1080 struct A { foo: i64, bar: u32, baz: u32 }
1081 struct B { x: (), y: f32, bar: u32 }
1082 fn f(foo: i64) {  }
1083 fn foo(a: A) { f(B { bar: a.$0 }); }
1084 "#,
1085             expect![[r#"
1086                 fd bar [type+name]
1087                 fd baz [type]
1088                 fd foo []
1089             "#]],
1090         );
1091     }
1092
1093     #[test]
1094     fn prioritize_exact_ref_match() {
1095         check_relevance(
1096             r#"
1097 struct WorldSnapshot { _f: () };
1098 fn go(world: &WorldSnapshot) { go(w$0) }
1099 "#,
1100             expect![[r#"
1101                 lc world [type+name+local]
1102                 st WorldSnapshot {…} []
1103                 st &WorldSnapshot {…} [type]
1104                 st WorldSnapshot []
1105                 fn go(…) []
1106             "#]],
1107         );
1108     }
1109
1110     #[test]
1111     fn too_many_arguments() {
1112         cov_mark::check!(too_many_arguments);
1113         check_relevance(
1114             r#"
1115 struct Foo;
1116 fn f(foo: &Foo) { f(foo, w$0) }
1117 "#,
1118             expect![[r#"
1119                 lc foo [local]
1120                 st Foo []
1121                 fn f(…) []
1122             "#]],
1123         );
1124     }
1125
1126     #[test]
1127     fn score_fn_type_and_name_match() {
1128         check_relevance(
1129             r#"
1130 struct A { bar: u8 }
1131 fn baz() -> u8 { 0 }
1132 fn bar() -> u8 { 0 }
1133 fn f() { A { bar: b$0 }; }
1134 "#,
1135             expect![[r#"
1136                 fn bar() [type+name]
1137                 fn baz() [type]
1138                 st A []
1139                 fn f() []
1140             "#]],
1141         );
1142     }
1143
1144     #[test]
1145     fn score_method_type_and_name_match() {
1146         check_relevance(
1147             r#"
1148 fn baz(aaa: u32){}
1149 struct Foo;
1150 impl Foo {
1151 fn aaa(&self) -> u32 { 0 }
1152 fn bbb(&self) -> u32 { 0 }
1153 fn ccc(&self) -> u64 { 0 }
1154 }
1155 fn f() {
1156     baz(Foo.$0
1157 }
1158 "#,
1159             expect![[r#"
1160                 me aaa() [type+name]
1161                 me bbb() [type]
1162                 me ccc() []
1163             "#]],
1164         );
1165     }
1166
1167     #[test]
1168     fn score_method_name_match_only() {
1169         check_relevance(
1170             r#"
1171 fn baz(aaa: u32){}
1172 struct Foo;
1173 impl Foo {
1174 fn aaa(&self) -> u64 { 0 }
1175 }
1176 fn f() {
1177     baz(Foo.$0
1178 }
1179 "#,
1180             expect![[r#"
1181                 me aaa() [name]
1182             "#]],
1183         );
1184     }
1185
1186     #[test]
1187     fn suggest_ref_mut() {
1188         cov_mark::check!(suggest_ref);
1189         check_relevance(
1190             r#"
1191 struct S;
1192 fn foo(s: &mut S) {}
1193 fn main() {
1194     let mut s = S;
1195     foo($0);
1196 }
1197             "#,
1198             expect![[r#"
1199                 lc s [name+local]
1200                 lc &mut s [type+name+local]
1201                 st S []
1202                 st &mut S [type]
1203                 st S []
1204                 fn main() []
1205                 fn foo(…) []
1206             "#]],
1207         );
1208         check_relevance(
1209             r#"
1210 struct S;
1211 fn foo(s: &mut S) {}
1212 fn main() {
1213     let mut s = S;
1214     foo(&mut $0);
1215 }
1216             "#,
1217             expect![[r#"
1218                 lc s [type+name+local]
1219                 st S [type]
1220                 st S []
1221                 fn main() []
1222                 fn foo(…) []
1223             "#]],
1224         );
1225         check_relevance(
1226             r#"
1227 struct S;
1228 fn foo(s: &mut S) {}
1229 fn main() {
1230     let mut ssss = S;
1231     foo(&mut s$0);
1232 }
1233             "#,
1234             expect![[r#"
1235                 lc ssss [type+local]
1236                 st S [type]
1237                 st S []
1238                 fn main() []
1239                 fn foo(…) []
1240             "#]],
1241         );
1242     }
1243
1244     #[test]
1245     fn suggest_deref() {
1246         check_relevance(
1247             r#"
1248 //- minicore: deref
1249 struct S;
1250 struct T(S);
1251
1252 impl core::ops::Deref for T {
1253     type Target = S;
1254
1255     fn deref(&self) -> &Self::Target {
1256         &self.0
1257     }
1258 }
1259
1260 fn foo(s: &S) {}
1261
1262 fn main() {
1263     let t = T(S);
1264     let m = 123;
1265
1266     foo($0);
1267 }
1268             "#,
1269             expect![[r#"
1270                 lc m [local]
1271                 lc t [local]
1272                 lc &t [type+local]
1273                 st S []
1274                 st &S [type]
1275                 st T []
1276                 st S []
1277                 fn main() []
1278                 fn foo(…) []
1279                 md core []
1280                 tt Sized []
1281             "#]],
1282         )
1283     }
1284
1285     #[test]
1286     fn suggest_deref_mut() {
1287         check_relevance(
1288             r#"
1289 //- minicore: deref_mut
1290 struct S;
1291 struct T(S);
1292
1293 impl core::ops::Deref for T {
1294     type Target = S;
1295
1296     fn deref(&self) -> &Self::Target {
1297         &self.0
1298     }
1299 }
1300
1301 impl core::ops::DerefMut for T {
1302     fn deref_mut(&mut self) -> &mut Self::Target {
1303         &mut self.0
1304     }
1305 }
1306
1307 fn foo(s: &mut S) {}
1308
1309 fn main() {
1310     let t = T(S);
1311     let m = 123;
1312
1313     foo($0);
1314 }
1315             "#,
1316             expect![[r#"
1317                 lc m [local]
1318                 lc t [local]
1319                 lc &mut t [type+local]
1320                 st S []
1321                 st &mut S [type]
1322                 st T []
1323                 st S []
1324                 fn main() []
1325                 fn foo(…) []
1326                 md core []
1327                 tt Sized []
1328             "#]],
1329         )
1330     }
1331
1332     #[test]
1333     fn locals() {
1334         check_relevance(
1335             r#"
1336 fn foo(bar: u32) {
1337     let baz = 0;
1338
1339     f$0
1340 }
1341 "#,
1342             expect![[r#"
1343                 lc baz [local]
1344                 lc bar [local]
1345                 fn foo(…) []
1346             "#]],
1347         );
1348     }
1349
1350     #[test]
1351     fn enum_owned() {
1352         check_relevance(
1353             r#"
1354 enum Foo { A, B }
1355 fn foo() {
1356     bar($0);
1357 }
1358 fn bar(t: Foo) {}
1359 "#,
1360             expect![[r#"
1361                 ev Foo::A [type]
1362                 ev Foo::B [type]
1363                 en Foo []
1364                 fn bar(…) []
1365                 fn foo() []
1366             "#]],
1367         );
1368     }
1369
1370     #[test]
1371     fn enum_ref() {
1372         check_relevance(
1373             r#"
1374 enum Foo { A, B }
1375 fn foo() {
1376     bar($0);
1377 }
1378 fn bar(t: &Foo) {}
1379 "#,
1380             expect![[r#"
1381                 ev Foo::A []
1382                 ev &Foo::A [type]
1383                 ev Foo::B []
1384                 ev &Foo::B [type]
1385                 en Foo []
1386                 fn bar(…) []
1387                 fn foo() []
1388             "#]],
1389         );
1390     }
1391
1392     #[test]
1393     fn suggest_deref_fn_ret() {
1394         check_relevance(
1395             r#"
1396 //- minicore: deref
1397 struct S;
1398 struct T(S);
1399
1400 impl core::ops::Deref for T {
1401     type Target = S;
1402
1403     fn deref(&self) -> &Self::Target {
1404         &self.0
1405     }
1406 }
1407
1408 fn foo(s: &S) {}
1409 fn bar() -> T {}
1410
1411 fn main() {
1412     foo($0);
1413 }
1414 "#,
1415             expect![[r#"
1416                 st S []
1417                 st &S [type]
1418                 st T []
1419                 st S []
1420                 fn main() []
1421                 fn bar() []
1422                 fn &bar() [type]
1423                 fn foo(…) []
1424                 md core []
1425                 tt Sized []
1426             "#]],
1427         )
1428     }
1429
1430     #[test]
1431     fn op_function_relevances() {
1432         check_relevance(
1433             r#"
1434 #[lang = "sub"]
1435 trait Sub {
1436     fn sub(self, other: Self) -> Self { self }
1437 }
1438 impl Sub for u32 {}
1439 fn foo(a: u32) { a.$0 }
1440 "#,
1441             expect![[r#"
1442                 me sub(…) (as Sub) [op_method]
1443             "#]],
1444         );
1445         check_relevance(
1446             r#"
1447 struct Foo;
1448 impl Foo {
1449     fn new() -> Self {}
1450 }
1451 #[lang = "eq"]
1452 pub trait PartialEq<Rhs: ?Sized = Self> {
1453     fn eq(&self, other: &Rhs) -> bool;
1454     fn ne(&self, other: &Rhs) -> bool;
1455 }
1456
1457 impl PartialEq for Foo {}
1458 fn main() {
1459     Foo::$0
1460 }
1461 "#,
1462             expect![[r#"
1463                 fn new() []
1464                 me eq(…) (as PartialEq) [op_method]
1465                 me ne(…) (as PartialEq) [op_method]
1466             "#]],
1467         );
1468     }
1469
1470     #[test]
1471     fn struct_field_method_ref() {
1472         check_kinds(
1473             r#"
1474 struct Foo { bar: u32 }
1475 impl Foo { fn baz(&self) -> u32 { 0 } }
1476
1477 fn foo(f: Foo) { let _: &u32 = f.b$0 }
1478 "#,
1479             &[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
1480             // FIXME
1481             // Ideally we'd also suggest &f.bar and &f.baz() as exact
1482             // type matches. See #8058.
1483             expect![[r#"
1484                 [
1485                     CompletionItem {
1486                         label: "baz()",
1487                         source_range: 98..99,
1488                         delete: 98..99,
1489                         insert: "baz()$0",
1490                         kind: Method,
1491                         lookup: "baz",
1492                         detail: "fn(&self) -> u32",
1493                     },
1494                     CompletionItem {
1495                         label: "bar",
1496                         source_range: 98..99,
1497                         delete: 98..99,
1498                         insert: "bar",
1499                         kind: SymbolKind(
1500                             Field,
1501                         ),
1502                         detail: "u32",
1503                     },
1504                 ]
1505             "#]],
1506         );
1507     }
1508
1509     #[test]
1510     fn qualified_path_ref() {
1511         // disabled right now because it doesn't render correctly, #8058
1512         check_kinds(
1513             r#"
1514 struct S;
1515
1516 struct T;
1517 impl T {
1518     fn foo() -> S {}
1519 }
1520
1521 fn bar(s: &S) {}
1522
1523 fn main() {
1524     bar(T::$0);
1525 }
1526 "#,
1527             &[CompletionItemKind::SymbolKind(SymbolKind::Function)],
1528             expect![[r#"
1529                 [
1530                     CompletionItem {
1531                         label: "foo()",
1532                         source_range: 95..95,
1533                         delete: 95..95,
1534                         insert: "foo()$0",
1535                         kind: SymbolKind(
1536                             Function,
1537                         ),
1538                         lookup: "foo",
1539                         detail: "fn() -> S",
1540                     },
1541                 ]
1542             "#]],
1543         );
1544     }
1545
1546     #[test]
1547     fn generic_enum() {
1548         check_relevance(
1549             r#"
1550 enum Foo<T> { A(T), B }
1551 // bar() should not be an exact type match
1552 // because the generic parameters are different
1553 fn bar() -> Foo<u8> { Foo::B }
1554 // FIXME baz() should be an exact type match
1555 // because the types could unify, but it currently
1556 // is not. This is due to the T here being
1557 // TyKind::Placeholder rather than TyKind::Missing.
1558 fn baz<T>() -> Foo<T> { Foo::B }
1559 fn foo() {
1560     let foo: Foo<u32> = Foo::B;
1561     let _: Foo<u32> = f$0;
1562 }
1563 "#,
1564             expect![[r#"
1565                 lc foo [type+local]
1566                 ev Foo::A(…) [type_could_unify]
1567                 ev Foo::B [type_could_unify]
1568                 fn foo() []
1569                 en Foo []
1570                 fn baz() []
1571                 fn bar() []
1572             "#]],
1573         );
1574     }
1575
1576     #[test]
1577     fn postfix_exact_match_is_high_priority() {
1578         cov_mark::check!(postfix_exact_match_is_high_priority);
1579         check_relevance_for_kinds(
1580             r#"
1581 mod ops {
1582     pub trait Not {
1583         type Output;
1584         fn not(self) -> Self::Output;
1585     }
1586
1587     impl Not for bool {
1588         type Output = bool;
1589         fn not(self) -> bool { if self { false } else { true }}
1590     }
1591 }
1592
1593 fn main() {
1594     let _: bool = (9 > 2).not$0;
1595 }
1596     "#,
1597             &[CompletionItemKind::Snippet, CompletionItemKind::Method],
1598             expect![[r#"
1599                 sn not [snippet]
1600                 me not() (use ops::Not) [type_could_unify+requires_import]
1601                 sn if []
1602                 sn while []
1603                 sn ref []
1604                 sn refm []
1605                 sn match []
1606                 sn box []
1607                 sn dbg []
1608                 sn dbgr []
1609                 sn call []
1610             "#]],
1611         );
1612     }
1613
1614     #[test]
1615     fn postfix_inexact_match_is_low_priority() {
1616         cov_mark::check!(postfix_inexact_match_is_low_priority);
1617         check_relevance_for_kinds(
1618             r#"
1619 struct S;
1620 impl S {
1621     fn f(&self) {}
1622 }
1623 fn main() {
1624     S.$0
1625 }
1626     "#,
1627             &[CompletionItemKind::Snippet, CompletionItemKind::Method],
1628             expect![[r#"
1629                 me f() []
1630                 sn ref []
1631                 sn refm []
1632                 sn match []
1633                 sn box []
1634                 sn dbg []
1635                 sn dbgr []
1636                 sn call []
1637                 sn let []
1638                 sn letm []
1639             "#]],
1640         );
1641     }
1642
1643     #[test]
1644     fn flyimport_reduced_relevance() {
1645         check_relevance(
1646             r#"
1647 mod std {
1648     pub mod io {
1649         pub trait BufRead {}
1650         pub struct BufReader;
1651         pub struct BufWriter;
1652     }
1653 }
1654 struct Buffer;
1655
1656 fn f() {
1657     Buf$0
1658 }
1659 "#,
1660             expect![[r#"
1661                 md std []
1662                 st Buffer []
1663                 fn f() []
1664                 tt BufRead (use std::io::BufRead) [requires_import]
1665                 st BufReader (use std::io::BufReader) [requires_import]
1666                 st BufWriter (use std::io::BufWriter) [requires_import]
1667             "#]],
1668         );
1669     }
1670 }