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