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