]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/hover.rs
Merge #7321
[rust.git] / crates / ide / src / hover.rs
1 use hir::{
2     Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
3     HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
4 };
5 use ide_db::base_db::SourceDatabase;
6 use ide_db::{
7     defs::{Definition, NameClass, NameRefClass},
8     RootDatabase,
9 };
10 use itertools::Itertools;
11 use stdx::format_to;
12 use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
13 use test_utils::mark;
14
15 use crate::{
16     display::{macro_label, ShortLabel, TryToNav},
17     doc_links::{remove_links, rewrite_links},
18     markdown_remove::remove_markdown,
19     markup::Markup,
20     runnables::{runnable_fn, runnable_mod},
21     FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
22 };
23
24 #[derive(Clone, Debug, PartialEq, Eq)]
25 pub struct HoverConfig {
26     pub implementations: bool,
27     pub run: bool,
28     pub debug: bool,
29     pub goto_type_def: bool,
30     pub links_in_hover: bool,
31     pub markdown: bool,
32 }
33
34 impl HoverConfig {
35     pub const NO_ACTIONS: Self = Self {
36         implementations: false,
37         run: false,
38         debug: false,
39         goto_type_def: false,
40         links_in_hover: true,
41         markdown: true,
42     };
43
44     pub fn any(&self) -> bool {
45         self.implementations || self.runnable() || self.goto_type_def
46     }
47
48     pub fn none(&self) -> bool {
49         !self.any()
50     }
51
52     pub fn runnable(&self) -> bool {
53         self.run || self.debug
54     }
55 }
56
57 #[derive(Debug, Clone)]
58 pub enum HoverAction {
59     Runnable(Runnable),
60     Implementation(FilePosition),
61     GoToType(Vec<HoverGotoTypeData>),
62 }
63
64 #[derive(Debug, Clone, Eq, PartialEq)]
65 pub struct HoverGotoTypeData {
66     pub mod_path: String,
67     pub nav: NavigationTarget,
68 }
69
70 /// Contains the results when hovering over an item
71 #[derive(Debug, Default)]
72 pub struct HoverResult {
73     pub markup: Markup,
74     pub actions: Vec<HoverAction>,
75 }
76
77 // Feature: Hover
78 //
79 // Shows additional information, like type of an expression or documentation for definition when "focusing" code.
80 // Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.
81 pub(crate) fn hover(
82     db: &RootDatabase,
83     position: FilePosition,
84     links_in_hover: bool,
85     markdown: bool,
86 ) -> Option<RangeInfo<HoverResult>> {
87     let sema = Semantics::new(db);
88     let file = sema.parse(position.file_id).syntax().clone();
89     let token = pick_best(file.token_at_offset(position.offset))?;
90     let token = sema.descend_into_macros(token);
91
92     let mut res = HoverResult::default();
93
94     let node = token.parent();
95     let definition = match_ast! {
96         match node {
97             ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)),
98             ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
99             ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
100                 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
101             _ => None,
102         }
103     };
104     if let Some(definition) = definition {
105         if let Some(markup) = hover_for_definition(db, definition) {
106             let markup = markup.as_str();
107             let markup = if !markdown {
108                 remove_markdown(markup)
109             } else if links_in_hover {
110                 rewrite_links(db, markup, &definition)
111             } else {
112                 remove_links(markup)
113             };
114             res.markup = Markup::from(markup);
115             if let Some(action) = show_implementations_action(db, definition) {
116                 res.actions.push(action);
117             }
118
119             if let Some(action) = runnable_action(&sema, definition, position.file_id) {
120                 res.actions.push(action);
121             }
122
123             if let Some(action) = goto_type_action(db, definition) {
124                 res.actions.push(action);
125             }
126
127             let range = sema.original_range(&node).range;
128             return Some(RangeInfo::new(range, res));
129         }
130     }
131
132     if token.kind() == syntax::SyntaxKind::COMMENT {
133         // don't highlight the entire parent node on comment hover
134         return None;
135     }
136
137     let node = token
138         .ancestors()
139         .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
140
141     let ty = match_ast! {
142         match node {
143             ast::Expr(it) => sema.type_of_expr(&it)?,
144             ast::Pat(it) => sema.type_of_pat(&it)?,
145             // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
146             // (e.g expanding a builtin macro). So we give up here.
147             ast::MacroCall(_it) => return None,
148             _ => return None,
149         }
150     };
151
152     res.markup = if markdown {
153         Markup::fenced_block(&ty.display(db))
154     } else {
155         ty.display(db).to_string().into()
156     };
157     let range = sema.original_range(&node).range;
158     Some(RangeInfo::new(range, res))
159 }
160
161 fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
162     fn to_action(nav_target: NavigationTarget) -> HoverAction {
163         HoverAction::Implementation(FilePosition {
164             file_id: nav_target.file_id,
165             offset: nav_target.focus_or_full_range().start(),
166         })
167     }
168
169     let adt = match def {
170         Definition::ModuleDef(ModuleDef::Trait(it)) => return it.try_to_nav(db).map(to_action),
171         Definition::ModuleDef(ModuleDef::Adt(it)) => Some(it),
172         Definition::SelfType(it) => it.target_ty(db).as_adt(),
173         _ => None,
174     }?;
175     adt.try_to_nav(db).map(to_action)
176 }
177
178 fn runnable_action(
179     sema: &Semantics<RootDatabase>,
180     def: Definition,
181     file_id: FileId,
182 ) -> Option<HoverAction> {
183     match def {
184         Definition::ModuleDef(it) => match it {
185             ModuleDef::Module(it) => runnable_mod(&sema, it).map(|it| HoverAction::Runnable(it)),
186             ModuleDef::Function(func) => {
187                 let src = func.source(sema.db)?;
188                 if src.file_id != file_id.into() {
189                     mark::hit!(hover_macro_generated_struct_fn_doc_comment);
190                     mark::hit!(hover_macro_generated_struct_fn_doc_attr);
191                     return None;
192                 }
193
194                 runnable_fn(&sema, func).map(HoverAction::Runnable)
195             }
196             _ => None,
197         },
198         _ => None,
199     }
200 }
201
202 fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
203     let mut targets: Vec<ModuleDef> = Vec::new();
204     let mut push_new_def = |item: ModuleDef| {
205         if !targets.contains(&item) {
206             targets.push(item);
207         }
208     };
209
210     if let Definition::GenericParam(GenericParam::TypeParam(it)) = def {
211         it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
212     } else {
213         let ty = match def {
214             Definition::Local(it) => it.ty(db),
215             Definition::GenericParam(GenericParam::ConstParam(it)) => it.ty(db),
216             _ => return None,
217         };
218
219         ty.walk(db, |t| {
220             if let Some(adt) = t.as_adt() {
221                 push_new_def(adt.into());
222             } else if let Some(trait_) = t.as_dyn_trait() {
223                 push_new_def(trait_.into());
224             } else if let Some(traits) = t.as_impl_traits(db) {
225                 traits.into_iter().for_each(|it| push_new_def(it.into()));
226             } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
227                 push_new_def(trait_.into());
228             }
229         });
230     }
231
232     let targets = targets
233         .into_iter()
234         .filter_map(|it| {
235             Some(HoverGotoTypeData {
236                 mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
237                 nav: it.try_to_nav(db)?,
238             })
239         })
240         .collect();
241
242     Some(HoverAction::GoToType(targets))
243 }
244
245 fn hover_markup(
246     docs: Option<String>,
247     desc: Option<String>,
248     mod_path: Option<String>,
249 ) -> Option<Markup> {
250     match desc {
251         Some(desc) => {
252             let mut buf = String::new();
253
254             if let Some(mod_path) = mod_path {
255                 if !mod_path.is_empty() {
256                     format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
257                 }
258             }
259             format_to!(buf, "```rust\n{}\n```", desc);
260
261             if let Some(doc) = docs {
262                 format_to!(buf, "\n___\n\n{}", doc);
263             }
264             Some(buf.into())
265         }
266         None => docs.map(Markup::from),
267     }
268 }
269
270 fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
271     match def {
272         Definition::Field(f) => Some(f.parent_def(db).name(db)),
273         Definition::Local(l) => l.parent(db).name(db),
274         Definition::ModuleDef(md) => match md {
275             ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
276                 AssocItemContainer::Trait(t) => Some(t.name(db)),
277                 AssocItemContainer::Impl(i) => i.target_ty(db).as_adt().map(|adt| adt.name(db)),
278             },
279             ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)),
280             _ => None,
281         },
282         _ => None,
283     }
284     .map(|name| name.to_string())
285 }
286
287 fn render_path(db: &RootDatabase, module: Module, item_name: Option<String>) -> String {
288     let crate_name =
289         db.crate_graph()[module.krate().into()].display_name.as_ref().map(|it| it.to_string());
290     let module_path = module
291         .path_to_root(db)
292         .into_iter()
293         .rev()
294         .flat_map(|it| it.name(db).map(|name| name.to_string()));
295     crate_name.into_iter().chain(module_path).chain(item_name).join("::")
296 }
297
298 fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
299     def.module(db).map(|module| render_path(db, module, definition_owner_name(db, def)))
300 }
301
302 fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
303     let mod_path = definition_mod_path(db, &def);
304     return match def {
305         Definition::Macro(it) => {
306             let label = macro_label(&it.source(db)?.value);
307             from_def_source_labeled(db, it, Some(label), mod_path)
308         }
309         Definition::Field(def) => {
310             let src = def.source(db)?.value;
311             if let FieldSource::Named(it) = src {
312                 from_def_source_labeled(db, def, it.short_label(), mod_path)
313             } else {
314                 None
315             }
316         }
317         Definition::ModuleDef(it) => match it {
318             ModuleDef::Module(it) => from_def_source_labeled(
319                 db,
320                 it,
321                 match it.definition_source(db).value {
322                     ModuleSource::Module(it) => it.short_label(),
323                     ModuleSource::SourceFile(it) => it.short_label(),
324                 },
325                 mod_path,
326             ),
327             ModuleDef::Function(it) => from_def_source(db, it, mod_path),
328             ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path),
329             ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path),
330             ModuleDef::Adt(Adt::Enum(it)) => from_def_source(db, it, mod_path),
331             ModuleDef::Variant(it) => from_def_source(db, it, mod_path),
332             ModuleDef::Const(it) => from_def_source(db, it, mod_path),
333             ModuleDef::Static(it) => from_def_source(db, it, mod_path),
334             ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
335             ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
336             ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)),
337         },
338         Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
339         Definition::SelfType(impl_def) => {
340             impl_def.target_ty(db).as_adt().and_then(|adt| match adt {
341                 Adt::Struct(it) => from_def_source(db, it, mod_path),
342                 Adt::Union(it) => from_def_source(db, it, mod_path),
343                 Adt::Enum(it) => from_def_source(db, it, mod_path),
344             })
345         }
346         Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))),
347         Definition::GenericParam(it) => match it {
348             GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))),
349             GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))),
350             GenericParam::ConstParam(it) => from_def_source(db, it, None),
351         },
352     };
353
354     fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup>
355     where
356         D: HasSource<Ast = A> + HasAttrs + Copy,
357         A: ShortLabel,
358     {
359         let short_label = def.source(db)?.value.short_label();
360         from_def_source_labeled(db, def, short_label, mod_path)
361     }
362
363     fn from_def_source_labeled<D>(
364         db: &RootDatabase,
365         def: D,
366         short_label: Option<String>,
367         mod_path: Option<String>,
368     ) -> Option<Markup>
369     where
370         D: HasAttrs,
371     {
372         let docs = def.attrs(db).docs().map(Into::into);
373         hover_markup(docs, short_label, mod_path)
374     }
375 }
376
377 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
378     return tokens.max_by_key(priority);
379     fn priority(n: &SyntaxToken) -> usize {
380         match n.kind() {
381             IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 3,
382             T!['('] | T![')'] => 2,
383             kind if kind.is_trivia() => 0,
384             _ => 1,
385         }
386     }
387 }
388
389 #[cfg(test)]
390 mod tests {
391     use expect_test::{expect, Expect};
392     use ide_db::base_db::FileLoader;
393
394     use crate::fixture;
395
396     use super::*;
397
398     fn check_hover_no_result(ra_fixture: &str) {
399         let (analysis, position) = fixture::position(ra_fixture);
400         assert!(analysis.hover(position, true, true).unwrap().is_none());
401     }
402
403     fn check(ra_fixture: &str, expect: Expect) {
404         let (analysis, position) = fixture::position(ra_fixture);
405         let hover = analysis.hover(position, true, true).unwrap().unwrap();
406
407         let content = analysis.db.file_text(position.file_id);
408         let hovered_element = &content[hover.range];
409
410         let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
411         expect.assert_eq(&actual)
412     }
413
414     fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
415         let (analysis, position) = fixture::position(ra_fixture);
416         let hover = analysis.hover(position, false, true).unwrap().unwrap();
417
418         let content = analysis.db.file_text(position.file_id);
419         let hovered_element = &content[hover.range];
420
421         let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
422         expect.assert_eq(&actual)
423     }
424
425     fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
426         let (analysis, position) = fixture::position(ra_fixture);
427         let hover = analysis.hover(position, true, false).unwrap().unwrap();
428
429         let content = analysis.db.file_text(position.file_id);
430         let hovered_element = &content[hover.range];
431
432         let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
433         expect.assert_eq(&actual)
434     }
435
436     fn check_actions(ra_fixture: &str, expect: Expect) {
437         let (analysis, position) = fixture::position(ra_fixture);
438         let hover = analysis.hover(position, true, true).unwrap().unwrap();
439         expect.assert_debug_eq(&hover.info.actions)
440     }
441
442     #[test]
443     fn hover_shows_type_of_an_expression() {
444         check(
445             r#"
446 pub fn foo() -> u32 { 1 }
447
448 fn main() {
449     let foo_test = foo()$0;
450 }
451 "#,
452             expect![[r#"
453                 *foo()*
454                 ```rust
455                 u32
456                 ```
457             "#]],
458         );
459     }
460
461     #[test]
462     fn hover_remove_markdown_if_configured() {
463         check_hover_no_markdown(
464             r#"
465 pub fn foo() -> u32 { 1 }
466
467 fn main() {
468     let foo_test = foo()$0;
469 }
470 "#,
471             expect![[r#"
472                 *foo()*
473                 u32
474             "#]],
475         );
476     }
477
478     #[test]
479     fn hover_shows_long_type_of_an_expression() {
480         check(
481             r#"
482 struct Scan<A, B, C> { a: A, b: B, c: C }
483 struct Iter<I> { inner: I }
484 enum Option<T> { Some(T), None }
485
486 struct OtherStruct<T> { i: T }
487
488 fn scan<A, B, C>(a: A, b: B, c: C) -> Iter<Scan<OtherStruct<A>, B, C>> {
489     Iter { inner: Scan { a, b, c } }
490 }
491
492 fn main() {
493     let num: i32 = 55;
494     let closure = |memo: &mut u32, value: &u32, _another: &mut u32| -> Option<u32> {
495         Option::Some(*memo + value)
496     };
497     let number = 5u32;
498     let mut iter$0 = scan(OtherStruct { i: num }, closure, number);
499 }
500 "#,
501             expect![[r#"
502                 *iter*
503
504                 ```rust
505                 Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>>
506                 ```
507             "#]],
508         );
509     }
510
511     #[test]
512     fn hover_shows_fn_signature() {
513         // Single file with result
514         check(
515             r#"
516 pub fn foo() -> u32 { 1 }
517
518 fn main() { let foo_test = fo$0o(); }
519 "#,
520             expect![[r#"
521                 *foo*
522
523                 ```rust
524                 test
525                 ```
526
527                 ```rust
528                 pub fn foo() -> u32
529                 ```
530             "#]],
531         );
532
533         // Multiple candidates but results are ambiguous.
534         check(
535             r#"
536 //- /a.rs
537 pub fn foo() -> u32 { 1 }
538
539 //- /b.rs
540 pub fn foo() -> &str { "" }
541
542 //- /c.rs
543 pub fn foo(a: u32, b: u32) {}
544
545 //- /main.rs
546 mod a;
547 mod b;
548 mod c;
549
550 fn main() { let foo_test = fo$0o(); }
551         "#,
552             expect![[r#"
553                 *foo*
554                 ```rust
555                 {unknown}
556                 ```
557             "#]],
558         );
559     }
560
561     #[test]
562     fn hover_shows_fn_signature_with_type_params() {
563         check(
564             r#"
565 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { }
566
567 fn main() { let foo_test = fo$0o(); }
568         "#,
569             expect![[r#"
570                 *foo*
571
572                 ```rust
573                 test
574                 ```
575
576                 ```rust
577                 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str
578                 ```
579             "#]],
580         );
581     }
582
583     #[test]
584     fn hover_shows_fn_signature_on_fn_name() {
585         check(
586             r#"
587 pub fn foo$0(a: u32, b: u32) -> u32 {}
588
589 fn main() { }
590 "#,
591             expect![[r#"
592                 *foo*
593
594                 ```rust
595                 test
596                 ```
597
598                 ```rust
599                 pub fn foo(a: u32, b: u32) -> u32
600                 ```
601             "#]],
602         );
603     }
604
605     #[test]
606     fn hover_shows_fn_doc() {
607         check(
608             r#"
609 /// # Example
610 /// ```
611 /// # use std::path::Path;
612 /// #
613 /// foo(Path::new("hello, world!"))
614 /// ```
615 pub fn foo$0(_: &Path) {}
616
617 fn main() { }
618 "#,
619             expect![[r##"
620                 *foo*
621
622                 ```rust
623                 test
624                 ```
625
626                 ```rust
627                 pub fn foo(_: &Path)
628                 ```
629
630                 ---
631
632                 # Example
633
634                 ```
635                 # use std::path::Path;
636                 #
637                 foo(Path::new("hello, world!"))
638                 ```
639             "##]],
640         );
641     }
642
643     #[test]
644     fn hover_shows_fn_doc_attr_raw_string() {
645         check(
646             r##"
647 #[doc = r#"Raw string doc attr"#]
648 pub fn foo$0(_: &Path) {}
649
650 fn main() { }
651 "##,
652             expect![[r##"
653                 *foo*
654
655                 ```rust
656                 test
657                 ```
658
659                 ```rust
660                 pub fn foo(_: &Path)
661                 ```
662
663                 ---
664
665                 Raw string doc attr
666             "##]],
667         );
668     }
669
670     #[test]
671     fn hover_shows_struct_field_info() {
672         // Hovering over the field when instantiating
673         check(
674             r#"
675 struct Foo { field_a: u32 }
676
677 fn main() {
678     let foo = Foo { field_a$0: 0, };
679 }
680 "#,
681             expect![[r#"
682                 *field_a*
683
684                 ```rust
685                 test::Foo
686                 ```
687
688                 ```rust
689                 field_a: u32
690                 ```
691             "#]],
692         );
693
694         // Hovering over the field in the definition
695         check(
696             r#"
697 struct Foo { field_a$0: u32 }
698
699 fn main() {
700     let foo = Foo { field_a: 0 };
701 }
702 "#,
703             expect![[r#"
704                 *field_a*
705
706                 ```rust
707                 test::Foo
708                 ```
709
710                 ```rust
711                 field_a: u32
712                 ```
713             "#]],
714         );
715     }
716
717     #[test]
718     fn hover_const_static() {
719         check(
720             r#"const foo$0: u32 = 123;"#,
721             expect![[r#"
722                 *foo*
723
724                 ```rust
725                 test
726                 ```
727
728                 ```rust
729                 const foo: u32 = 123
730                 ```
731             "#]],
732         );
733         check(
734             r#"static foo$0: u32 = 456;"#,
735             expect![[r#"
736                 *foo*
737
738                 ```rust
739                 test
740                 ```
741
742                 ```rust
743                 static foo: u32
744                 ```
745             "#]],
746         );
747     }
748
749     #[test]
750     fn hover_default_generic_types() {
751         check(
752             r#"
753 struct Test<K, T = u8> { k: K, t: T }
754
755 fn main() {
756     let zz$0 = Test { t: 23u8, k: 33 };
757 }"#,
758             expect![[r#"
759                 *zz*
760
761                 ```rust
762                 Test<i32, u8>
763                 ```
764             "#]],
765         );
766     }
767
768     #[test]
769     fn hover_some() {
770         check(
771             r#"
772 enum Option<T> { Some(T) }
773 use Option::Some;
774
775 fn main() { So$0me(12); }
776 "#,
777             expect![[r#"
778                 *Some*
779
780                 ```rust
781                 test::Option
782                 ```
783
784                 ```rust
785                 Some
786                 ```
787             "#]],
788         );
789
790         check(
791             r#"
792 enum Option<T> { Some(T) }
793 use Option::Some;
794
795 fn main() { let b$0ar = Some(12); }
796 "#,
797             expect![[r#"
798                 *bar*
799
800                 ```rust
801                 Option<i32>
802                 ```
803             "#]],
804         );
805     }
806
807     #[test]
808     fn hover_enum_variant() {
809         check(
810             r#"
811 enum Option<T> {
812     /// The None variant
813     Non$0e
814 }
815 "#,
816             expect![[r#"
817                 *None*
818
819                 ```rust
820                 test::Option
821                 ```
822
823                 ```rust
824                 None
825                 ```
826
827                 ---
828
829                 The None variant
830             "#]],
831         );
832
833         check(
834             r#"
835 enum Option<T> {
836     /// The Some variant
837     Some(T)
838 }
839 fn main() {
840     let s = Option::Som$0e(12);
841 }
842 "#,
843             expect![[r#"
844                 *Some*
845
846                 ```rust
847                 test::Option
848                 ```
849
850                 ```rust
851                 Some
852                 ```
853
854                 ---
855
856                 The Some variant
857             "#]],
858         );
859     }
860
861     #[test]
862     fn hover_for_local_variable() {
863         check(
864             r#"fn func(foo: i32) { fo$0o; }"#,
865             expect![[r#"
866                 *foo*
867
868                 ```rust
869                 i32
870                 ```
871             "#]],
872         )
873     }
874
875     #[test]
876     fn hover_for_local_variable_pat() {
877         check(
878             r#"fn func(fo$0o: i32) {}"#,
879             expect![[r#"
880                 *foo*
881
882                 ```rust
883                 i32
884                 ```
885             "#]],
886         )
887     }
888
889     #[test]
890     fn hover_local_var_edge() {
891         check(
892             r#"fn func(foo: i32) { if true { $0foo; }; }"#,
893             expect![[r#"
894                 *foo*
895
896                 ```rust
897                 i32
898                 ```
899             "#]],
900         )
901     }
902
903     #[test]
904     fn hover_for_param_edge() {
905         check(
906             r#"fn func($0foo: i32) {}"#,
907             expect![[r#"
908                 *foo*
909
910                 ```rust
911                 i32
912                 ```
913             "#]],
914         )
915     }
916
917     #[test]
918     fn hover_for_param_with_multiple_traits() {
919         check(
920             r#"trait Deref {
921                 type Target: ?Sized;
922             }
923             trait DerefMut {
924                 type Target: ?Sized;
925             }
926             fn f(_x$0: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#,
927             expect![[r#"
928                 *_x*
929
930                 ```rust
931                 impl Deref<Target = u8> + DerefMut<Target = u8>
932                 ```
933             "#]],
934         )
935     }
936
937     #[test]
938     fn test_hover_infer_associated_method_result() {
939         check(
940             r#"
941 struct Thing { x: u32 }
942
943 impl Thing {
944     fn new() -> Thing { Thing { x: 0 } }
945 }
946
947 fn main() { let foo_$0test = Thing::new(); }
948             "#,
949             expect![[r#"
950                 *foo_test*
951
952                 ```rust
953                 Thing
954                 ```
955             "#]],
956         )
957     }
958
959     #[test]
960     fn test_hover_infer_associated_method_exact() {
961         check(
962             r#"
963 mod wrapper {
964     struct Thing { x: u32 }
965
966     impl Thing {
967         fn new() -> Thing { Thing { x: 0 } }
968     }
969 }
970
971 fn main() { let foo_test = wrapper::Thing::new$0(); }
972 "#,
973             expect![[r#"
974                 *new*
975
976                 ```rust
977                 test::wrapper::Thing
978                 ```
979
980                 ```rust
981                 fn new() -> Thing
982                 ```
983             "#]],
984         )
985     }
986
987     #[test]
988     fn test_hover_infer_associated_const_in_pattern() {
989         check(
990             r#"
991 struct X;
992 impl X {
993     const C: u32 = 1;
994 }
995
996 fn main() {
997     match 1 {
998         X::C$0 => {},
999         2 => {},
1000         _ => {}
1001     };
1002 }
1003 "#,
1004             expect![[r#"
1005                 *C*
1006
1007                 ```rust
1008                 test
1009                 ```
1010
1011                 ```rust
1012                 const C: u32 = 1
1013                 ```
1014             "#]],
1015         )
1016     }
1017
1018     #[test]
1019     fn test_hover_self() {
1020         check(
1021             r#"
1022 struct Thing { x: u32 }
1023 impl Thing {
1024     fn new() -> Self { Self$0 { x: 0 } }
1025 }
1026 "#,
1027             expect![[r#"
1028                 *Self*
1029
1030                 ```rust
1031                 test
1032                 ```
1033
1034                 ```rust
1035                 struct Thing
1036                 ```
1037             "#]],
1038         );
1039         check(
1040             r#"
1041 struct Thing { x: u32 }
1042 impl Thing {
1043     fn new() -> Self$0 { Self { x: 0 } }
1044 }
1045 "#,
1046             expect![[r#"
1047                 *Self*
1048
1049                 ```rust
1050                 test
1051                 ```
1052
1053                 ```rust
1054                 struct Thing
1055                 ```
1056             "#]],
1057         );
1058         check(
1059             r#"
1060 enum Thing { A }
1061 impl Thing {
1062     pub fn new() -> Self$0 { Thing::A }
1063 }
1064 "#,
1065             expect![[r#"
1066                 *Self*
1067
1068                 ```rust
1069                 test
1070                 ```
1071
1072                 ```rust
1073                 enum Thing
1074                 ```
1075             "#]],
1076         );
1077         check(
1078             r#"
1079         enum Thing { A }
1080         impl Thing {
1081             pub fn thing(a: Self$0) {}
1082         }
1083         "#,
1084             expect![[r#"
1085                 *Self*
1086
1087                 ```rust
1088                 test
1089                 ```
1090
1091                 ```rust
1092                 enum Thing
1093                 ```
1094             "#]],
1095         );
1096     }
1097
1098     #[test]
1099     fn test_hover_shadowing_pat() {
1100         check(
1101             r#"
1102 fn x() {}
1103
1104 fn y() {
1105     let x = 0i32;
1106     x$0;
1107 }
1108 "#,
1109             expect![[r#"
1110                 *x*
1111
1112                 ```rust
1113                 i32
1114                 ```
1115             "#]],
1116         )
1117     }
1118
1119     #[test]
1120     fn test_hover_macro_invocation() {
1121         check(
1122             r#"
1123 macro_rules! foo { () => {} }
1124
1125 fn f() { fo$0o!(); }
1126 "#,
1127             expect![[r#"
1128                 *foo*
1129
1130                 ```rust
1131                 test
1132                 ```
1133
1134                 ```rust
1135                 macro_rules! foo
1136                 ```
1137             "#]],
1138         )
1139     }
1140
1141     #[test]
1142     fn test_hover_tuple_field() {
1143         check(
1144             r#"struct TS(String, i32$0);"#,
1145             expect![[r#"
1146                 *i32*
1147
1148                 ```rust
1149                 i32
1150                 ```
1151             "#]],
1152         )
1153     }
1154
1155     #[test]
1156     fn test_hover_through_macro() {
1157         check(
1158             r#"
1159 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1160 fn foo() {}
1161 id! {
1162     fn bar() { fo$0o(); }
1163 }
1164 "#,
1165             expect![[r#"
1166                 *foo*
1167
1168                 ```rust
1169                 test
1170                 ```
1171
1172                 ```rust
1173                 fn foo()
1174                 ```
1175             "#]],
1176         );
1177     }
1178
1179     #[test]
1180     fn test_hover_through_expr_in_macro() {
1181         check(
1182             r#"
1183 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1184 fn foo(bar:u32) { let a = id!(ba$0r); }
1185 "#,
1186             expect![[r#"
1187                 *bar*
1188
1189                 ```rust
1190                 u32
1191                 ```
1192             "#]],
1193         );
1194     }
1195
1196     #[test]
1197     fn test_hover_through_expr_in_macro_recursive() {
1198         check(
1199             r#"
1200 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1201 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1202 fn foo(bar:u32) { let a = id!(ba$0r); }
1203 "#,
1204             expect![[r#"
1205                 *bar*
1206
1207                 ```rust
1208                 u32
1209                 ```
1210             "#]],
1211         );
1212     }
1213
1214     #[test]
1215     fn test_hover_through_func_in_macro_recursive() {
1216         check(
1217             r#"
1218 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1219 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1220 fn bar() -> u32 { 0 }
1221 fn foo() { let a = id!([0u32, bar($0)] ); }
1222 "#,
1223             expect![[r#"
1224                 *bar()*
1225                 ```rust
1226                 u32
1227                 ```
1228             "#]],
1229         );
1230     }
1231
1232     #[test]
1233     fn test_hover_through_literal_string_in_macro() {
1234         check(
1235             r#"
1236 macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } }
1237 fn foo() {
1238     let mastered_for_itunes = "";
1239     let _ = arr!("Tr$0acks", &mastered_for_itunes);
1240 }
1241 "#,
1242             expect![[r#"
1243                 *"Tracks"*
1244                 ```rust
1245                 &str
1246                 ```
1247             "#]],
1248         );
1249     }
1250
1251     #[test]
1252     fn test_hover_through_assert_macro() {
1253         check(
1254             r#"
1255 #[rustc_builtin_macro]
1256 macro_rules! assert {}
1257
1258 fn bar() -> bool { true }
1259 fn foo() {
1260     assert!(ba$0r());
1261 }
1262 "#,
1263             expect![[r#"
1264                 *bar*
1265
1266                 ```rust
1267                 test
1268                 ```
1269
1270                 ```rust
1271                 fn bar() -> bool
1272                 ```
1273             "#]],
1274         );
1275     }
1276
1277     #[test]
1278     fn test_hover_through_literal_string_in_builtin_macro() {
1279         check_hover_no_result(
1280             r#"
1281             #[rustc_builtin_macro]
1282             macro_rules! format {}
1283
1284             fn foo() {
1285                 format!("hel$0lo {}", 0);
1286             }
1287             "#,
1288         );
1289     }
1290
1291     #[test]
1292     fn test_hover_non_ascii_space_doc() {
1293         check(
1294             "
1295 /// <- `\u{3000}` here
1296 fn foo() { }
1297
1298 fn bar() { fo$0o(); }
1299 ",
1300             expect![[r#"
1301                 *foo*
1302
1303                 ```rust
1304                 test
1305                 ```
1306
1307                 ```rust
1308                 fn foo()
1309                 ```
1310
1311                 ---
1312
1313                 \<- ` ` here
1314             "#]],
1315         );
1316     }
1317
1318     #[test]
1319     fn test_hover_function_show_qualifiers() {
1320         check(
1321             r#"async fn foo$0() {}"#,
1322             expect![[r#"
1323                 *foo*
1324
1325                 ```rust
1326                 test
1327                 ```
1328
1329                 ```rust
1330                 async fn foo()
1331                 ```
1332             "#]],
1333         );
1334         check(
1335             r#"pub const unsafe fn foo$0() {}"#,
1336             expect![[r#"
1337                 *foo*
1338
1339                 ```rust
1340                 test
1341                 ```
1342
1343                 ```rust
1344                 pub const unsafe fn foo()
1345                 ```
1346             "#]],
1347         );
1348         check(
1349             r#"pub(crate) async unsafe extern "C" fn foo$0() {}"#,
1350             expect![[r#"
1351                 *foo*
1352
1353                 ```rust
1354                 test
1355                 ```
1356
1357                 ```rust
1358                 pub(crate) async unsafe extern "C" fn foo()
1359                 ```
1360             "#]],
1361         );
1362     }
1363
1364     #[test]
1365     fn test_hover_trait_show_qualifiers() {
1366         check_actions(
1367             r"unsafe trait foo$0() {}",
1368             expect![[r#"
1369                 [
1370                     Implementation(
1371                         FilePosition {
1372                             file_id: FileId(
1373                                 0,
1374                             ),
1375                             offset: 13,
1376                         },
1377                     ),
1378                 ]
1379             "#]],
1380         );
1381     }
1382
1383     #[test]
1384     fn test_hover_extern_crate() {
1385         check(
1386             r#"
1387 //- /main.rs crate:main deps:std
1388 extern crate st$0d;
1389 //- /std/lib.rs crate:std
1390 //! Standard library for this test
1391 //!
1392 //! Printed?
1393 //! abc123
1394             "#,
1395             expect![[r#"
1396             *std*
1397             Standard library for this test
1398
1399             Printed?
1400             abc123
1401             "#]],
1402         );
1403         check(
1404             r#"
1405 //- /main.rs crate:main deps:std
1406 extern crate std as ab$0c;
1407 //- /std/lib.rs crate:std
1408 //! Standard library for this test
1409 //!
1410 //! Printed?
1411 //! abc123
1412             "#,
1413             expect![[r#"
1414             *abc*
1415             Standard library for this test
1416
1417             Printed?
1418             abc123
1419             "#]],
1420         );
1421     }
1422
1423     #[test]
1424     fn test_hover_mod_with_same_name_as_function() {
1425         check(
1426             r#"
1427 use self::m$0y::Bar;
1428 mod my { pub struct Bar; }
1429
1430 fn my() {}
1431 "#,
1432             expect![[r#"
1433                 *my*
1434
1435                 ```rust
1436                 test
1437                 ```
1438
1439                 ```rust
1440                 mod my
1441                 ```
1442             "#]],
1443         );
1444     }
1445
1446     #[test]
1447     fn test_hover_struct_doc_comment() {
1448         check(
1449             r#"
1450 /// bar docs
1451 struct Bar;
1452
1453 fn foo() { let bar = Ba$0r; }
1454 "#,
1455             expect![[r#"
1456                 *Bar*
1457
1458                 ```rust
1459                 test
1460                 ```
1461
1462                 ```rust
1463                 struct Bar
1464                 ```
1465
1466                 ---
1467
1468                 bar docs
1469             "#]],
1470         );
1471     }
1472
1473     #[test]
1474     fn test_hover_struct_doc_attr() {
1475         check(
1476             r#"
1477 #[doc = "bar docs"]
1478 struct Bar;
1479
1480 fn foo() { let bar = Ba$0r; }
1481 "#,
1482             expect![[r#"
1483                 *Bar*
1484
1485                 ```rust
1486                 test
1487                 ```
1488
1489                 ```rust
1490                 struct Bar
1491                 ```
1492
1493                 ---
1494
1495                 bar docs
1496             "#]],
1497         );
1498     }
1499
1500     #[test]
1501     fn test_hover_struct_doc_attr_multiple_and_mixed() {
1502         check(
1503             r#"
1504 /// bar docs 0
1505 #[doc = "bar docs 1"]
1506 #[doc = "bar docs 2"]
1507 struct Bar;
1508
1509 fn foo() { let bar = Ba$0r; }
1510 "#,
1511             expect![[r#"
1512                 *Bar*
1513
1514                 ```rust
1515                 test
1516                 ```
1517
1518                 ```rust
1519                 struct Bar
1520                 ```
1521
1522                 ---
1523
1524                 bar docs 0
1525                 bar docs 1
1526                 bar docs 2
1527             "#]],
1528         );
1529     }
1530
1531     #[test]
1532     fn test_hover_path_link() {
1533         check(
1534             r#"
1535 pub struct Foo;
1536 /// [Foo](struct.Foo.html)
1537 pub struct B$0ar
1538 "#,
1539             expect![[r#"
1540                 *Bar*
1541
1542                 ```rust
1543                 test
1544                 ```
1545
1546                 ```rust
1547                 pub struct Bar
1548                 ```
1549
1550                 ---
1551
1552                 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1553             "#]],
1554         );
1555     }
1556
1557     #[test]
1558     fn test_hover_path_link_no_strip() {
1559         check(
1560             r#"
1561 pub struct Foo;
1562 /// [struct Foo](struct.Foo.html)
1563 pub struct B$0ar
1564 "#,
1565             expect![[r#"
1566                 *Bar*
1567
1568                 ```rust
1569                 test
1570                 ```
1571
1572                 ```rust
1573                 pub struct Bar
1574                 ```
1575
1576                 ---
1577
1578                 [struct Foo](https://docs.rs/test/*/test/struct.Foo.html)
1579             "#]],
1580         );
1581     }
1582
1583     #[ignore = "path based links currently only support documentation on ModuleDef items"]
1584     #[test]
1585     fn test_hover_path_link_field() {
1586         check(
1587             r#"
1588 pub struct Foo;
1589 pub struct Bar {
1590     /// [Foo](struct.Foo.html)
1591     fie$0ld: ()
1592 }
1593 "#,
1594             expect![[r#"
1595                 *field*
1596
1597                 ```rust
1598                 test::Bar
1599                 ```
1600
1601                 ```rust
1602                 field: ()
1603                 ```
1604
1605                 ---
1606
1607                 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1608             "#]],
1609         );
1610     }
1611
1612     #[test]
1613     fn test_hover_intra_link() {
1614         check(
1615             r#"
1616 pub mod foo {
1617     pub struct Foo;
1618 }
1619 /// [Foo](foo::Foo)
1620 pub struct B$0ar
1621 "#,
1622             expect![[r#"
1623                 *Bar*
1624
1625                 ```rust
1626                 test
1627                 ```
1628
1629                 ```rust
1630                 pub struct Bar
1631                 ```
1632
1633                 ---
1634
1635                 [Foo](https://docs.rs/test/*/test/foo/struct.Foo.html)
1636             "#]],
1637         );
1638     }
1639
1640     #[test]
1641     fn test_hover_intra_link_html_root_url() {
1642         check(
1643             r#"
1644 #![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
1645
1646 pub mod foo {
1647     pub struct Foo;
1648 }
1649 /// [Foo](foo::Foo)
1650 pub struct B$0ar
1651 "#,
1652             expect![[r#"
1653                 *Bar*
1654
1655                 ```rust
1656                 test
1657                 ```
1658
1659                 ```rust
1660                 pub struct Bar
1661                 ```
1662
1663                 ---
1664
1665                 [Foo](https://example.com/test/foo/struct.Foo.html)
1666             "#]],
1667         );
1668     }
1669
1670     #[test]
1671     fn test_hover_intra_link_shortlink() {
1672         check(
1673             r#"
1674 pub struct Foo;
1675 /// [Foo]
1676 pub struct B$0ar
1677 "#,
1678             expect![[r#"
1679                 *Bar*
1680
1681                 ```rust
1682                 test
1683                 ```
1684
1685                 ```rust
1686                 pub struct Bar
1687                 ```
1688
1689                 ---
1690
1691                 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1692             "#]],
1693         );
1694     }
1695
1696     #[test]
1697     fn test_hover_intra_link_shortlink_code() {
1698         check(
1699             r#"
1700 pub struct Foo;
1701 /// [`Foo`]
1702 pub struct B$0ar
1703 "#,
1704             expect![[r#"
1705                 *Bar*
1706
1707                 ```rust
1708                 test
1709                 ```
1710
1711                 ```rust
1712                 pub struct Bar
1713                 ```
1714
1715                 ---
1716
1717                 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1718             "#]],
1719         );
1720     }
1721
1722     #[test]
1723     fn test_hover_intra_link_namespaced() {
1724         check(
1725             r#"
1726 pub struct Foo;
1727 fn Foo() {}
1728 /// [Foo()]
1729 pub struct B$0ar
1730 "#,
1731             expect![[r#"
1732                 *Bar*
1733
1734                 ```rust
1735                 test
1736                 ```
1737
1738                 ```rust
1739                 pub struct Bar
1740                 ```
1741
1742                 ---
1743
1744                 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1745             "#]],
1746         );
1747     }
1748
1749     #[test]
1750     fn test_hover_intra_link_shortlink_namspaced_code() {
1751         check(
1752             r#"
1753 pub struct Foo;
1754 /// [`struct Foo`]
1755 pub struct B$0ar
1756 "#,
1757             expect![[r#"
1758                 *Bar*
1759
1760                 ```rust
1761                 test
1762                 ```
1763
1764                 ```rust
1765                 pub struct Bar
1766                 ```
1767
1768                 ---
1769
1770                 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1771             "#]],
1772         );
1773     }
1774
1775     #[test]
1776     fn test_hover_intra_link_shortlink_namspaced_code_with_at() {
1777         check(
1778             r#"
1779 pub struct Foo;
1780 /// [`struct@Foo`]
1781 pub struct B$0ar
1782 "#,
1783             expect![[r#"
1784                 *Bar*
1785
1786                 ```rust
1787                 test
1788                 ```
1789
1790                 ```rust
1791                 pub struct Bar
1792                 ```
1793
1794                 ---
1795
1796                 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1797             "#]],
1798         );
1799     }
1800
1801     #[test]
1802     fn test_hover_intra_link_reference() {
1803         check(
1804             r#"
1805 pub struct Foo;
1806 /// [my Foo][foo]
1807 ///
1808 /// [foo]: Foo
1809 pub struct B$0ar
1810 "#,
1811             expect![[r#"
1812                 *Bar*
1813
1814                 ```rust
1815                 test
1816                 ```
1817
1818                 ```rust
1819                 pub struct Bar
1820                 ```
1821
1822                 ---
1823
1824                 [my Foo](https://docs.rs/test/*/test/struct.Foo.html)
1825             "#]],
1826         );
1827     }
1828
1829     #[test]
1830     fn test_hover_external_url() {
1831         check(
1832             r#"
1833 pub struct Foo;
1834 /// [external](https://www.google.com)
1835 pub struct B$0ar
1836 "#,
1837             expect![[r#"
1838                 *Bar*
1839
1840                 ```rust
1841                 test
1842                 ```
1843
1844                 ```rust
1845                 pub struct Bar
1846                 ```
1847
1848                 ---
1849
1850                 [external](https://www.google.com)
1851             "#]],
1852         );
1853     }
1854
1855     // Check that we don't rewrite links which we can't identify
1856     #[test]
1857     fn test_hover_unknown_target() {
1858         check(
1859             r#"
1860 pub struct Foo;
1861 /// [baz](Baz)
1862 pub struct B$0ar
1863 "#,
1864             expect![[r#"
1865                 *Bar*
1866
1867                 ```rust
1868                 test
1869                 ```
1870
1871                 ```rust
1872                 pub struct Bar
1873                 ```
1874
1875                 ---
1876
1877                 [baz](Baz)
1878             "#]],
1879         );
1880     }
1881
1882     #[test]
1883     fn test_doc_links_enum_variant() {
1884         check(
1885             r#"
1886 enum E {
1887     /// [E]
1888     V$0 { field: i32 }
1889 }
1890 "#,
1891             expect![[r#"
1892                 *V*
1893
1894                 ```rust
1895                 test::E
1896                 ```
1897
1898                 ```rust
1899                 V
1900                 ```
1901
1902                 ---
1903
1904                 [E](https://docs.rs/test/*/test/enum.E.html)
1905             "#]],
1906         );
1907     }
1908
1909     #[test]
1910     fn test_doc_links_field() {
1911         check(
1912             r#"
1913 struct S {
1914     /// [`S`]
1915     field$0: i32
1916 }
1917 "#,
1918             expect![[r#"
1919                 *field*
1920
1921                 ```rust
1922                 test::S
1923                 ```
1924
1925                 ```rust
1926                 field: i32
1927                 ```
1928
1929                 ---
1930
1931                 [`S`](https://docs.rs/test/*/test/struct.S.html)
1932             "#]],
1933         );
1934     }
1935
1936     #[test]
1937     fn test_hover_no_links() {
1938         check_hover_no_links(
1939             r#"
1940 /// Test cases:
1941 /// case 1.  bare URL: https://www.example.com/
1942 /// case 2.  inline URL with title: [example](https://www.example.com/)
1943 /// case 3.  code reference: [`Result`]
1944 /// case 4.  code reference but miss footnote: [`String`]
1945 /// case 5.  autolink: <http://www.example.com/>
1946 /// case 6.  email address: <test@example.com>
1947 /// case 7.  reference: [example][example]
1948 /// case 8.  collapsed link: [example][]
1949 /// case 9.  shortcut link: [example]
1950 /// case 10. inline without URL: [example]()
1951 /// case 11. reference: [foo][foo]
1952 /// case 12. reference: [foo][bar]
1953 /// case 13. collapsed link: [foo][]
1954 /// case 14. shortcut link: [foo]
1955 /// case 15. inline without URL: [foo]()
1956 /// case 16. just escaped text: \[foo]
1957 /// case 17. inline link: [Foo](foo::Foo)
1958 ///
1959 /// [`Result`]: ../../std/result/enum.Result.html
1960 /// [^example]: https://www.example.com/
1961 pub fn fo$0o() {}
1962 "#,
1963             expect![[r#"
1964                 *foo*
1965
1966                 ```rust
1967                 test
1968                 ```
1969
1970                 ```rust
1971                 pub fn foo()
1972                 ```
1973
1974                 ---
1975
1976                 Test cases:
1977                 case 1.  bare URL: https://www.example.com/
1978                 case 2.  inline URL with title: [example](https://www.example.com/)
1979                 case 3.  code reference: `Result`
1980                 case 4.  code reference but miss footnote: `String`
1981                 case 5.  autolink: http://www.example.com/
1982                 case 6.  email address: test@example.com
1983                 case 7.  reference: example
1984                 case 8.  collapsed link: example
1985                 case 9.  shortcut link: example
1986                 case 10. inline without URL: example
1987                 case 11. reference: foo
1988                 case 12. reference: foo
1989                 case 13. collapsed link: foo
1990                 case 14. shortcut link: foo
1991                 case 15. inline without URL: foo
1992                 case 16. just escaped text: \[foo]
1993                 case 17. inline link: Foo
1994
1995                 [^example]: https://www.example.com/
1996             "#]],
1997         );
1998     }
1999
2000     #[test]
2001     fn test_hover_macro_generated_struct_fn_doc_comment() {
2002         mark::check!(hover_macro_generated_struct_fn_doc_comment);
2003
2004         check(
2005             r#"
2006 macro_rules! bar {
2007     () => {
2008         struct Bar;
2009         impl Bar {
2010             /// Do the foo
2011             fn foo(&self) {}
2012         }
2013     }
2014 }
2015
2016 bar!();
2017
2018 fn foo() { let bar = Bar; bar.fo$0o(); }
2019 "#,
2020             expect![[r#"
2021                 *foo*
2022
2023                 ```rust
2024                 test::Bar
2025                 ```
2026
2027                 ```rust
2028                 fn foo(&self)
2029                 ```
2030
2031                 ---
2032
2033                 Do the foo
2034             "#]],
2035         );
2036     }
2037
2038     #[test]
2039     fn test_hover_macro_generated_struct_fn_doc_attr() {
2040         mark::check!(hover_macro_generated_struct_fn_doc_attr);
2041
2042         check(
2043             r#"
2044 macro_rules! bar {
2045     () => {
2046         struct Bar;
2047         impl Bar {
2048             #[doc = "Do the foo"]
2049             fn foo(&self) {}
2050         }
2051     }
2052 }
2053
2054 bar!();
2055
2056 fn foo() { let bar = Bar; bar.fo$0o(); }
2057 "#,
2058             expect![[r#"
2059                 *foo*
2060
2061                 ```rust
2062                 test::Bar
2063                 ```
2064
2065                 ```rust
2066                 fn foo(&self)
2067                 ```
2068
2069                 ---
2070
2071                 Do the foo
2072             "#]],
2073         );
2074     }
2075
2076     #[test]
2077     fn test_hover_trait_has_impl_action() {
2078         check_actions(
2079             r#"trait foo$0() {}"#,
2080             expect![[r#"
2081                 [
2082                     Implementation(
2083                         FilePosition {
2084                             file_id: FileId(
2085                                 0,
2086                             ),
2087                             offset: 6,
2088                         },
2089                     ),
2090                 ]
2091             "#]],
2092         );
2093     }
2094
2095     #[test]
2096     fn test_hover_struct_has_impl_action() {
2097         check_actions(
2098             r"struct foo$0() {}",
2099             expect![[r#"
2100                 [
2101                     Implementation(
2102                         FilePosition {
2103                             file_id: FileId(
2104                                 0,
2105                             ),
2106                             offset: 7,
2107                         },
2108                     ),
2109                 ]
2110             "#]],
2111         );
2112     }
2113
2114     #[test]
2115     fn test_hover_union_has_impl_action() {
2116         check_actions(
2117             r#"union foo$0() {}"#,
2118             expect![[r#"
2119                 [
2120                     Implementation(
2121                         FilePosition {
2122                             file_id: FileId(
2123                                 0,
2124                             ),
2125                             offset: 6,
2126                         },
2127                     ),
2128                 ]
2129             "#]],
2130         );
2131     }
2132
2133     #[test]
2134     fn test_hover_enum_has_impl_action() {
2135         check_actions(
2136             r"enum foo$0() { A, B }",
2137             expect![[r#"
2138                 [
2139                     Implementation(
2140                         FilePosition {
2141                             file_id: FileId(
2142                                 0,
2143                             ),
2144                             offset: 5,
2145                         },
2146                     ),
2147                 ]
2148             "#]],
2149         );
2150     }
2151
2152     #[test]
2153     fn test_hover_self_has_impl_action() {
2154         check_actions(
2155             r#"struct foo where Self$0:;"#,
2156             expect![[r#"
2157                 [
2158                     Implementation(
2159                         FilePosition {
2160                             file_id: FileId(
2161                                 0,
2162                             ),
2163                             offset: 7,
2164                         },
2165                     ),
2166                 ]
2167             "#]],
2168         );
2169     }
2170
2171     #[test]
2172     fn test_hover_test_has_action() {
2173         check_actions(
2174             r#"
2175 #[test]
2176 fn foo_$0test() {}
2177 "#,
2178             expect![[r#"
2179                 [
2180                     Runnable(
2181                         Runnable {
2182                             nav: NavigationTarget {
2183                                 file_id: FileId(
2184                                     0,
2185                                 ),
2186                                 full_range: 0..24,
2187                                 focus_range: 11..19,
2188                                 name: "foo_test",
2189                                 kind: Function,
2190                             },
2191                             kind: Test {
2192                                 test_id: Path(
2193                                     "foo_test",
2194                                 ),
2195                                 attr: TestAttr {
2196                                     ignore: false,
2197                                 },
2198                             },
2199                             cfg: None,
2200                         },
2201                     ),
2202                 ]
2203             "#]],
2204         );
2205     }
2206
2207     #[test]
2208     fn test_hover_test_mod_has_action() {
2209         check_actions(
2210             r#"
2211 mod tests$0 {
2212     #[test]
2213     fn foo_test() {}
2214 }
2215 "#,
2216             expect![[r#"
2217                 [
2218                     Runnable(
2219                         Runnable {
2220                             nav: NavigationTarget {
2221                                 file_id: FileId(
2222                                     0,
2223                                 ),
2224                                 full_range: 0..46,
2225                                 focus_range: 4..9,
2226                                 name: "tests",
2227                                 kind: Module,
2228                             },
2229                             kind: TestMod {
2230                                 path: "tests",
2231                             },
2232                             cfg: None,
2233                         },
2234                     ),
2235                 ]
2236             "#]],
2237         );
2238     }
2239
2240     #[test]
2241     fn test_hover_struct_has_goto_type_action() {
2242         check_actions(
2243             r#"
2244 struct S{ f1: u32 }
2245
2246 fn main() { let s$0t = S{ f1:0 }; }
2247             "#,
2248             expect![[r#"
2249                 [
2250                     GoToType(
2251                         [
2252                             HoverGotoTypeData {
2253                                 mod_path: "test::S",
2254                                 nav: NavigationTarget {
2255                                     file_id: FileId(
2256                                         0,
2257                                     ),
2258                                     full_range: 0..19,
2259                                     focus_range: 7..8,
2260                                     name: "S",
2261                                     kind: Struct,
2262                                     description: "struct S",
2263                                 },
2264                             },
2265                         ],
2266                     ),
2267                 ]
2268             "#]],
2269         );
2270     }
2271
2272     #[test]
2273     fn test_hover_generic_struct_has_goto_type_actions() {
2274         check_actions(
2275             r#"
2276 struct Arg(u32);
2277 struct S<T>{ f1: T }
2278
2279 fn main() { let s$0t = S{ f1:Arg(0) }; }
2280 "#,
2281             expect![[r#"
2282                 [
2283                     GoToType(
2284                         [
2285                             HoverGotoTypeData {
2286                                 mod_path: "test::S",
2287                                 nav: NavigationTarget {
2288                                     file_id: FileId(
2289                                         0,
2290                                     ),
2291                                     full_range: 17..37,
2292                                     focus_range: 24..25,
2293                                     name: "S",
2294                                     kind: Struct,
2295                                     description: "struct S",
2296                                 },
2297                             },
2298                             HoverGotoTypeData {
2299                                 mod_path: "test::Arg",
2300                                 nav: NavigationTarget {
2301                                     file_id: FileId(
2302                                         0,
2303                                     ),
2304                                     full_range: 0..16,
2305                                     focus_range: 7..10,
2306                                     name: "Arg",
2307                                     kind: Struct,
2308                                     description: "struct Arg",
2309                                 },
2310                             },
2311                         ],
2312                     ),
2313                 ]
2314             "#]],
2315         );
2316     }
2317
2318     #[test]
2319     fn test_hover_generic_struct_has_flattened_goto_type_actions() {
2320         check_actions(
2321             r#"
2322 struct Arg(u32);
2323 struct S<T>{ f1: T }
2324
2325 fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
2326             "#,
2327             expect![[r#"
2328                 [
2329                     GoToType(
2330                         [
2331                             HoverGotoTypeData {
2332                                 mod_path: "test::S",
2333                                 nav: NavigationTarget {
2334                                     file_id: FileId(
2335                                         0,
2336                                     ),
2337                                     full_range: 17..37,
2338                                     focus_range: 24..25,
2339                                     name: "S",
2340                                     kind: Struct,
2341                                     description: "struct S",
2342                                 },
2343                             },
2344                             HoverGotoTypeData {
2345                                 mod_path: "test::Arg",
2346                                 nav: NavigationTarget {
2347                                     file_id: FileId(
2348                                         0,
2349                                     ),
2350                                     full_range: 0..16,
2351                                     focus_range: 7..10,
2352                                     name: "Arg",
2353                                     kind: Struct,
2354                                     description: "struct Arg",
2355                                 },
2356                             },
2357                         ],
2358                     ),
2359                 ]
2360             "#]],
2361         );
2362     }
2363
2364     #[test]
2365     fn test_hover_tuple_has_goto_type_actions() {
2366         check_actions(
2367             r#"
2368 struct A(u32);
2369 struct B(u32);
2370 mod M {
2371     pub struct C(u32);
2372 }
2373
2374 fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
2375 "#,
2376             expect![[r#"
2377                 [
2378                     GoToType(
2379                         [
2380                             HoverGotoTypeData {
2381                                 mod_path: "test::A",
2382                                 nav: NavigationTarget {
2383                                     file_id: FileId(
2384                                         0,
2385                                     ),
2386                                     full_range: 0..14,
2387                                     focus_range: 7..8,
2388                                     name: "A",
2389                                     kind: Struct,
2390                                     description: "struct A",
2391                                 },
2392                             },
2393                             HoverGotoTypeData {
2394                                 mod_path: "test::B",
2395                                 nav: NavigationTarget {
2396                                     file_id: FileId(
2397                                         0,
2398                                     ),
2399                                     full_range: 15..29,
2400                                     focus_range: 22..23,
2401                                     name: "B",
2402                                     kind: Struct,
2403                                     description: "struct B",
2404                                 },
2405                             },
2406                             HoverGotoTypeData {
2407                                 mod_path: "test::M::C",
2408                                 nav: NavigationTarget {
2409                                     file_id: FileId(
2410                                         0,
2411                                     ),
2412                                     full_range: 42..60,
2413                                     focus_range: 53..54,
2414                                     name: "C",
2415                                     kind: Struct,
2416                                     description: "pub struct C",
2417                                 },
2418                             },
2419                         ],
2420                     ),
2421                 ]
2422             "#]],
2423         );
2424     }
2425
2426     #[test]
2427     fn test_hover_return_impl_trait_has_goto_type_action() {
2428         check_actions(
2429             r#"
2430 trait Foo {}
2431 fn foo() -> impl Foo {}
2432
2433 fn main() { let s$0t = foo(); }
2434 "#,
2435             expect![[r#"
2436                 [
2437                     GoToType(
2438                         [
2439                             HoverGotoTypeData {
2440                                 mod_path: "test::Foo",
2441                                 nav: NavigationTarget {
2442                                     file_id: FileId(
2443                                         0,
2444                                     ),
2445                                     full_range: 0..12,
2446                                     focus_range: 6..9,
2447                                     name: "Foo",
2448                                     kind: Trait,
2449                                     description: "trait Foo",
2450                                 },
2451                             },
2452                         ],
2453                     ),
2454                 ]
2455             "#]],
2456         );
2457     }
2458
2459     #[test]
2460     fn test_hover_generic_return_impl_trait_has_goto_type_action() {
2461         check_actions(
2462             r#"
2463 trait Foo<T> {}
2464 struct S;
2465 fn foo() -> impl Foo<S> {}
2466
2467 fn main() { let s$0t = foo(); }
2468 "#,
2469             expect![[r#"
2470                 [
2471                     GoToType(
2472                         [
2473                             HoverGotoTypeData {
2474                                 mod_path: "test::Foo",
2475                                 nav: NavigationTarget {
2476                                     file_id: FileId(
2477                                         0,
2478                                     ),
2479                                     full_range: 0..15,
2480                                     focus_range: 6..9,
2481                                     name: "Foo",
2482                                     kind: Trait,
2483                                     description: "trait Foo",
2484                                 },
2485                             },
2486                             HoverGotoTypeData {
2487                                 mod_path: "test::S",
2488                                 nav: NavigationTarget {
2489                                     file_id: FileId(
2490                                         0,
2491                                     ),
2492                                     full_range: 16..25,
2493                                     focus_range: 23..24,
2494                                     name: "S",
2495                                     kind: Struct,
2496                                     description: "struct S",
2497                                 },
2498                             },
2499                         ],
2500                     ),
2501                 ]
2502             "#]],
2503         );
2504     }
2505
2506     #[test]
2507     fn test_hover_return_impl_traits_has_goto_type_action() {
2508         check_actions(
2509             r#"
2510 trait Foo {}
2511 trait Bar {}
2512 fn foo() -> impl Foo + Bar {}
2513
2514 fn main() { let s$0t = foo(); }
2515             "#,
2516             expect![[r#"
2517                 [
2518                     GoToType(
2519                         [
2520                             HoverGotoTypeData {
2521                                 mod_path: "test::Foo",
2522                                 nav: NavigationTarget {
2523                                     file_id: FileId(
2524                                         0,
2525                                     ),
2526                                     full_range: 0..12,
2527                                     focus_range: 6..9,
2528                                     name: "Foo",
2529                                     kind: Trait,
2530                                     description: "trait Foo",
2531                                 },
2532                             },
2533                             HoverGotoTypeData {
2534                                 mod_path: "test::Bar",
2535                                 nav: NavigationTarget {
2536                                     file_id: FileId(
2537                                         0,
2538                                     ),
2539                                     full_range: 13..25,
2540                                     focus_range: 19..22,
2541                                     name: "Bar",
2542                                     kind: Trait,
2543                                     description: "trait Bar",
2544                                 },
2545                             },
2546                         ],
2547                     ),
2548                 ]
2549             "#]],
2550         );
2551     }
2552
2553     #[test]
2554     fn test_hover_generic_return_impl_traits_has_goto_type_action() {
2555         check_actions(
2556             r#"
2557 trait Foo<T> {}
2558 trait Bar<T> {}
2559 struct S1 {}
2560 struct S2 {}
2561
2562 fn foo() -> impl Foo<S1> + Bar<S2> {}
2563
2564 fn main() { let s$0t = foo(); }
2565 "#,
2566             expect![[r#"
2567                 [
2568                     GoToType(
2569                         [
2570                             HoverGotoTypeData {
2571                                 mod_path: "test::Foo",
2572                                 nav: NavigationTarget {
2573                                     file_id: FileId(
2574                                         0,
2575                                     ),
2576                                     full_range: 0..15,
2577                                     focus_range: 6..9,
2578                                     name: "Foo",
2579                                     kind: Trait,
2580                                     description: "trait Foo",
2581                                 },
2582                             },
2583                             HoverGotoTypeData {
2584                                 mod_path: "test::Bar",
2585                                 nav: NavigationTarget {
2586                                     file_id: FileId(
2587                                         0,
2588                                     ),
2589                                     full_range: 16..31,
2590                                     focus_range: 22..25,
2591                                     name: "Bar",
2592                                     kind: Trait,
2593                                     description: "trait Bar",
2594                                 },
2595                             },
2596                             HoverGotoTypeData {
2597                                 mod_path: "test::S1",
2598                                 nav: NavigationTarget {
2599                                     file_id: FileId(
2600                                         0,
2601                                     ),
2602                                     full_range: 32..44,
2603                                     focus_range: 39..41,
2604                                     name: "S1",
2605                                     kind: Struct,
2606                                     description: "struct S1",
2607                                 },
2608                             },
2609                             HoverGotoTypeData {
2610                                 mod_path: "test::S2",
2611                                 nav: NavigationTarget {
2612                                     file_id: FileId(
2613                                         0,
2614                                     ),
2615                                     full_range: 45..57,
2616                                     focus_range: 52..54,
2617                                     name: "S2",
2618                                     kind: Struct,
2619                                     description: "struct S2",
2620                                 },
2621                             },
2622                         ],
2623                     ),
2624                 ]
2625             "#]],
2626         );
2627     }
2628
2629     #[test]
2630     fn test_hover_arg_impl_trait_has_goto_type_action() {
2631         check_actions(
2632             r#"
2633 trait Foo {}
2634 fn foo(ar$0g: &impl Foo) {}
2635 "#,
2636             expect![[r#"
2637                 [
2638                     GoToType(
2639                         [
2640                             HoverGotoTypeData {
2641                                 mod_path: "test::Foo",
2642                                 nav: NavigationTarget {
2643                                     file_id: FileId(
2644                                         0,
2645                                     ),
2646                                     full_range: 0..12,
2647                                     focus_range: 6..9,
2648                                     name: "Foo",
2649                                     kind: Trait,
2650                                     description: "trait Foo",
2651                                 },
2652                             },
2653                         ],
2654                     ),
2655                 ]
2656             "#]],
2657         );
2658     }
2659
2660     #[test]
2661     fn test_hover_arg_impl_traits_has_goto_type_action() {
2662         check_actions(
2663             r#"
2664 trait Foo {}
2665 trait Bar<T> {}
2666 struct S{}
2667
2668 fn foo(ar$0g: &impl Foo + Bar<S>) {}
2669 "#,
2670             expect![[r#"
2671                 [
2672                     GoToType(
2673                         [
2674                             HoverGotoTypeData {
2675                                 mod_path: "test::Foo",
2676                                 nav: NavigationTarget {
2677                                     file_id: FileId(
2678                                         0,
2679                                     ),
2680                                     full_range: 0..12,
2681                                     focus_range: 6..9,
2682                                     name: "Foo",
2683                                     kind: Trait,
2684                                     description: "trait Foo",
2685                                 },
2686                             },
2687                             HoverGotoTypeData {
2688                                 mod_path: "test::Bar",
2689                                 nav: NavigationTarget {
2690                                     file_id: FileId(
2691                                         0,
2692                                     ),
2693                                     full_range: 13..28,
2694                                     focus_range: 19..22,
2695                                     name: "Bar",
2696                                     kind: Trait,
2697                                     description: "trait Bar",
2698                                 },
2699                             },
2700                             HoverGotoTypeData {
2701                                 mod_path: "test::S",
2702                                 nav: NavigationTarget {
2703                                     file_id: FileId(
2704                                         0,
2705                                     ),
2706                                     full_range: 29..39,
2707                                     focus_range: 36..37,
2708                                     name: "S",
2709                                     kind: Struct,
2710                                     description: "struct S",
2711                                 },
2712                             },
2713                         ],
2714                     ),
2715                 ]
2716             "#]],
2717         );
2718     }
2719
2720     #[test]
2721     fn test_hover_async_block_impl_trait_has_goto_type_action() {
2722         check_actions(
2723             r#"
2724 struct S;
2725 fn foo() {
2726     let fo$0o = async { S };
2727 }
2728
2729 #[prelude_import] use future::*;
2730 mod future {
2731     #[lang = "future_trait"]
2732     pub trait Future { type Output; }
2733 }
2734 "#,
2735             expect![[r#"
2736                 [
2737                     GoToType(
2738                         [
2739                             HoverGotoTypeData {
2740                                 mod_path: "test::future::Future",
2741                                 nav: NavigationTarget {
2742                                     file_id: FileId(
2743                                         0,
2744                                     ),
2745                                     full_range: 101..163,
2746                                     focus_range: 140..146,
2747                                     name: "Future",
2748                                     kind: Trait,
2749                                     description: "pub trait Future",
2750                                 },
2751                             },
2752                             HoverGotoTypeData {
2753                                 mod_path: "test::S",
2754                                 nav: NavigationTarget {
2755                                     file_id: FileId(
2756                                         0,
2757                                     ),
2758                                     full_range: 0..9,
2759                                     focus_range: 7..8,
2760                                     name: "S",
2761                                     kind: Struct,
2762                                     description: "struct S",
2763                                 },
2764                             },
2765                         ],
2766                     ),
2767                 ]
2768             "#]],
2769         );
2770     }
2771
2772     #[test]
2773     fn test_hover_arg_generic_impl_trait_has_goto_type_action() {
2774         check_actions(
2775             r#"
2776 trait Foo<T> {}
2777 struct S {}
2778 fn foo(ar$0g: &impl Foo<S>) {}
2779 "#,
2780             expect![[r#"
2781                 [
2782                     GoToType(
2783                         [
2784                             HoverGotoTypeData {
2785                                 mod_path: "test::Foo",
2786                                 nav: NavigationTarget {
2787                                     file_id: FileId(
2788                                         0,
2789                                     ),
2790                                     full_range: 0..15,
2791                                     focus_range: 6..9,
2792                                     name: "Foo",
2793                                     kind: Trait,
2794                                     description: "trait Foo",
2795                                 },
2796                             },
2797                             HoverGotoTypeData {
2798                                 mod_path: "test::S",
2799                                 nav: NavigationTarget {
2800                                     file_id: FileId(
2801                                         0,
2802                                     ),
2803                                     full_range: 16..27,
2804                                     focus_range: 23..24,
2805                                     name: "S",
2806                                     kind: Struct,
2807                                     description: "struct S",
2808                                 },
2809                             },
2810                         ],
2811                     ),
2812                 ]
2813             "#]],
2814         );
2815     }
2816
2817     #[test]
2818     fn test_hover_dyn_return_has_goto_type_action() {
2819         check_actions(
2820             r#"
2821 trait Foo {}
2822 struct S;
2823 impl Foo for S {}
2824
2825 struct B<T>{}
2826 fn foo() -> B<dyn Foo> {}
2827
2828 fn main() { let s$0t = foo(); }
2829 "#,
2830             expect![[r#"
2831                 [
2832                     GoToType(
2833                         [
2834                             HoverGotoTypeData {
2835                                 mod_path: "test::B",
2836                                 nav: NavigationTarget {
2837                                     file_id: FileId(
2838                                         0,
2839                                     ),
2840                                     full_range: 42..55,
2841                                     focus_range: 49..50,
2842                                     name: "B",
2843                                     kind: Struct,
2844                                     description: "struct B",
2845                                 },
2846                             },
2847                             HoverGotoTypeData {
2848                                 mod_path: "test::Foo",
2849                                 nav: NavigationTarget {
2850                                     file_id: FileId(
2851                                         0,
2852                                     ),
2853                                     full_range: 0..12,
2854                                     focus_range: 6..9,
2855                                     name: "Foo",
2856                                     kind: Trait,
2857                                     description: "trait Foo",
2858                                 },
2859                             },
2860                         ],
2861                     ),
2862                 ]
2863             "#]],
2864         );
2865     }
2866
2867     #[test]
2868     fn test_hover_dyn_arg_has_goto_type_action() {
2869         check_actions(
2870             r#"
2871 trait Foo {}
2872 fn foo(ar$0g: &dyn Foo) {}
2873 "#,
2874             expect![[r#"
2875                 [
2876                     GoToType(
2877                         [
2878                             HoverGotoTypeData {
2879                                 mod_path: "test::Foo",
2880                                 nav: NavigationTarget {
2881                                     file_id: FileId(
2882                                         0,
2883                                     ),
2884                                     full_range: 0..12,
2885                                     focus_range: 6..9,
2886                                     name: "Foo",
2887                                     kind: Trait,
2888                                     description: "trait Foo",
2889                                 },
2890                             },
2891                         ],
2892                     ),
2893                 ]
2894             "#]],
2895         );
2896     }
2897
2898     #[test]
2899     fn test_hover_generic_dyn_arg_has_goto_type_action() {
2900         check_actions(
2901             r#"
2902 trait Foo<T> {}
2903 struct S {}
2904 fn foo(ar$0g: &dyn Foo<S>) {}
2905 "#,
2906             expect![[r#"
2907                 [
2908                     GoToType(
2909                         [
2910                             HoverGotoTypeData {
2911                                 mod_path: "test::Foo",
2912                                 nav: NavigationTarget {
2913                                     file_id: FileId(
2914                                         0,
2915                                     ),
2916                                     full_range: 0..15,
2917                                     focus_range: 6..9,
2918                                     name: "Foo",
2919                                     kind: Trait,
2920                                     description: "trait Foo",
2921                                 },
2922                             },
2923                             HoverGotoTypeData {
2924                                 mod_path: "test::S",
2925                                 nav: NavigationTarget {
2926                                     file_id: FileId(
2927                                         0,
2928                                     ),
2929                                     full_range: 16..27,
2930                                     focus_range: 23..24,
2931                                     name: "S",
2932                                     kind: Struct,
2933                                     description: "struct S",
2934                                 },
2935                             },
2936                         ],
2937                     ),
2938                 ]
2939             "#]],
2940         );
2941     }
2942
2943     #[test]
2944     fn test_hover_goto_type_action_links_order() {
2945         check_actions(
2946             r#"
2947 trait ImplTrait<T> {}
2948 trait DynTrait<T> {}
2949 struct B<T> {}
2950 struct S {}
2951
2952 fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
2953             "#,
2954             expect![[r#"
2955                 [
2956                     GoToType(
2957                         [
2958                             HoverGotoTypeData {
2959                                 mod_path: "test::ImplTrait",
2960                                 nav: NavigationTarget {
2961                                     file_id: FileId(
2962                                         0,
2963                                     ),
2964                                     full_range: 0..21,
2965                                     focus_range: 6..15,
2966                                     name: "ImplTrait",
2967                                     kind: Trait,
2968                                     description: "trait ImplTrait",
2969                                 },
2970                             },
2971                             HoverGotoTypeData {
2972                                 mod_path: "test::B",
2973                                 nav: NavigationTarget {
2974                                     file_id: FileId(
2975                                         0,
2976                                     ),
2977                                     full_range: 43..57,
2978                                     focus_range: 50..51,
2979                                     name: "B",
2980                                     kind: Struct,
2981                                     description: "struct B",
2982                                 },
2983                             },
2984                             HoverGotoTypeData {
2985                                 mod_path: "test::DynTrait",
2986                                 nav: NavigationTarget {
2987                                     file_id: FileId(
2988                                         0,
2989                                     ),
2990                                     full_range: 22..42,
2991                                     focus_range: 28..36,
2992                                     name: "DynTrait",
2993                                     kind: Trait,
2994                                     description: "trait DynTrait",
2995                                 },
2996                             },
2997                             HoverGotoTypeData {
2998                                 mod_path: "test::S",
2999                                 nav: NavigationTarget {
3000                                     file_id: FileId(
3001                                         0,
3002                                     ),
3003                                     full_range: 58..69,
3004                                     focus_range: 65..66,
3005                                     name: "S",
3006                                     kind: Struct,
3007                                     description: "struct S",
3008                                 },
3009                             },
3010                         ],
3011                     ),
3012                 ]
3013             "#]],
3014         );
3015     }
3016
3017     #[test]
3018     fn test_hover_associated_type_has_goto_type_action() {
3019         check_actions(
3020             r#"
3021 trait Foo {
3022     type Item;
3023     fn get(self) -> Self::Item {}
3024 }
3025
3026 struct Bar{}
3027 struct S{}
3028
3029 impl Foo for S { type Item = Bar; }
3030
3031 fn test() -> impl Foo { S {} }
3032
3033 fn main() { let s$0t = test().get(); }
3034 "#,
3035             expect![[r#"
3036                 [
3037                     GoToType(
3038                         [
3039                             HoverGotoTypeData {
3040                                 mod_path: "test::Foo",
3041                                 nav: NavigationTarget {
3042                                     file_id: FileId(
3043                                         0,
3044                                     ),
3045                                     full_range: 0..62,
3046                                     focus_range: 6..9,
3047                                     name: "Foo",
3048                                     kind: Trait,
3049                                     description: "trait Foo",
3050                                 },
3051                             },
3052                         ],
3053                     ),
3054                 ]
3055             "#]],
3056         );
3057     }
3058
3059     #[test]
3060     fn test_hover_const_param_has_goto_type_action() {
3061         check_actions(
3062             r#"
3063 struct Bar;
3064 struct Foo<const BAR: Bar>;
3065
3066 impl<const BAR: Bar> Foo<BAR$0> {}
3067 "#,
3068             expect![[r#"
3069                 [
3070                     GoToType(
3071                         [
3072                             HoverGotoTypeData {
3073                                 mod_path: "test::Bar",
3074                                 nav: NavigationTarget {
3075                                     file_id: FileId(
3076                                         0,
3077                                     ),
3078                                     full_range: 0..11,
3079                                     focus_range: 7..10,
3080                                     name: "Bar",
3081                                     kind: Struct,
3082                                     description: "struct Bar",
3083                                 },
3084                             },
3085                         ],
3086                     ),
3087                 ]
3088             "#]],
3089         );
3090     }
3091
3092     #[test]
3093     fn test_hover_type_param_has_goto_type_action() {
3094         check_actions(
3095             r#"
3096 trait Foo {}
3097
3098 fn foo<T: Foo>(t: T$0){}
3099 "#,
3100             expect![[r#"
3101                 [
3102                     GoToType(
3103                         [
3104                             HoverGotoTypeData {
3105                                 mod_path: "test::Foo",
3106                                 nav: NavigationTarget {
3107                                     file_id: FileId(
3108                                         0,
3109                                     ),
3110                                     full_range: 0..12,
3111                                     focus_range: 6..9,
3112                                     name: "Foo",
3113                                     kind: Trait,
3114                                     description: "trait Foo",
3115                                 },
3116                             },
3117                         ],
3118                     ),
3119                 ]
3120             "#]],
3121         );
3122     }
3123
3124     #[test]
3125     fn test_hover_self_has_go_to_type() {
3126         check_actions(
3127             r#"
3128 struct Foo;
3129 impl Foo {
3130     fn foo(&self$0) {}
3131 }
3132 "#,
3133             expect![[r#"
3134                 [
3135                     GoToType(
3136                         [
3137                             HoverGotoTypeData {
3138                                 mod_path: "test::Foo",
3139                                 nav: NavigationTarget {
3140                                     file_id: FileId(
3141                                         0,
3142                                     ),
3143                                     full_range: 0..11,
3144                                     focus_range: 7..10,
3145                                     name: "Foo",
3146                                     kind: Struct,
3147                                     description: "struct Foo",
3148                                 },
3149                             },
3150                         ],
3151                     ),
3152                 ]
3153             "#]],
3154         );
3155     }
3156
3157     #[test]
3158     fn hover_displays_normalized_crate_names() {
3159         check(
3160             r#"
3161 //- /lib.rs crate:name-with-dashes
3162 pub mod wrapper {
3163     pub struct Thing { x: u32 }
3164
3165     impl Thing {
3166         pub fn new() -> Thing { Thing { x: 0 } }
3167     }
3168 }
3169
3170 //- /main.rs crate:main deps:name-with-dashes
3171 fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
3172 "#,
3173             expect![[r#"
3174             *new*
3175
3176             ```rust
3177             name_with_dashes::wrapper::Thing
3178             ```
3179
3180             ```rust
3181             pub fn new() -> Thing
3182             ```
3183             "#]],
3184         )
3185     }
3186
3187     #[test]
3188     fn hover_field_pat_shorthand_ref_match_ergonomics() {
3189         check(
3190             r#"
3191 struct S {
3192     f: i32,
3193 }
3194
3195 fn main() {
3196     let s = S { f: 0 };
3197     let S { f$0 } = &s;
3198 }
3199 "#,
3200             expect![[r#"
3201                 *f*
3202
3203                 ```rust
3204                 &i32
3205                 ```
3206             "#]],
3207         );
3208     }
3209
3210     #[test]
3211     fn hover_self_param_shows_type() {
3212         check(
3213             r#"
3214 struct Foo {}
3215 impl Foo {
3216     fn bar(&sel$0f) {}
3217 }
3218 "#,
3219             expect![[r#"
3220                 *self*
3221
3222                 ```rust
3223                 &Foo
3224                 ```
3225             "#]],
3226         );
3227     }
3228
3229     #[test]
3230     fn hover_self_param_shows_type_for_arbitrary_self_type() {
3231         check(
3232             r#"
3233 struct Arc<T>(T);
3234 struct Foo {}
3235 impl Foo {
3236     fn bar(sel$0f: Arc<Foo>) {}
3237 }
3238 "#,
3239             expect![[r#"
3240                 *self*
3241
3242                 ```rust
3243                 Arc<Foo>
3244                 ```
3245             "#]],
3246         );
3247     }
3248
3249     #[test]
3250     fn hover_doc_outer_inner() {
3251         check(
3252             r#"
3253 /// Be quick;
3254 mod Foo$0 {
3255     //! time is mana
3256
3257     /// This comment belongs to the function
3258     fn foo() {}
3259 }
3260 "#,
3261             expect![[r#"
3262                 *Foo*
3263
3264                 ```rust
3265                 test
3266                 ```
3267
3268                 ```rust
3269                 mod Foo
3270                 ```
3271
3272                 ---
3273
3274                 Be quick;
3275                 time is mana
3276             "#]],
3277         );
3278     }
3279
3280     #[test]
3281     fn hover_doc_outer_inner_attribue() {
3282         check(
3283             r#"
3284 #[doc = "Be quick;"]
3285 mod Foo$0 {
3286     #![doc = "time is mana"]
3287
3288     #[doc = "This comment belongs to the function"]
3289     fn foo() {}
3290 }
3291 "#,
3292             expect![[r#"
3293                 *Foo*
3294
3295                 ```rust
3296                 test
3297                 ```
3298
3299                 ```rust
3300                 mod Foo
3301                 ```
3302
3303                 ---
3304
3305                 Be quick;
3306                 time is mana
3307             "#]],
3308         );
3309     }
3310
3311     #[test]
3312     fn hover_comments_dont_highlight_parent() {
3313         check_hover_no_result(
3314             r#"
3315 fn no_hover() {
3316     // no$0hover
3317 }
3318 "#,
3319         );
3320     }
3321
3322     #[test]
3323     fn hover_label() {
3324         check(
3325             r#"
3326 fn foo() {
3327     'label$0: loop {}
3328 }
3329 "#,
3330             expect![[r#"
3331             *'label*
3332
3333             ```rust
3334             'label
3335             ```
3336             "#]],
3337         );
3338     }
3339
3340     #[test]
3341     fn hover_lifetime() {
3342         check(
3343             r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#,
3344             expect![[r#"
3345             *'lifetime*
3346
3347             ```rust
3348             'lifetime
3349             ```
3350             "#]],
3351         );
3352     }
3353
3354     #[test]
3355     fn hover_type_param() {
3356         check(
3357             r#"
3358 struct Foo<T>(T);
3359 trait Copy {}
3360 trait Clone {}
3361 trait Sized {}
3362 impl<T: Copy + Clone> Foo<T$0> where T: Sized {}
3363 "#,
3364             expect![[r#"
3365                 *T*
3366
3367                 ```rust
3368                 T: Copy + Clone + Sized
3369                 ```
3370             "#]],
3371         );
3372         check(
3373             r#"
3374 struct Foo<T>(T);
3375 impl<T> Foo<T$0> {}
3376 "#,
3377             expect![[r#"
3378                 *T*
3379
3380                 ```rust
3381                 T
3382                 ```
3383                 "#]],
3384         );
3385         // lifetimes aren't being substituted yet
3386         check(
3387             r#"
3388 struct Foo<T>(T);
3389 impl<T: 'static> Foo<T$0> {}
3390 "#,
3391             expect![[r#"
3392                 *T*
3393
3394                 ```rust
3395                 T: {error}
3396                 ```
3397                 "#]],
3398         );
3399     }
3400
3401     #[test]
3402     fn hover_const_param() {
3403         check(
3404             r#"
3405 struct Foo<const LEN: usize>;
3406 impl<const LEN: usize> Foo<LEN$0> {}
3407 "#,
3408             expect![[r#"
3409                 *LEN*
3410
3411                 ```rust
3412                 const LEN: usize
3413                 ```
3414             "#]],
3415         );
3416     }
3417 }