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