]> git.lizzy.rs Git - rust.git/blob - crates/ide-completion/src/render.rs
fix: visibility completion
[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                 fn go(…) []
1104             "#]],
1105         );
1106     }
1107
1108     #[test]
1109     fn too_many_arguments() {
1110         cov_mark::check!(too_many_arguments);
1111         check_relevance(
1112             r#"
1113 struct Foo;
1114 fn f(foo: &Foo) { f(foo, w$0) }
1115 "#,
1116             expect![[r#"
1117                 lc foo [local]
1118                 st Foo []
1119                 fn f(…) []
1120             "#]],
1121         );
1122     }
1123
1124     #[test]
1125     fn score_fn_type_and_name_match() {
1126         check_relevance(
1127             r#"
1128 struct A { bar: u8 }
1129 fn baz() -> u8 { 0 }
1130 fn bar() -> u8 { 0 }
1131 fn f() { A { bar: b$0 }; }
1132 "#,
1133             expect![[r#"
1134                 fn bar() [type+name]
1135                 fn baz() [type]
1136                 st A []
1137                 fn f() []
1138             "#]],
1139         );
1140     }
1141
1142     #[test]
1143     fn score_method_type_and_name_match() {
1144         check_relevance(
1145             r#"
1146 fn baz(aaa: u32){}
1147 struct Foo;
1148 impl Foo {
1149 fn aaa(&self) -> u32 { 0 }
1150 fn bbb(&self) -> u32 { 0 }
1151 fn ccc(&self) -> u64 { 0 }
1152 }
1153 fn f() {
1154     baz(Foo.$0
1155 }
1156 "#,
1157             expect![[r#"
1158                 me aaa() [type+name]
1159                 me bbb() [type]
1160                 me ccc() []
1161             "#]],
1162         );
1163     }
1164
1165     #[test]
1166     fn score_method_name_match_only() {
1167         check_relevance(
1168             r#"
1169 fn baz(aaa: u32){}
1170 struct Foo;
1171 impl Foo {
1172 fn aaa(&self) -> u64 { 0 }
1173 }
1174 fn f() {
1175     baz(Foo.$0
1176 }
1177 "#,
1178             expect![[r#"
1179                 me aaa() [name]
1180             "#]],
1181         );
1182     }
1183
1184     #[test]
1185     fn suggest_ref_mut() {
1186         cov_mark::check!(suggest_ref);
1187         check_relevance(
1188             r#"
1189 struct S;
1190 fn foo(s: &mut S) {}
1191 fn main() {
1192     let mut s = S;
1193     foo($0);
1194 }
1195             "#,
1196             expect![[r#"
1197                 lc s [name+local]
1198                 lc &mut s [type+name+local]
1199                 st S []
1200                 fn main() []
1201                 fn foo(…) []
1202             "#]],
1203         );
1204         check_relevance(
1205             r#"
1206 struct S;
1207 fn foo(s: &mut S) {}
1208 fn main() {
1209     let mut s = S;
1210     foo(&mut $0);
1211 }
1212             "#,
1213             expect![[r#"
1214                 lc s [type+name+local]
1215                 st S [type]
1216                 st S []
1217                 fn main() []
1218                 fn foo(…) []
1219             "#]],
1220         );
1221         check_relevance(
1222             r#"
1223 struct S;
1224 fn foo(s: &mut S) {}
1225 fn main() {
1226     let mut ssss = S;
1227     foo(&mut s$0);
1228 }
1229             "#,
1230             expect![[r#"
1231                 lc ssss [type+local]
1232                 st S [type]
1233                 st S []
1234                 fn main() []
1235                 fn foo(…) []
1236             "#]],
1237         );
1238     }
1239
1240     #[test]
1241     fn suggest_deref() {
1242         check_relevance(
1243             r#"
1244 //- minicore: deref
1245 struct S;
1246 struct T(S);
1247
1248 impl core::ops::Deref for T {
1249     type Target = S;
1250
1251     fn deref(&self) -> &Self::Target {
1252         &self.0
1253     }
1254 }
1255
1256 fn foo(s: &S) {}
1257
1258 fn main() {
1259     let t = T(S);
1260     let m = 123;
1261
1262     foo($0);
1263 }
1264             "#,
1265             expect![[r#"
1266                 lc m [local]
1267                 lc t [local]
1268                 lc &t [type+local]
1269                 st T []
1270                 st S []
1271                 fn main() []
1272                 fn foo(…) []
1273                 md core []
1274                 tt Sized []
1275             "#]],
1276         )
1277     }
1278
1279     #[test]
1280     fn suggest_deref_mut() {
1281         check_relevance(
1282             r#"
1283 //- minicore: deref_mut
1284 struct S;
1285 struct T(S);
1286
1287 impl core::ops::Deref for T {
1288     type Target = S;
1289
1290     fn deref(&self) -> &Self::Target {
1291         &self.0
1292     }
1293 }
1294
1295 impl core::ops::DerefMut for T {
1296     fn deref_mut(&mut self) -> &mut Self::Target {
1297         &mut self.0
1298     }
1299 }
1300
1301 fn foo(s: &mut S) {}
1302
1303 fn main() {
1304     let t = T(S);
1305     let m = 123;
1306
1307     foo($0);
1308 }
1309             "#,
1310             expect![[r#"
1311                 lc m [local]
1312                 lc t [local]
1313                 lc &mut t [type+local]
1314                 st T []
1315                 st S []
1316                 fn main() []
1317                 fn foo(…) []
1318                 md core []
1319                 tt Sized []
1320             "#]],
1321         )
1322     }
1323
1324     #[test]
1325     fn locals() {
1326         check_relevance(
1327             r#"
1328 fn foo(bar: u32) {
1329     let baz = 0;
1330
1331     f$0
1332 }
1333 "#,
1334             expect![[r#"
1335                 lc baz [local]
1336                 lc bar [local]
1337                 fn foo(…) []
1338             "#]],
1339         );
1340     }
1341
1342     #[test]
1343     fn enum_owned() {
1344         check_relevance(
1345             r#"
1346 enum Foo { A, B }
1347 fn foo() {
1348     bar($0);
1349 }
1350 fn bar(t: Foo) {}
1351 "#,
1352             expect![[r#"
1353                 ev Foo::A [type]
1354                 ev Foo::B [type]
1355                 en Foo []
1356                 fn bar(…) []
1357                 fn foo() []
1358             "#]],
1359         );
1360     }
1361
1362     #[test]
1363     fn enum_ref() {
1364         check_relevance(
1365             r#"
1366 enum Foo { A, B }
1367 fn foo() {
1368     bar($0);
1369 }
1370 fn bar(t: &Foo) {}
1371 "#,
1372             expect![[r#"
1373                 ev Foo::A []
1374                 ev &Foo::A [type]
1375                 ev Foo::B []
1376                 ev &Foo::B [type]
1377                 en Foo []
1378                 fn bar(…) []
1379                 fn foo() []
1380             "#]],
1381         );
1382     }
1383
1384     #[test]
1385     fn suggest_deref_fn_ret() {
1386         check_relevance(
1387             r#"
1388 //- minicore: deref
1389 struct S;
1390 struct T(S);
1391
1392 impl core::ops::Deref for T {
1393     type Target = S;
1394
1395     fn deref(&self) -> &Self::Target {
1396         &self.0
1397     }
1398 }
1399
1400 fn foo(s: &S) {}
1401 fn bar() -> T {}
1402
1403 fn main() {
1404     foo($0);
1405 }
1406 "#,
1407             expect![[r#"
1408                 st T []
1409                 st S []
1410                 fn main() []
1411                 fn bar() []
1412                 fn &bar() [type]
1413                 fn foo(…) []
1414                 md core []
1415                 tt Sized []
1416             "#]],
1417         )
1418     }
1419
1420     #[test]
1421     fn op_function_relevances() {
1422         check_relevance(
1423             r#"
1424 #[lang = "sub"]
1425 trait Sub {
1426     fn sub(self, other: Self) -> Self { self }
1427 }
1428 impl Sub for u32 {}
1429 fn foo(a: u32) { a.$0 }
1430 "#,
1431             expect![[r#"
1432                 me sub(…) (as Sub) [op_method]
1433             "#]],
1434         );
1435         check_relevance(
1436             r#"
1437 struct Foo;
1438 impl Foo {
1439     fn new() -> Self {}
1440 }
1441 #[lang = "eq"]
1442 pub trait PartialEq<Rhs: ?Sized = Self> {
1443     fn eq(&self, other: &Rhs) -> bool;
1444     fn ne(&self, other: &Rhs) -> bool;
1445 }
1446
1447 impl PartialEq for Foo {}
1448 fn main() {
1449     Foo::$0
1450 }
1451 "#,
1452             expect![[r#"
1453                 fn new() []
1454                 me eq(…) (as PartialEq) [op_method]
1455                 me ne(…) (as PartialEq) [op_method]
1456             "#]],
1457         );
1458     }
1459
1460     #[test]
1461     fn struct_field_method_ref() {
1462         check_kinds(
1463             r#"
1464 struct Foo { bar: u32 }
1465 impl Foo { fn baz(&self) -> u32 { 0 } }
1466
1467 fn foo(f: Foo) { let _: &u32 = f.b$0 }
1468 "#,
1469             &[CompletionItemKind::Method, CompletionItemKind::SymbolKind(SymbolKind::Field)],
1470             // FIXME
1471             // Ideally we'd also suggest &f.bar and &f.baz() as exact
1472             // type matches. See #8058.
1473             expect![[r#"
1474                 [
1475                     CompletionItem {
1476                         label: "baz()",
1477                         source_range: 98..99,
1478                         delete: 98..99,
1479                         insert: "baz()$0",
1480                         kind: Method,
1481                         lookup: "baz",
1482                         detail: "fn(&self) -> u32",
1483                     },
1484                     CompletionItem {
1485                         label: "bar",
1486                         source_range: 98..99,
1487                         delete: 98..99,
1488                         insert: "bar",
1489                         kind: SymbolKind(
1490                             Field,
1491                         ),
1492                         detail: "u32",
1493                     },
1494                 ]
1495             "#]],
1496         );
1497     }
1498
1499     #[test]
1500     fn qualified_path_ref() {
1501         // disabled right now because it doesn't render correctly, #8058
1502         check_kinds(
1503             r#"
1504 struct S;
1505
1506 struct T;
1507 impl T {
1508     fn foo() -> S {}
1509 }
1510
1511 fn bar(s: &S) {}
1512
1513 fn main() {
1514     bar(T::$0);
1515 }
1516 "#,
1517             &[CompletionItemKind::SymbolKind(SymbolKind::Function)],
1518             expect![[r#"
1519                 [
1520                     CompletionItem {
1521                         label: "foo()",
1522                         source_range: 95..95,
1523                         delete: 95..95,
1524                         insert: "foo()$0",
1525                         kind: SymbolKind(
1526                             Function,
1527                         ),
1528                         lookup: "foo",
1529                         detail: "fn() -> S",
1530                     },
1531                 ]
1532             "#]],
1533         );
1534     }
1535
1536     #[test]
1537     fn generic_enum() {
1538         check_relevance(
1539             r#"
1540 enum Foo<T> { A(T), B }
1541 // bar() should not be an exact type match
1542 // because the generic parameters are different
1543 fn bar() -> Foo<u8> { Foo::B }
1544 // FIXME baz() should be an exact type match
1545 // because the types could unify, but it currently
1546 // is not. This is due to the T here being
1547 // TyKind::Placeholder rather than TyKind::Missing.
1548 fn baz<T>() -> Foo<T> { Foo::B }
1549 fn foo() {
1550     let foo: Foo<u32> = Foo::B;
1551     let _: Foo<u32> = f$0;
1552 }
1553 "#,
1554             expect![[r#"
1555                 lc foo [type+local]
1556                 ev Foo::A(…) [type_could_unify]
1557                 ev Foo::B [type_could_unify]
1558                 fn foo() []
1559                 en Foo []
1560                 fn baz() []
1561                 fn bar() []
1562             "#]],
1563         );
1564     }
1565
1566     #[test]
1567     fn postfix_exact_match_is_high_priority() {
1568         cov_mark::check!(postfix_exact_match_is_high_priority);
1569         check_relevance_for_kinds(
1570             r#"
1571 mod ops {
1572     pub trait Not {
1573         type Output;
1574         fn not(self) -> Self::Output;
1575     }
1576
1577     impl Not for bool {
1578         type Output = bool;
1579         fn not(self) -> bool { if self { false } else { true }}
1580     }
1581 }
1582
1583 fn main() {
1584     let _: bool = (9 > 2).not$0;
1585 }
1586     "#,
1587             &[CompletionItemKind::Snippet, CompletionItemKind::Method],
1588             expect![[r#"
1589                 sn not [snippet]
1590                 me not() (use ops::Not) [type_could_unify+requires_import]
1591                 sn if []
1592                 sn while []
1593                 sn ref []
1594                 sn refm []
1595                 sn match []
1596                 sn box []
1597                 sn dbg []
1598                 sn dbgr []
1599                 sn call []
1600             "#]],
1601         );
1602     }
1603
1604     #[test]
1605     fn postfix_inexact_match_is_low_priority() {
1606         cov_mark::check!(postfix_inexact_match_is_low_priority);
1607         check_relevance_for_kinds(
1608             r#"
1609 struct S;
1610 impl S {
1611     fn f(&self) {}
1612 }
1613 fn main() {
1614     S.$0
1615 }
1616     "#,
1617             &[CompletionItemKind::Snippet, CompletionItemKind::Method],
1618             expect![[r#"
1619                 me f() []
1620                 sn ref []
1621                 sn refm []
1622                 sn match []
1623                 sn box []
1624                 sn dbg []
1625                 sn dbgr []
1626                 sn call []
1627                 sn let []
1628                 sn letm []
1629             "#]],
1630         );
1631     }
1632
1633     #[test]
1634     fn flyimport_reduced_relevance() {
1635         check_relevance(
1636             r#"
1637 mod std {
1638     pub mod io {
1639         pub trait BufRead {}
1640         pub struct BufReader;
1641         pub struct BufWriter;
1642     }
1643 }
1644 struct Buffer;
1645
1646 fn f() {
1647     Buf$0
1648 }
1649 "#,
1650             expect![[r#"
1651                 md std []
1652                 st Buffer []
1653                 fn f() []
1654                 tt BufRead (use std::io::BufRead) [requires_import]
1655                 st BufReader (use std::io::BufReader) [requires_import]
1656                 st BufWriter (use std::io::BufWriter) [requires_import]
1657             "#]],
1658         );
1659     }
1660 }