]> git.lizzy.rs Git - rust.git/blob - crates/ide/src/hover.rs
Merge #9316
[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 //- minicore: future
3004 struct S;
3005 fn foo() {
3006     let fo$0o = async { S };
3007 }
3008 "#,
3009             expect![[r#"
3010                 [
3011                     GoToType(
3012                         [
3013                             HoverGotoTypeData {
3014                                 mod_path: "core::future::Future",
3015                                 nav: NavigationTarget {
3016                                     file_id: FileId(
3017                                         1,
3018                                     ),
3019                                     full_range: 246..428,
3020                                     focus_range: 285..291,
3021                                     name: "Future",
3022                                     kind: Trait,
3023                                     description: "pub trait Future",
3024                                 },
3025                             },
3026                             HoverGotoTypeData {
3027                                 mod_path: "test::S",
3028                                 nav: NavigationTarget {
3029                                     file_id: FileId(
3030                                         0,
3031                                     ),
3032                                     full_range: 0..9,
3033                                     focus_range: 7..8,
3034                                     name: "S",
3035                                     kind: Struct,
3036                                     description: "struct S",
3037                                 },
3038                             },
3039                         ],
3040                     ),
3041                 ]
3042             "#]],
3043         );
3044     }
3045
3046     #[test]
3047     fn test_hover_arg_generic_impl_trait_has_goto_type_action() {
3048         check_actions(
3049             r#"
3050 trait Foo<T> {}
3051 struct S {}
3052 fn foo(ar$0g: &impl Foo<S>) {}
3053 "#,
3054             expect![[r#"
3055                 [
3056                     GoToType(
3057                         [
3058                             HoverGotoTypeData {
3059                                 mod_path: "test::Foo",
3060                                 nav: NavigationTarget {
3061                                     file_id: FileId(
3062                                         0,
3063                                     ),
3064                                     full_range: 0..15,
3065                                     focus_range: 6..9,
3066                                     name: "Foo",
3067                                     kind: Trait,
3068                                     description: "trait Foo<T>",
3069                                 },
3070                             },
3071                             HoverGotoTypeData {
3072                                 mod_path: "test::S",
3073                                 nav: NavigationTarget {
3074                                     file_id: FileId(
3075                                         0,
3076                                     ),
3077                                     full_range: 16..27,
3078                                     focus_range: 23..24,
3079                                     name: "S",
3080                                     kind: Struct,
3081                                     description: "struct S",
3082                                 },
3083                             },
3084                         ],
3085                     ),
3086                 ]
3087             "#]],
3088         );
3089     }
3090
3091     #[test]
3092     fn test_hover_dyn_return_has_goto_type_action() {
3093         check_actions(
3094             r#"
3095 trait Foo {}
3096 struct S;
3097 impl Foo for S {}
3098
3099 struct B<T>{}
3100 fn foo() -> B<dyn Foo> {}
3101
3102 fn main() { let s$0t = foo(); }
3103 "#,
3104             expect![[r#"
3105                 [
3106                     GoToType(
3107                         [
3108                             HoverGotoTypeData {
3109                                 mod_path: "test::B",
3110                                 nav: NavigationTarget {
3111                                     file_id: FileId(
3112                                         0,
3113                                     ),
3114                                     full_range: 42..55,
3115                                     focus_range: 49..50,
3116                                     name: "B",
3117                                     kind: Struct,
3118                                     description: "struct B<T>",
3119                                 },
3120                             },
3121                             HoverGotoTypeData {
3122                                 mod_path: "test::Foo",
3123                                 nav: NavigationTarget {
3124                                     file_id: FileId(
3125                                         0,
3126                                     ),
3127                                     full_range: 0..12,
3128                                     focus_range: 6..9,
3129                                     name: "Foo",
3130                                     kind: Trait,
3131                                     description: "trait Foo",
3132                                 },
3133                             },
3134                         ],
3135                     ),
3136                 ]
3137             "#]],
3138         );
3139     }
3140
3141     #[test]
3142     fn test_hover_dyn_arg_has_goto_type_action() {
3143         check_actions(
3144             r#"
3145 trait Foo {}
3146 fn foo(ar$0g: &dyn Foo) {}
3147 "#,
3148             expect![[r#"
3149                 [
3150                     GoToType(
3151                         [
3152                             HoverGotoTypeData {
3153                                 mod_path: "test::Foo",
3154                                 nav: NavigationTarget {
3155                                     file_id: FileId(
3156                                         0,
3157                                     ),
3158                                     full_range: 0..12,
3159                                     focus_range: 6..9,
3160                                     name: "Foo",
3161                                     kind: Trait,
3162                                     description: "trait Foo",
3163                                 },
3164                             },
3165                         ],
3166                     ),
3167                 ]
3168             "#]],
3169         );
3170     }
3171
3172     #[test]
3173     fn test_hover_generic_dyn_arg_has_goto_type_action() {
3174         check_actions(
3175             r#"
3176 trait Foo<T> {}
3177 struct S {}
3178 fn foo(ar$0g: &dyn Foo<S>) {}
3179 "#,
3180             expect![[r#"
3181                 [
3182                     GoToType(
3183                         [
3184                             HoverGotoTypeData {
3185                                 mod_path: "test::Foo",
3186                                 nav: NavigationTarget {
3187                                     file_id: FileId(
3188                                         0,
3189                                     ),
3190                                     full_range: 0..15,
3191                                     focus_range: 6..9,
3192                                     name: "Foo",
3193                                     kind: Trait,
3194                                     description: "trait Foo<T>",
3195                                 },
3196                             },
3197                             HoverGotoTypeData {
3198                                 mod_path: "test::S",
3199                                 nav: NavigationTarget {
3200                                     file_id: FileId(
3201                                         0,
3202                                     ),
3203                                     full_range: 16..27,
3204                                     focus_range: 23..24,
3205                                     name: "S",
3206                                     kind: Struct,
3207                                     description: "struct S",
3208                                 },
3209                             },
3210                         ],
3211                     ),
3212                 ]
3213             "#]],
3214         );
3215     }
3216
3217     #[test]
3218     fn test_hover_goto_type_action_links_order() {
3219         check_actions(
3220             r#"
3221 trait ImplTrait<T> {}
3222 trait DynTrait<T> {}
3223 struct B<T> {}
3224 struct S {}
3225
3226 fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
3227             "#,
3228             expect![[r#"
3229                 [
3230                     GoToType(
3231                         [
3232                             HoverGotoTypeData {
3233                                 mod_path: "test::ImplTrait",
3234                                 nav: NavigationTarget {
3235                                     file_id: FileId(
3236                                         0,
3237                                     ),
3238                                     full_range: 0..21,
3239                                     focus_range: 6..15,
3240                                     name: "ImplTrait",
3241                                     kind: Trait,
3242                                     description: "trait ImplTrait<T>",
3243                                 },
3244                             },
3245                             HoverGotoTypeData {
3246                                 mod_path: "test::B",
3247                                 nav: NavigationTarget {
3248                                     file_id: FileId(
3249                                         0,
3250                                     ),
3251                                     full_range: 43..57,
3252                                     focus_range: 50..51,
3253                                     name: "B",
3254                                     kind: Struct,
3255                                     description: "struct B<T>",
3256                                 },
3257                             },
3258                             HoverGotoTypeData {
3259                                 mod_path: "test::DynTrait",
3260                                 nav: NavigationTarget {
3261                                     file_id: FileId(
3262                                         0,
3263                                     ),
3264                                     full_range: 22..42,
3265                                     focus_range: 28..36,
3266                                     name: "DynTrait",
3267                                     kind: Trait,
3268                                     description: "trait DynTrait<T>",
3269                                 },
3270                             },
3271                             HoverGotoTypeData {
3272                                 mod_path: "test::S",
3273                                 nav: NavigationTarget {
3274                                     file_id: FileId(
3275                                         0,
3276                                     ),
3277                                     full_range: 58..69,
3278                                     focus_range: 65..66,
3279                                     name: "S",
3280                                     kind: Struct,
3281                                     description: "struct S",
3282                                 },
3283                             },
3284                         ],
3285                     ),
3286                 ]
3287             "#]],
3288         );
3289     }
3290
3291     #[test]
3292     fn test_hover_associated_type_has_goto_type_action() {
3293         check_actions(
3294             r#"
3295 trait Foo {
3296     type Item;
3297     fn get(self) -> Self::Item {}
3298 }
3299
3300 struct Bar{}
3301 struct S{}
3302
3303 impl Foo for S { type Item = Bar; }
3304
3305 fn test() -> impl Foo { S {} }
3306
3307 fn main() { let s$0t = test().get(); }
3308 "#,
3309             expect![[r#"
3310                 [
3311                     GoToType(
3312                         [
3313                             HoverGotoTypeData {
3314                                 mod_path: "test::Foo",
3315                                 nav: NavigationTarget {
3316                                     file_id: FileId(
3317                                         0,
3318                                     ),
3319                                     full_range: 0..62,
3320                                     focus_range: 6..9,
3321                                     name: "Foo",
3322                                     kind: Trait,
3323                                     description: "trait Foo",
3324                                 },
3325                             },
3326                         ],
3327                     ),
3328                 ]
3329             "#]],
3330         );
3331     }
3332
3333     #[test]
3334     fn test_hover_const_param_has_goto_type_action() {
3335         check_actions(
3336             r#"
3337 struct Bar;
3338 struct Foo<const BAR: Bar>;
3339
3340 impl<const BAR: Bar> Foo<BAR$0> {}
3341 "#,
3342             expect![[r#"
3343                 [
3344                     GoToType(
3345                         [
3346                             HoverGotoTypeData {
3347                                 mod_path: "test::Bar",
3348                                 nav: NavigationTarget {
3349                                     file_id: FileId(
3350                                         0,
3351                                     ),
3352                                     full_range: 0..11,
3353                                     focus_range: 7..10,
3354                                     name: "Bar",
3355                                     kind: Struct,
3356                                     description: "struct Bar",
3357                                 },
3358                             },
3359                         ],
3360                     ),
3361                 ]
3362             "#]],
3363         );
3364     }
3365
3366     #[test]
3367     fn test_hover_type_param_has_goto_type_action() {
3368         check_actions(
3369             r#"
3370 trait Foo {}
3371
3372 fn foo<T: Foo>(t: T$0){}
3373 "#,
3374             expect![[r#"
3375                 [
3376                     GoToType(
3377                         [
3378                             HoverGotoTypeData {
3379                                 mod_path: "test::Foo",
3380                                 nav: NavigationTarget {
3381                                     file_id: FileId(
3382                                         0,
3383                                     ),
3384                                     full_range: 0..12,
3385                                     focus_range: 6..9,
3386                                     name: "Foo",
3387                                     kind: Trait,
3388                                     description: "trait Foo",
3389                                 },
3390                             },
3391                         ],
3392                     ),
3393                 ]
3394             "#]],
3395         );
3396     }
3397
3398     #[test]
3399     fn test_hover_self_has_go_to_type() {
3400         check_actions(
3401             r#"
3402 struct Foo;
3403 impl Foo {
3404     fn foo(&self$0) {}
3405 }
3406 "#,
3407             expect![[r#"
3408                 [
3409                     GoToType(
3410                         [
3411                             HoverGotoTypeData {
3412                                 mod_path: "test::Foo",
3413                                 nav: NavigationTarget {
3414                                     file_id: FileId(
3415                                         0,
3416                                     ),
3417                                     full_range: 0..11,
3418                                     focus_range: 7..10,
3419                                     name: "Foo",
3420                                     kind: Struct,
3421                                     description: "struct Foo",
3422                                 },
3423                             },
3424                         ],
3425                     ),
3426                 ]
3427             "#]],
3428         );
3429     }
3430
3431     #[test]
3432     fn hover_displays_normalized_crate_names() {
3433         check(
3434             r#"
3435 //- /lib.rs crate:name-with-dashes
3436 pub mod wrapper {
3437     pub struct Thing { x: u32 }
3438
3439     impl Thing {
3440         pub fn new() -> Thing { Thing { x: 0 } }
3441     }
3442 }
3443
3444 //- /main.rs crate:main deps:name-with-dashes
3445 fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
3446 "#,
3447             expect![[r#"
3448             *new*
3449
3450             ```rust
3451             name_with_dashes::wrapper::Thing
3452             ```
3453
3454             ```rust
3455             pub fn new() -> Thing
3456             ```
3457             "#]],
3458         )
3459     }
3460
3461     #[test]
3462     fn hover_field_pat_shorthand_ref_match_ergonomics() {
3463         check(
3464             r#"
3465 struct S {
3466     f: i32,
3467 }
3468
3469 fn main() {
3470     let s = S { f: 0 };
3471     let S { f$0 } = &s;
3472 }
3473 "#,
3474             expect![[r#"
3475                 *f*
3476
3477                 ```rust
3478                 f: &i32
3479                 ```
3480             "#]],
3481         );
3482     }
3483
3484     #[test]
3485     fn hover_self_param_shows_type() {
3486         check(
3487             r#"
3488 struct Foo {}
3489 impl Foo {
3490     fn bar(&sel$0f) {}
3491 }
3492 "#,
3493             expect![[r#"
3494                 *self*
3495
3496                 ```rust
3497                 self: &Foo
3498                 ```
3499             "#]],
3500         );
3501     }
3502
3503     #[test]
3504     fn hover_self_param_shows_type_for_arbitrary_self_type() {
3505         check(
3506             r#"
3507 struct Arc<T>(T);
3508 struct Foo {}
3509 impl Foo {
3510     fn bar(sel$0f: Arc<Foo>) {}
3511 }
3512 "#,
3513             expect![[r#"
3514                 *self*
3515
3516                 ```rust
3517                 self: Arc<Foo>
3518                 ```
3519             "#]],
3520         );
3521     }
3522
3523     #[test]
3524     fn hover_doc_outer_inner() {
3525         check(
3526             r#"
3527 /// Be quick;
3528 mod Foo$0 {
3529     //! time is mana
3530
3531     /// This comment belongs to the function
3532     fn foo() {}
3533 }
3534 "#,
3535             expect![[r#"
3536                 *Foo*
3537
3538                 ```rust
3539                 test
3540                 ```
3541
3542                 ```rust
3543                 mod Foo
3544                 ```
3545
3546                 ---
3547
3548                 Be quick;
3549                 time is mana
3550             "#]],
3551         );
3552     }
3553
3554     #[test]
3555     fn hover_doc_outer_inner_attribue() {
3556         check(
3557             r#"
3558 #[doc = "Be quick;"]
3559 mod Foo$0 {
3560     #![doc = "time is mana"]
3561
3562     #[doc = "This comment belongs to the function"]
3563     fn foo() {}
3564 }
3565 "#,
3566             expect![[r#"
3567                 *Foo*
3568
3569                 ```rust
3570                 test
3571                 ```
3572
3573                 ```rust
3574                 mod Foo
3575                 ```
3576
3577                 ---
3578
3579                 Be quick;
3580                 time is mana
3581             "#]],
3582         );
3583     }
3584
3585     #[test]
3586     fn hover_doc_block_style_indentend() {
3587         check(
3588             r#"
3589 /**
3590     foo
3591     ```rust
3592     let x = 3;
3593     ```
3594 */
3595 fn foo$0() {}
3596 "#,
3597             expect![[r#"
3598                 *foo*
3599
3600                 ```rust
3601                 test
3602                 ```
3603
3604                 ```rust
3605                 fn foo()
3606                 ```
3607
3608                 ---
3609
3610                 foo
3611
3612                 ```rust
3613                 let x = 3;
3614                 ```
3615             "#]],
3616         );
3617     }
3618
3619     #[test]
3620     fn hover_comments_dont_highlight_parent() {
3621         cov_mark::check!(no_highlight_on_comment_hover);
3622         check_hover_no_result(
3623             r#"
3624 fn no_hover() {
3625     // no$0hover
3626 }
3627 "#,
3628         );
3629     }
3630
3631     #[test]
3632     fn hover_label() {
3633         check(
3634             r#"
3635 fn foo() {
3636     'label$0: loop {}
3637 }
3638 "#,
3639             expect![[r#"
3640             *'label*
3641
3642             ```rust
3643             'label
3644             ```
3645             "#]],
3646         );
3647     }
3648
3649     #[test]
3650     fn hover_lifetime() {
3651         check(
3652             r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#,
3653             expect![[r#"
3654             *'lifetime*
3655
3656             ```rust
3657             'lifetime
3658             ```
3659             "#]],
3660         );
3661     }
3662
3663     #[test]
3664     fn hover_type_param() {
3665         check(
3666             r#"
3667 struct Foo<T>(T);
3668 trait Copy {}
3669 trait Clone {}
3670 trait Sized {}
3671 impl<T: Copy + Clone> Foo<T$0> where T: Sized {}
3672 "#,
3673             expect![[r#"
3674                 *T*
3675
3676                 ```rust
3677                 T: Copy + Clone + Sized
3678                 ```
3679             "#]],
3680         );
3681         check(
3682             r#"
3683 struct Foo<T>(T);
3684 impl<T> Foo<T$0> {}
3685 "#,
3686             expect![[r#"
3687                 *T*
3688
3689                 ```rust
3690                 T
3691                 ```
3692                 "#]],
3693         );
3694         // lifetimes bounds arent being tracked yet
3695         check(
3696             r#"
3697 struct Foo<T>(T);
3698 impl<T: 'static> Foo<T$0> {}
3699 "#,
3700             expect![[r#"
3701                 *T*
3702
3703                 ```rust
3704                 T
3705                 ```
3706                 "#]],
3707         );
3708     }
3709
3710     #[test]
3711     fn hover_const_param() {
3712         check(
3713             r#"
3714 struct Foo<const LEN: usize>;
3715 impl<const LEN: usize> Foo<LEN$0> {}
3716 "#,
3717             expect![[r#"
3718                 *LEN*
3719
3720                 ```rust
3721                 const LEN: usize
3722                 ```
3723             "#]],
3724         );
3725     }
3726
3727     #[test]
3728     fn hover_const_pat() {
3729         check(
3730             r#"
3731 /// This is a doc
3732 const FOO: usize = 3;
3733 fn foo() {
3734     match 5 {
3735         FOO$0 => (),
3736         _ => ()
3737     }
3738 }
3739 "#,
3740             expect![[r#"
3741                 *FOO*
3742
3743                 ```rust
3744                 test
3745                 ```
3746
3747                 ```rust
3748                 const FOO: usize
3749                 ```
3750
3751                 ---
3752
3753                 This is a doc
3754             "#]],
3755         );
3756     }
3757
3758     #[test]
3759     fn hover_mod_def() {
3760         check(
3761             r#"
3762 //- /main.rs
3763 mod foo$0;
3764 //- /foo.rs
3765 //! For the horde!
3766 "#,
3767             expect![[r#"
3768                 *foo*
3769
3770                 ```rust
3771                 test
3772                 ```
3773
3774                 ```rust
3775                 mod foo
3776                 ```
3777
3778                 ---
3779
3780                 For the horde!
3781             "#]],
3782         );
3783     }
3784
3785     #[test]
3786     fn hover_self_in_use() {
3787         check(
3788             r#"
3789 //! This should not appear
3790 mod foo {
3791     /// But this should appear
3792     pub mod bar {}
3793 }
3794 use foo::bar::{self$0};
3795 "#,
3796             expect![[r#"
3797                 *self*
3798
3799                 ```rust
3800                 test::foo
3801                 ```
3802
3803                 ```rust
3804                 mod bar
3805                 ```
3806
3807                 ---
3808
3809                 But this should appear
3810             "#]],
3811         )
3812     }
3813
3814     #[test]
3815     fn hover_keyword() {
3816         let ra_fixture = r#"//- /main.rs crate:main deps:std
3817 fn f() { retur$0n; }"#;
3818         let fixture = format!("{}\n{}", ra_fixture, FamousDefs::FIXTURE);
3819         check(
3820             &fixture,
3821             expect![[r#"
3822                 *return*
3823
3824                 ```rust
3825                 return
3826                 ```
3827
3828                 ---
3829
3830                 Docs for return_keyword
3831             "#]],
3832         );
3833     }
3834
3835     #[test]
3836     fn hover_builtin() {
3837         let ra_fixture = r#"//- /main.rs crate:main deps:std
3838 cosnt _: &str$0 = ""; }"#;
3839         let fixture = format!("{}\n{}", ra_fixture, FamousDefs::FIXTURE);
3840         check(
3841             &fixture,
3842             expect![[r#"
3843                 *str*
3844
3845                 ```rust
3846                 str
3847                 ```
3848
3849                 ---
3850
3851                 Docs for prim_str
3852             "#]],
3853         );
3854     }
3855
3856     #[test]
3857     fn hover_macro_expanded_function() {
3858         check(
3859             r#"
3860 struct S<'a, T>(&'a T);
3861 trait Clone {}
3862 macro_rules! foo {
3863     () => {
3864         fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where
3865             't: 't + 't,
3866             for<'a> T: Clone + 'a
3867         { 0 as _ }
3868     };
3869 }
3870
3871 foo!();
3872
3873 fn main() {
3874     bar$0;
3875 }
3876 "#,
3877             expect![[r#"
3878                 *bar*
3879
3880                 ```rust
3881                 test
3882                 ```
3883
3884                 ```rust
3885                 fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32
3886                 where
3887                     T: Clone + 't,
3888                     't: 't + 't,
3889                     for<'a> T: Clone + 'a,
3890                 ```
3891             "#]],
3892         )
3893     }
3894
3895     #[test]
3896     fn hover_intra_doc_links() {
3897         check(
3898             r#"
3899
3900 pub mod theitem {
3901     /// This is the item. Cool!
3902     pub struct TheItem;
3903 }
3904
3905 /// Gives you a [`TheItem$0`].
3906 ///
3907 /// [`TheItem`]: theitem::TheItem
3908 pub fn gimme() -> theitem::TheItem {
3909     theitem::TheItem
3910 }
3911 "#,
3912             expect![[r#"
3913                 *[`TheItem`]*
3914
3915                 ```rust
3916                 test::theitem
3917                 ```
3918
3919                 ```rust
3920                 pub struct TheItem
3921                 ```
3922
3923                 ---
3924
3925                 This is the item. Cool!
3926             "#]],
3927         );
3928     }
3929
3930     #[test]
3931     fn hover_generic_assoc() {
3932         check(
3933             r#"
3934 fn foo<T: A>() where T::Assoc$0: {}
3935
3936 trait A {
3937     type Assoc;
3938 }"#,
3939             expect![[r#"
3940                 *Assoc*
3941
3942                 ```rust
3943                 test
3944                 ```
3945
3946                 ```rust
3947                 type Assoc
3948                 ```
3949             "#]],
3950         );
3951         check(
3952             r#"
3953 fn foo<T: A>() {
3954     let _: <T>::Assoc$0;
3955 }
3956
3957 trait A {
3958     type Assoc;
3959 }"#,
3960             expect![[r#"
3961                 *Assoc*
3962
3963                 ```rust
3964                 test
3965                 ```
3966
3967                 ```rust
3968                 type Assoc
3969                 ```
3970             "#]],
3971         );
3972         check(
3973             r#"
3974 trait A where
3975     Self::Assoc$0: ,
3976 {
3977     type Assoc;
3978 }"#,
3979             expect![[r#"
3980                 *Assoc*
3981
3982                 ```rust
3983                 test
3984                 ```
3985
3986                 ```rust
3987                 type Assoc
3988                 ```
3989             "#]],
3990         );
3991     }
3992
3993     #[test]
3994     fn string_shadowed_with_inner_items() {
3995         check(
3996             r#"
3997 //- /main.rs crate:main deps:alloc
3998
3999 /// Custom `String` type.
4000 struct String;
4001
4002 fn f() {
4003     let _: String$0;
4004
4005     fn inner() {}
4006 }
4007
4008 //- /alloc.rs crate:alloc
4009 #[prelude_import]
4010 pub use string::*;
4011
4012 mod string {
4013     /// This is `alloc::String`.
4014     pub struct String;
4015 }
4016             "#,
4017             expect![[r#"
4018                 *String*
4019
4020                 ```rust
4021                 main
4022                 ```
4023
4024                 ```rust
4025                 struct String
4026                 ```
4027
4028                 ---
4029
4030                 Custom `String` type.
4031             "#]],
4032         )
4033     }
4034
4035     #[test]
4036     fn function_doesnt_shadow_crate_in_use_tree() {
4037         check(
4038             r#"
4039 //- /main.rs crate:main deps:foo
4040 use foo$0::{foo};
4041
4042 //- /foo.rs crate:foo
4043 pub fn foo() {}
4044 "#,
4045             expect![[r#"
4046                 *foo*
4047
4048                 ```rust
4049                 extern crate foo
4050                 ```
4051             "#]],
4052         )
4053     }
4054
4055     #[test]
4056     fn hover_feature() {
4057         check(
4058             r#"#![feature(box_syntax$0)]"#,
4059             expect![[r##"
4060                 *box_syntax*
4061                 ```
4062                 box_syntax
4063                 ```
4064                 ___
4065
4066                 # `box_syntax`
4067
4068                 The tracking issue for this feature is: [#49733]
4069
4070                 [#49733]: https://github.com/rust-lang/rust/issues/49733
4071
4072                 See also [`box_patterns`](box-patterns.md)
4073
4074                 ------------------------
4075
4076                 Currently the only stable way to create a `Box` is via the `Box::new` method.
4077                 Also it is not possible in stable Rust to destructure a `Box` in a match
4078                 pattern. The unstable `box` keyword can be used to create a `Box`. An example
4079                 usage would be:
4080
4081                 ```rust
4082                 #![feature(box_syntax)]
4083
4084                 fn main() {
4085                     let b = box 5;
4086                 }
4087                 ```
4088
4089             "##]],
4090         )
4091     }
4092
4093     #[test]
4094     fn hover_lint() {
4095         check(
4096             r#"#![allow(arithmetic_overflow$0)]"#,
4097             expect![[r#"
4098                 *arithmetic_overflow*
4099                 ```
4100                 arithmetic_overflow
4101                 ```
4102                 ___
4103
4104                 arithmetic operation overflows
4105             "#]],
4106         )
4107     }
4108
4109     #[test]
4110     fn hover_clippy_lint() {
4111         check(
4112             r#"#![allow(clippy::almost_swapped$0)]"#,
4113             expect![[r#"
4114                 *almost_swapped*
4115                 ```
4116                 clippy::almost_swapped
4117                 ```
4118                 ___
4119
4120                 Checks for `foo = bar; bar = foo` sequences.
4121             "#]],
4122         )
4123     }
4124 }