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