2 use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
4 base_db::SourceDatabase,
5 defs::{Definition, NameClass, NameRefClass},
7 generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
12 use itertools::Itertools;
15 algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxKind::*, SyntaxToken, TokenAtOffset,
20 display::{macro_label, TryToNav},
22 doc_attributes, extract_definitions_from_markdown, remove_links, resolve_doc_path_for_def,
25 markdown_remove::remove_markdown,
27 runnables::{runnable_fn, runnable_mod},
28 FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,
31 #[derive(Clone, Debug, PartialEq, Eq)]
32 pub struct HoverConfig {
33 pub links_in_hover: bool,
34 pub documentation: Option<HoverDocFormat>,
38 fn markdown(&self) -> bool {
39 matches!(self.documentation, Some(HoverDocFormat::Markdown))
43 #[derive(Clone, Debug, PartialEq, Eq)]
44 pub enum HoverDocFormat {
49 #[derive(Debug, Clone)]
50 pub enum HoverAction {
52 Implementation(FilePosition),
53 Reference(FilePosition),
54 GoToType(Vec<HoverGotoTypeData>),
57 #[derive(Debug, Clone, Eq, PartialEq)]
58 pub struct HoverGotoTypeData {
60 pub nav: NavigationTarget,
63 /// Contains the results when hovering over an item
64 #[derive(Debug, Default)]
65 pub struct HoverResult {
67 pub actions: Vec<HoverAction>,
72 // Shows additional information, like type of an expression or documentation for definition when "focusing" code.
73 // Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.
75 // image::https://user-images.githubusercontent.com/48062697/113020658-b5f98b80-917a-11eb-9f88-3dbc27320c95.gif[]
78 position: FilePosition,
80 ) -> Option<RangeInfo<HoverResult>> {
81 let sema = hir::Semantics::new(db);
82 let file = sema.parse(position.file_id).syntax().clone();
83 let token = pick_best(file.token_at_offset(position.offset))?;
84 let token = sema.descend_into_macros(token);
86 let mut res = HoverResult::default();
88 let node = token.parent()?;
90 let definition = match_ast! {
92 // we don't use NameClass::referenced_or_defined here as we do not want to resolve
93 // field pattern shorthands to their definition
94 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
95 NameClass::ConstReference(def) => Some(def),
96 def => def.defined(db),
98 ast::NameRef(name_ref) => {
99 NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(db))
101 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime).map_or_else(
102 || NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(db)),
107 if ast::Comment::cast(token.clone()).is_some() {
108 cov_mark::hit!(no_highlight_on_comment_hover);
109 let (attributes, def) = doc_attributes(&sema, &node)?;
110 let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
111 let (idl_range, link, ns) =
112 extract_definitions_from_markdown(docs.as_str()).into_iter().find_map(|(range, link, ns)| {
113 let hir::InFile { file_id, value: range } = doc_mapping.map(range)?;
114 if file_id == position.file_id.into() && range.contains(position.offset) {
115 Some((range, link, ns))
120 range = Some(idl_range);
121 resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef)
122 } else if let res@Some(_) = try_hover_for_attribute(&token) {
131 if let Some(definition) = definition {
132 let famous_defs = match &definition {
133 Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
134 Some(FamousDefs(&sema, sema.scope(&node).krate()))
138 if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref(), config) {
139 res.markup = process_markup(sema.db, definition, &markup, config);
140 if let Some(action) = show_implementations_action(db, definition) {
141 res.actions.push(action);
144 if let Some(action) = show_fn_references_action(db, definition) {
145 res.actions.push(action);
148 if let Some(action) = runnable_action(&sema, definition, position.file_id) {
149 res.actions.push(action);
152 if let Some(action) = goto_type_action(db, definition) {
153 res.actions.push(action);
156 let range = range.unwrap_or_else(|| sema.original_range(&node).range);
157 return Some(RangeInfo::new(range, res));
161 if let res @ Some(_) = hover_for_keyword(&sema, config, &token) {
167 .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
169 let ty = match_ast! {
171 ast::Expr(it) => sema.type_of_expr(&it)?,
172 ast::Pat(it) => sema.type_of_pat(&it)?,
173 // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
174 // (e.g expanding a builtin macro). So we give up here.
175 ast::MacroCall(_it) => return None,
180 res.markup = if config.markdown() {
181 Markup::fenced_block(&ty.display(db))
183 ty.display(db).to_string().into()
185 let range = sema.original_range(&node).range;
186 Some(RangeInfo::new(range, res))
189 fn try_hover_for_attribute(token: &SyntaxToken) -> Option<RangeInfo<HoverResult>> {
190 let attr = token.ancestors().find_map(ast::Attr::cast)?;
191 let (path, tt) = attr.as_simple_call()?;
192 if !tt.syntax().text_range().contains(token.text_range().start()) {
195 let (is_clippy, lints) = match &*path {
196 "feature" => (false, FEATURES),
197 "allow" | "deny" | "forbid" | "warn" => {
198 let is_clippy = algo::non_trivia_sibling(token.clone().into(), Direction::Prev)
199 .filter(|t| t.kind() == T![:])
200 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
201 .filter(|t| t.kind() == T![:])
202 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
204 t.kind() == T![ident] && t.into_token().map_or(false, |t| t.text() == "clippy")
209 (false, DEFAULT_LINTS)
216 let needle = if is_clippy {
217 tmp = format!("clippy::{}", token.text());
224 lints.binary_search_by_key(&needle, |lint| lint.label).ok().map(|idx| &lints[idx])?;
228 markup: Markup::from(format!("```\n{}\n```\n___\n\n{}", lint.label, lint.description)),
234 fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
235 fn to_action(nav_target: NavigationTarget) -> HoverAction {
236 HoverAction::Implementation(FilePosition {
237 file_id: nav_target.file_id,
238 offset: nav_target.focus_or_full_range().start(),
242 let adt = match def {
243 Definition::ModuleDef(hir::ModuleDef::Trait(it)) => {
244 return it.try_to_nav(db).map(to_action)
246 Definition::ModuleDef(hir::ModuleDef::Adt(it)) => Some(it),
247 Definition::SelfType(it) => it.self_ty(db).as_adt(),
250 adt.try_to_nav(db).map(to_action)
253 fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
255 Definition::ModuleDef(hir::ModuleDef::Function(it)) => {
256 it.try_to_nav(db).map(|nav_target| {
257 HoverAction::Reference(FilePosition {
258 file_id: nav_target.file_id,
259 offset: nav_target.focus_or_full_range().start(),
268 sema: &hir::Semantics<RootDatabase>,
271 ) -> Option<HoverAction> {
273 Definition::ModuleDef(it) => match it {
274 hir::ModuleDef::Module(it) => runnable_mod(sema, it).map(HoverAction::Runnable),
275 hir::ModuleDef::Function(func) => {
276 let src = func.source(sema.db)?;
277 if src.file_id != file_id.into() {
278 cov_mark::hit!(hover_macro_generated_struct_fn_doc_comment);
279 cov_mark::hit!(hover_macro_generated_struct_fn_doc_attr);
283 runnable_fn(sema, func).map(HoverAction::Runnable)
291 fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
292 let mut targets: Vec<hir::ModuleDef> = Vec::new();
293 let mut push_new_def = |item: hir::ModuleDef| {
294 if !targets.contains(&item) {
299 if let Definition::GenericParam(hir::GenericParam::TypeParam(it)) = def {
300 it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
303 Definition::Local(it) => it.ty(db),
304 Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
309 if let Some(adt) = t.as_adt() {
310 push_new_def(adt.into());
311 } else if let Some(trait_) = t.as_dyn_trait() {
312 push_new_def(trait_.into());
313 } else if let Some(traits) = t.as_impl_traits(db) {
314 traits.into_iter().for_each(|it| push_new_def(it.into()));
315 } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
316 push_new_def(trait_.into());
321 let targets = targets
324 Some(HoverGotoTypeData {
325 mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
326 nav: it.try_to_nav(db)?,
331 Some(HoverAction::GoToType(targets))
334 fn hover_markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Option<Markup> {
335 let mut buf = String::new();
337 if let Some(mod_path) = mod_path {
338 if !mod_path.is_empty() {
339 format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
342 format_to!(buf, "```rust\n{}\n```", desc);
344 if let Some(doc) = docs {
345 format_to!(buf, "\n___\n\n{}", doc);
354 config: &HoverConfig,
356 let markup = markup.as_str();
357 let markup = if !config.markdown() {
358 remove_markdown(markup)
359 } else if config.links_in_hover {
360 rewrite_links(db, markup, &def)
367 fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
369 Definition::Field(f) => Some(f.parent_def(db).name(db)),
370 Definition::Local(l) => l.parent(db).name(db),
371 Definition::ModuleDef(md) => match md {
372 hir::ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
373 hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
374 hir::AssocItemContainer::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
376 hir::ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)),
381 .map(|name| name.to_string())
384 fn render_path(db: &RootDatabase, module: hir::Module, item_name: Option<String>) -> String {
386 db.crate_graph()[module.krate().into()].display_name.as_ref().map(|it| it.to_string());
387 let module_path = module
391 .flat_map(|it| it.name(db).map(|name| name.to_string()));
392 crate_name.into_iter().chain(module_path).chain(item_name).join("::")
395 fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
396 if let Definition::GenericParam(_) = def {
399 def.module(db).map(|module| render_path(db, module, definition_owner_name(db, def)))
402 fn hover_for_definition(
405 famous_defs: Option<&FamousDefs>,
406 config: &HoverConfig,
407 ) -> Option<Markup> {
408 let mod_path = definition_mod_path(db, &def);
409 let (label, docs) = match def {
410 Definition::Macro(it) => match &it.source(db)?.value {
411 Either::Left(mac) => {
412 let label = macro_label(mac);
413 (label, it.attrs(db).docs())
415 Either::Right(_) => {
420 Definition::Field(def) => label_and_docs(db, def),
421 Definition::ModuleDef(it) => match it {
422 hir::ModuleDef::Module(it) => label_and_docs(db, it),
423 hir::ModuleDef::Function(it) => label_and_docs(db, it),
424 hir::ModuleDef::Adt(it) => label_and_docs(db, it),
425 hir::ModuleDef::Variant(it) => label_and_docs(db, it),
426 hir::ModuleDef::Const(it) => label_and_docs(db, it),
427 hir::ModuleDef::Static(it) => label_and_docs(db, it),
428 hir::ModuleDef::Trait(it) => label_and_docs(db, it),
429 hir::ModuleDef::TypeAlias(it) => label_and_docs(db, it),
430 hir::ModuleDef::BuiltinType(it) => {
432 .and_then(|fd| hover_for_builtin(fd, it))
433 .or_else(|| Some(Markup::fenced_block(&it.name())))
436 Definition::Local(it) => return hover_for_local(it, db),
437 Definition::SelfType(impl_def) => {
438 impl_def.self_ty(db).as_adt().map(|adt| label_and_docs(db, adt))?
440 Definition::GenericParam(it) => label_and_docs(db, it),
441 Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
445 docs.filter(|_| config.documentation.is_some()).map(Into::into),
450 fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
452 D: HasAttrs + HirDisplay,
454 let label = def.display(db).to_string();
455 let docs = def.attrs(db).docs();
460 fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option<Markup> {
462 let ty = ty.display(db);
463 let is_mut = if it.is_mut(db) { "mut " } else { "" };
464 let desc = match it.source(db).value {
465 Either::Left(ident) => {
466 let name = it.name(db).unwrap();
467 let let_kw = if ident
470 .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION)
476 format!("{}{}{}: {}", let_kw, is_mut, name, ty)
478 Either::Right(_) => format!("{}self: {}", is_mut, ty),
480 hover_markup(None, desc, None)
483 fn hover_for_keyword(
484 sema: &Semantics<RootDatabase>,
485 config: &HoverConfig,
487 ) -> Option<RangeInfo<HoverResult>> {
488 if !token.kind().is_keyword() || !config.documentation.is_some() {
491 let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate());
492 // std exposes {}_keyword modules with docstrings on the root to document keywords
493 let keyword_mod = format!("{}_keyword", token.text());
494 let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
495 let docs = doc_owner.attrs(sema.db).docs()?;
496 let markup = process_markup(
498 Definition::ModuleDef(doc_owner.into()),
499 &hover_markup(Some(docs.into()), token.text().into(), None)?,
502 Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
505 fn hover_for_builtin(famous_defs: &FamousDefs, builtin: hir::BuiltinType) -> Option<Markup> {
506 // std exposes prim_{} modules with docstrings on the root to document the builtins
507 let primitive_mod = format!("prim_{}", builtin.name());
508 let doc_owner = find_std_module(famous_defs, &primitive_mod)?;
509 let docs = doc_owner.attrs(famous_defs.0.db).docs()?;
510 hover_markup(Some(docs.into()), builtin.name().to_string(), None)
513 fn find_std_module(famous_defs: &FamousDefs, name: &str) -> Option<hir::Module> {
514 let db = famous_defs.0.db;
515 let std_crate = famous_defs.std()?;
516 let std_root_module = std_crate.root_module(db);
519 .find(|module| module.name(db).map_or(false, |module| module.to_string() == name))
522 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
523 return tokens.max_by_key(priority);
525 fn priority(n: &SyntaxToken) -> usize {
527 IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3,
528 T!['('] | T![')'] => 2,
529 kind if kind.is_trivia() => 0,
537 use expect_test::{expect, Expect};
538 use ide_db::base_db::FileLoader;
540 use crate::{fixture, hover::HoverDocFormat, HoverConfig};
542 fn check_hover_no_result(ra_fixture: &str) {
543 let (analysis, position) = fixture::position(ra_fixture);
548 links_in_hover: true,
549 documentation: Some(HoverDocFormat::Markdown)
556 fn check(ra_fixture: &str, expect: Expect) {
557 let (analysis, position) = fixture::position(ra_fixture);
562 links_in_hover: true,
563 documentation: Some(HoverDocFormat::Markdown),
569 let content = analysis.db.file_text(position.file_id);
570 let hovered_element = &content[hover.range];
572 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
573 expect.assert_eq(&actual)
576 fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
577 let (analysis, position) = fixture::position(ra_fixture);
582 links_in_hover: false,
583 documentation: Some(HoverDocFormat::Markdown),
589 let content = analysis.db.file_text(position.file_id);
590 let hovered_element = &content[hover.range];
592 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
593 expect.assert_eq(&actual)
596 fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
597 let (analysis, position) = fixture::position(ra_fixture);
602 links_in_hover: true,
603 documentation: Some(HoverDocFormat::PlainText),
609 let content = analysis.db.file_text(position.file_id);
610 let hovered_element = &content[hover.range];
612 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
613 expect.assert_eq(&actual)
616 fn check_actions(ra_fixture: &str, expect: Expect) {
617 let (analysis, position) = fixture::position(ra_fixture);
622 links_in_hover: true,
623 documentation: Some(HoverDocFormat::Markdown),
628 expect.assert_debug_eq(&hover.info.actions)
632 fn hover_shows_type_of_an_expression() {
635 pub fn foo() -> u32 { 1 }
638 let foo_test = foo()$0;
651 fn hover_remove_markdown_if_configured() {
652 check_hover_no_markdown(
654 pub fn foo() -> u32 { 1 }
657 let foo_test = foo()$0;
668 fn hover_shows_long_type_of_an_expression() {
671 struct Scan<A, B, C> { a: A, b: B, c: C }
672 struct Iter<I> { inner: I }
673 enum Option<T> { Some(T), None }
675 struct OtherStruct<T> { i: T }
677 fn scan<A, B, C>(a: A, b: B, c: C) -> Iter<Scan<OtherStruct<A>, B, C>> {
678 Iter { inner: Scan { a, b, c } }
683 let closure = |memo: &mut u32, value: &u32, _another: &mut u32| -> Option<u32> {
684 Option::Some(*memo + value)
687 let mut iter$0 = scan(OtherStruct { i: num }, closure, number);
694 let mut iter: Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>>
701 fn hover_shows_fn_signature() {
702 // Single file with result
705 pub fn foo() -> u32 { 1 }
707 fn main() { let foo_test = fo$0o(); }
722 // Multiple candidates but results are ambiguous.
726 pub fn foo() -> u32 { 1 }
729 pub fn foo() -> &str { "" }
732 pub fn foo(a: u32, b: u32) {}
739 fn main() { let foo_test = fo$0o(); }
751 fn hover_shows_fn_signature_with_type_params() {
754 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { }
756 fn main() { let foo_test = fo$0o(); }
766 pub fn foo<'a, T>(b: &'a T) -> &'a str
775 fn hover_shows_fn_signature_on_fn_name() {
778 pub fn foo$0(a: u32, b: u32) -> u32 {}
790 pub fn foo(a: u32, b: u32) -> u32
797 fn hover_shows_fn_doc() {
802 /// # use std::path::Path;
804 /// foo(Path::new("hello, world!"))
806 pub fn foo$0(_: &Path) {}
826 # use std::path::Path;
828 foo(Path::new("hello, world!"))
835 fn hover_shows_fn_doc_attr_raw_string() {
838 #[doc = r#"Raw string doc attr"#]
839 pub fn foo$0(_: &Path) {}
862 fn hover_shows_struct_field_info() {
863 // Hovering over the field when instantiating
866 struct Foo { field_a: u32 }
869 let foo = Foo { field_a$0: 0, };
885 // Hovering over the field in the definition
888 struct Foo { field_a$0: u32 }
891 let foo = Foo { field_a: 0 };
909 fn hover_const_static() {
911 r#"const foo$0: u32 = 123;"#,
925 r#"static foo$0: u32 = 456;"#,
941 fn hover_default_generic_types() {
944 struct Test<K, T = u8> { k: K, t: T }
947 let zz$0 = Test { t: 23u8, k: 33 };
953 let zz: Test<i32, u8>
963 enum Option<T> { Some(T) }
966 fn main() { So$0me(12); }
983 enum Option<T> { Some(T) }
986 fn main() { let b$0ar = Some(12); }
999 fn hover_enum_variant() {
1003 /// The None variant
1027 /// The Some variant
1031 let s = Option::Som$0e(12);
1053 fn hover_for_local_variable() {
1055 r#"fn func(foo: i32) { fo$0o; }"#,
1067 fn hover_for_local_variable_pat() {
1069 r#"fn func(fo$0o: i32) {}"#,
1081 fn hover_local_var_edge() {
1083 r#"fn func(foo: i32) { if true { $0foo; }; }"#,
1095 fn hover_for_param_edge() {
1097 r#"fn func($0foo: i32) {}"#,
1109 fn hover_for_param_with_multiple_traits() {
1112 type Target: ?Sized;
1115 type Target: ?Sized;
1117 fn f(_x$0: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#,
1122 _x: impl Deref<Target = u8> + DerefMut<Target = u8>
1129 fn test_hover_infer_associated_method_result() {
1132 struct Thing { x: u32 }
1135 fn new() -> Thing { Thing { x: 0 } }
1138 fn main() { let foo_$0test = Thing::new(); }
1151 fn test_hover_infer_associated_method_exact() {
1155 struct Thing { x: u32 }
1158 fn new() -> Thing { Thing { x: 0 } }
1162 fn main() { let foo_test = wrapper::Thing::new$0(); }
1168 test::wrapper::Thing
1179 fn test_hover_infer_associated_const_in_pattern() {
1210 fn test_hover_self() {
1213 struct Thing { x: u32 }
1215 fn new() -> Self { Self$0 { x: 0 } }
1232 struct Thing { x: u32 }
1234 fn new() -> Self$0 { Self { x: 0 } }
1253 pub fn new() -> Self$0 { Thing::A }
1272 pub fn thing(a: Self$0) {}
1290 fn test_hover_shadowing_pat() {
1311 fn test_hover_macro_invocation() {
1314 macro_rules! foo { () => {} }
1316 fn f() { fo$0o!(); }
1333 fn test_hover_macro2_invocation() {
1341 fn f() { fo$0o!(); }
1364 fn test_hover_tuple_field() {
1366 r#"struct TS(String, i32$0);"#,
1378 fn test_hover_through_macro() {
1381 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1384 fn bar() { fo$0o(); }
1402 fn test_hover_through_expr_in_macro() {
1405 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1406 fn foo(bar:u32) { let a = id!(ba$0r); }
1419 fn test_hover_through_expr_in_macro_recursive() {
1422 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1423 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1424 fn foo(bar:u32) { let a = id!(ba$0r); }
1437 fn test_hover_through_func_in_macro_recursive() {
1440 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1441 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1442 fn bar() -> u32 { 0 }
1443 fn foo() { let a = id!([0u32, bar($0)] ); }
1455 fn test_hover_through_literal_string_in_macro() {
1458 macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } }
1460 let mastered_for_itunes = "";
1461 let _ = arr!("Tr$0acks", &mastered_for_itunes);
1474 fn test_hover_through_assert_macro() {
1477 #[rustc_builtin_macro]
1478 macro_rules! assert {}
1480 fn bar() -> bool { true }
1500 fn test_hover_through_literal_string_in_builtin_macro() {
1501 check_hover_no_result(
1503 #[rustc_builtin_macro]
1504 macro_rules! format {}
1507 format!("hel$0lo {}", 0);
1514 fn test_hover_non_ascii_space_doc() {
1517 /// <- `\u{3000}` here
1520 fn bar() { fo$0o(); }
1541 fn test_hover_function_show_qualifiers() {
1543 r#"async fn foo$0() {}"#,
1557 r#"pub const unsafe fn foo$0() {}"#,
1566 pub const unsafe fn foo()
1570 // Top level `pub(crate)` will be displayed as no visibility.
1572 r#"mod m { pub(crate) async unsafe extern "C" fn foo$0() {} }"#,
1581 pub(crate) async unsafe extern "C" fn foo()
1588 fn test_hover_trait_show_qualifiers() {
1590 r"unsafe trait foo$0() {}",
1607 fn test_hover_extern_crate() {
1610 //- /main.rs crate:main deps:std
1612 //- /std/lib.rs crate:std
1613 //! Standard library for this test
1627 Standard library for this test
1635 //- /main.rs crate:main deps:std
1636 extern crate std as ab$0c;
1637 //- /std/lib.rs crate:std
1638 //! Standard library for this test
1652 Standard library for this test
1661 fn test_hover_mod_with_same_name_as_function() {
1664 use self::m$0y::Bar;
1665 mod my { pub struct Bar; }
1684 fn test_hover_struct_doc_comment() {
1687 /// This is an example
1695 /// assert_eq!(6, my_crate::add_one(5));
1699 fn foo() { let bar = Ba$0r; }
1722 assert_eq!(6, my_crate::add_one(5));
1729 fn test_hover_struct_doc_attr() {
1735 fn foo() { let bar = Ba$0r; }
1756 fn test_hover_struct_doc_attr_multiple_and_mixed() {
1760 #[doc = "bar docs 1"]
1761 #[doc = "bar docs 2"]
1764 fn foo() { let bar = Ba$0r; }
1787 fn test_hover_path_link() {
1791 /// [Foo](struct.Foo.html)
1807 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1813 fn test_hover_path_link_no_strip() {
1817 /// [struct Foo](struct.Foo.html)
1833 [struct Foo](https://docs.rs/test/*/test/struct.Foo.html)
1839 fn test_hover_path_link_field() {
1841 // [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1846 /// [Foo](struct.Foo.html)
1863 [Foo](struct.Foo.html)
1869 fn test_hover_intra_link() {
1891 [Foo](https://docs.rs/test/*/test/foo/struct.Foo.html)
1897 fn test_hover_intra_link_html_root_url() {
1900 #![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
1921 [Foo](https://example.com/test/foo/struct.Foo.html)
1927 fn test_hover_intra_link_shortlink() {
1947 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1953 fn test_hover_intra_link_shortlink_code() {
1973 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1979 fn test_hover_intra_link_namespaced() {
2000 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
2006 fn test_hover_intra_link_shortlink_namspaced_code() {
2026 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2032 fn test_hover_intra_link_shortlink_namspaced_code_with_at() {
2052 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2058 fn test_hover_intra_link_reference() {
2080 [my Foo](https://docs.rs/test/*/test/struct.Foo.html)
2085 fn test_hover_intra_link_reference_to_trait_method() {
2093 /// [buzz]: Foo::buzz
2109 [Foo](https://docs.rs/test/*/test/trait.Foo.html#tymethod.buzz)
2115 fn test_hover_external_url() {
2119 /// [external](https://www.google.com)
2135 [external](https://www.google.com)
2140 // Check that we don't rewrite links which we can't identify
2142 fn test_hover_unknown_target() {
2168 fn test_doc_links_enum_variant() {
2189 [E](https://docs.rs/test/*/test/enum.E.html)
2195 fn test_doc_links_field() {
2216 [`S`](https://docs.rs/test/*/test/struct.S.html)
2222 fn test_hover_no_links() {
2223 check_hover_no_links(
2226 /// case 1. bare URL: https://www.example.com/
2227 /// case 2. inline URL with title: [example](https://www.example.com/)
2228 /// case 3. code reference: [`Result`]
2229 /// case 4. code reference but miss footnote: [`String`]
2230 /// case 5. autolink: <http://www.example.com/>
2231 /// case 6. email address: <test@example.com>
2232 /// case 7. reference: [example][example]
2233 /// case 8. collapsed link: [example][]
2234 /// case 9. shortcut link: [example]
2235 /// case 10. inline without URL: [example]()
2236 /// case 11. reference: [foo][foo]
2237 /// case 12. reference: [foo][bar]
2238 /// case 13. collapsed link: [foo][]
2239 /// case 14. shortcut link: [foo]
2240 /// case 15. inline without URL: [foo]()
2241 /// case 16. just escaped text: \[foo]
2242 /// case 17. inline link: [Foo](foo::Foo)
2244 /// [`Result`]: ../../std/result/enum.Result.html
2245 /// [^example]: https://www.example.com/
2262 case 1. bare URL: https://www.example.com/
2263 case 2. inline URL with title: [example](https://www.example.com/)
2264 case 3. code reference: `Result`
2265 case 4. code reference but miss footnote: `String`
2266 case 5. autolink: http://www.example.com/
2267 case 6. email address: test@example.com
2268 case 7. reference: example
2269 case 8. collapsed link: example
2270 case 9. shortcut link: example
2271 case 10. inline without URL: example
2272 case 11. reference: foo
2273 case 12. reference: foo
2274 case 13. collapsed link: foo
2275 case 14. shortcut link: foo
2276 case 15. inline without URL: foo
2277 case 16. just escaped text: \[foo\]
2278 case 17. inline link: Foo
2280 [^example]: https://www.example.com/
2286 fn test_hover_macro_generated_struct_fn_doc_comment() {
2287 cov_mark::check!(hover_macro_generated_struct_fn_doc_comment);
2303 fn foo() { let bar = Bar; bar.fo$0o(); }
2324 fn test_hover_macro_generated_struct_fn_doc_attr() {
2325 cov_mark::check!(hover_macro_generated_struct_fn_doc_attr);
2333 #[doc = "Do the foo"]
2341 fn foo() { let bar = Bar; bar.fo$0o(); }
2362 fn test_hover_trait_has_impl_action() {
2364 r#"trait foo$0() {}"#,
2381 fn test_hover_struct_has_impl_action() {
2383 r"struct foo$0() {}",
2400 fn test_hover_union_has_impl_action() {
2402 r#"union foo$0() {}"#,
2419 fn test_hover_enum_has_impl_action() {
2421 r"enum foo$0() { A, B }",
2438 fn test_hover_self_has_impl_action() {
2440 r#"struct foo where Self$0:;"#,
2457 fn test_hover_test_has_action() {
2475 nav: NavigationTarget {
2480 focus_range: 11..19,
2501 fn test_hover_test_mod_has_action() {
2513 nav: NavigationTarget {
2534 fn test_hover_struct_has_goto_type_action() {
2539 fn main() { let s$0t = S{ f1:0 }; }
2546 mod_path: "test::S",
2547 nav: NavigationTarget {
2555 description: "struct S",
2566 fn test_hover_generic_struct_has_goto_type_actions() {
2570 struct S<T>{ f1: T }
2572 fn main() { let s$0t = S{ f1:Arg(0) }; }
2579 mod_path: "test::S",
2580 nav: NavigationTarget {
2585 focus_range: 24..25,
2588 description: "struct S<T>",
2592 mod_path: "test::Arg",
2593 nav: NavigationTarget {
2601 description: "struct Arg",
2612 fn test_hover_generic_struct_has_flattened_goto_type_actions() {
2616 struct S<T>{ f1: T }
2618 fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
2625 mod_path: "test::S",
2626 nav: NavigationTarget {
2631 focus_range: 24..25,
2634 description: "struct S<T>",
2638 mod_path: "test::Arg",
2639 nav: NavigationTarget {
2647 description: "struct Arg",
2658 fn test_hover_tuple_has_goto_type_actions() {
2667 fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
2674 mod_path: "test::A",
2675 nav: NavigationTarget {
2683 description: "struct A",
2687 mod_path: "test::B",
2688 nav: NavigationTarget {
2693 focus_range: 22..23,
2696 description: "struct B",
2700 mod_path: "test::M::C",
2701 nav: NavigationTarget {
2706 focus_range: 53..54,
2709 description: "pub struct C",
2720 fn test_hover_return_impl_trait_has_goto_type_action() {
2724 fn foo() -> impl Foo {}
2726 fn main() { let s$0t = foo(); }
2733 mod_path: "test::Foo",
2734 nav: NavigationTarget {
2742 description: "trait Foo",
2753 fn test_hover_generic_return_impl_trait_has_goto_type_action() {
2758 fn foo() -> impl Foo<S> {}
2760 fn main() { let s$0t = foo(); }
2767 mod_path: "test::Foo",
2768 nav: NavigationTarget {
2776 description: "trait Foo<T>",
2780 mod_path: "test::S",
2781 nav: NavigationTarget {
2786 focus_range: 23..24,
2789 description: "struct S",
2800 fn test_hover_return_impl_traits_has_goto_type_action() {
2805 fn foo() -> impl Foo + Bar {}
2807 fn main() { let s$0t = foo(); }
2814 mod_path: "test::Foo",
2815 nav: NavigationTarget {
2823 description: "trait Foo",
2827 mod_path: "test::Bar",
2828 nav: NavigationTarget {
2833 focus_range: 19..22,
2836 description: "trait Bar",
2847 fn test_hover_generic_return_impl_traits_has_goto_type_action() {
2855 fn foo() -> impl Foo<S1> + Bar<S2> {}
2857 fn main() { let s$0t = foo(); }
2864 mod_path: "test::Foo",
2865 nav: NavigationTarget {
2873 description: "trait Foo<T>",
2877 mod_path: "test::Bar",
2878 nav: NavigationTarget {
2883 focus_range: 22..25,
2886 description: "trait Bar<T>",
2890 mod_path: "test::S1",
2891 nav: NavigationTarget {
2896 focus_range: 39..41,
2899 description: "struct S1",
2903 mod_path: "test::S2",
2904 nav: NavigationTarget {
2909 focus_range: 52..54,
2912 description: "struct S2",
2923 fn test_hover_arg_impl_trait_has_goto_type_action() {
2927 fn foo(ar$0g: &impl Foo) {}
2934 mod_path: "test::Foo",
2935 nav: NavigationTarget {
2943 description: "trait Foo",
2954 fn test_hover_arg_impl_traits_has_goto_type_action() {
2961 fn foo(ar$0g: &impl Foo + Bar<S>) {}
2968 mod_path: "test::Foo",
2969 nav: NavigationTarget {
2977 description: "trait Foo",
2981 mod_path: "test::Bar",
2982 nav: NavigationTarget {
2987 focus_range: 19..22,
2990 description: "trait Bar<T>",
2994 mod_path: "test::S",
2995 nav: NavigationTarget {
3000 focus_range: 36..37,
3003 description: "struct S",
3014 fn test_hover_async_block_impl_trait_has_goto_type_action() {
3017 //- minicore: future
3020 let fo$0o = async { S };
3028 mod_path: "core::future::Future",
3029 nav: NavigationTarget {
3033 full_range: 251..433,
3034 focus_range: 290..296,
3037 description: "pub trait Future",
3041 mod_path: "test::S",
3042 nav: NavigationTarget {
3050 description: "struct S",
3061 fn test_hover_arg_generic_impl_trait_has_goto_type_action() {
3066 fn foo(ar$0g: &impl Foo<S>) {}
3073 mod_path: "test::Foo",
3074 nav: NavigationTarget {
3082 description: "trait Foo<T>",
3086 mod_path: "test::S",
3087 nav: NavigationTarget {
3092 focus_range: 23..24,
3095 description: "struct S",
3106 fn test_hover_dyn_return_has_goto_type_action() {
3114 fn foo() -> B<dyn Foo> {}
3116 fn main() { let s$0t = foo(); }
3123 mod_path: "test::B",
3124 nav: NavigationTarget {
3129 focus_range: 49..50,
3132 description: "struct B<T>",
3136 mod_path: "test::Foo",
3137 nav: NavigationTarget {
3145 description: "trait Foo",
3156 fn test_hover_dyn_arg_has_goto_type_action() {
3160 fn foo(ar$0g: &dyn Foo) {}
3167 mod_path: "test::Foo",
3168 nav: NavigationTarget {
3176 description: "trait Foo",
3187 fn test_hover_generic_dyn_arg_has_goto_type_action() {
3192 fn foo(ar$0g: &dyn Foo<S>) {}
3199 mod_path: "test::Foo",
3200 nav: NavigationTarget {
3208 description: "trait Foo<T>",
3212 mod_path: "test::S",
3213 nav: NavigationTarget {
3218 focus_range: 23..24,
3221 description: "struct S",
3232 fn test_hover_goto_type_action_links_order() {
3235 trait ImplTrait<T> {}
3236 trait DynTrait<T> {}
3240 fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
3247 mod_path: "test::ImplTrait",
3248 nav: NavigationTarget {
3256 description: "trait ImplTrait<T>",
3260 mod_path: "test::B",
3261 nav: NavigationTarget {
3266 focus_range: 50..51,
3269 description: "struct B<T>",
3273 mod_path: "test::DynTrait",
3274 nav: NavigationTarget {
3279 focus_range: 28..36,
3282 description: "trait DynTrait<T>",
3286 mod_path: "test::S",
3287 nav: NavigationTarget {
3292 focus_range: 65..66,
3295 description: "struct S",
3306 fn test_hover_associated_type_has_goto_type_action() {
3311 fn get(self) -> Self::Item {}
3317 impl Foo for S { type Item = Bar; }
3319 fn test() -> impl Foo { S {} }
3321 fn main() { let s$0t = test().get(); }
3328 mod_path: "test::Foo",
3329 nav: NavigationTarget {
3337 description: "trait Foo",
3348 fn test_hover_const_param_has_goto_type_action() {
3352 struct Foo<const BAR: Bar>;
3354 impl<const BAR: Bar> Foo<BAR$0> {}
3361 mod_path: "test::Bar",
3362 nav: NavigationTarget {
3370 description: "struct Bar",
3381 fn test_hover_type_param_has_goto_type_action() {
3386 fn foo<T: Foo>(t: T$0){}
3393 mod_path: "test::Foo",
3394 nav: NavigationTarget {
3402 description: "trait Foo",
3413 fn test_hover_self_has_go_to_type() {
3426 mod_path: "test::Foo",
3427 nav: NavigationTarget {
3435 description: "struct Foo",
3446 fn hover_displays_normalized_crate_names() {
3449 //- /lib.rs crate:name-with-dashes
3451 pub struct Thing { x: u32 }
3454 pub fn new() -> Thing { Thing { x: 0 } }
3458 //- /main.rs crate:main deps:name-with-dashes
3459 fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
3465 name_with_dashes::wrapper::Thing
3469 pub fn new() -> Thing
3476 fn hover_field_pat_shorthand_ref_match_ergonomics() {
3499 fn hover_self_param_shows_type() {
3518 fn hover_self_param_shows_type_for_arbitrary_self_type() {
3524 fn bar(sel$0f: Arc<Foo>) {}
3538 fn hover_doc_outer_inner() {
3545 /// This comment belongs to the function
3569 fn hover_doc_outer_inner_attribue() {
3572 #[doc = "Be quick;"]
3574 #![doc = "time is mana"]
3576 #[doc = "This comment belongs to the function"]
3600 fn hover_doc_block_style_indentend() {
3634 fn hover_comments_dont_highlight_parent() {
3635 cov_mark::check!(no_highlight_on_comment_hover);
3636 check_hover_no_result(
3664 fn hover_lifetime() {
3666 r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#,
3678 fn hover_type_param() {
3685 impl<T: Copy + Clone> Foo<T$0> where T: Sized {}
3691 T: Copy + Clone + Sized
3708 // lifetimes bounds arent being tracked yet
3712 impl<T: 'static> Foo<T$0> {}
3725 fn hover_const_param() {
3728 struct Foo<const LEN: usize>;
3729 impl<const LEN: usize> Foo<LEN$0> {}
3742 fn hover_const_pat() {
3746 const FOO: usize = 3;
3773 fn hover_mod_def() {
3800 fn hover_self_in_use() {
3803 //! This should not appear
3805 /// But this should appear
3808 use foo::bar::{self$0};
3823 But this should appear
3829 fn hover_keyword() {
3832 //- /main.rs crate:main deps:std
3833 fn f() { retur$0n; }
3834 //- /libstd.rs crate:std
3835 /// Docs for return_keyword
3836 mod return_keyword {}
3847 Docs for return_keyword
3853 fn hover_builtin() {
3856 //- /main.rs crate:main deps:std
3857 cosnt _: &str$0 = ""; }
3859 //- /libstd.rs crate:std
3860 /// Docs for prim_str
3878 fn hover_macro_expanded_function() {
3881 struct S<'a, T>(&'a T);
3885 fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where
3887 for<'a> T: Clone + 'a
3906 fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32
3910 for<'a> T: Clone + 'a,
3917 fn hover_intra_doc_links() {
3922 /// This is the item. Cool!
3926 /// Gives you a [`TheItem$0`].
3928 /// [`TheItem`]: theitem::TheItem
3929 pub fn gimme() -> theitem::TheItem {
3946 This is the item. Cool!
3952 fn hover_generic_assoc() {
3955 fn foo<T: A>() where T::Assoc$0: {}
3975 let _: <T>::Assoc$0;
4015 fn string_shadowed_with_inner_items() {
4018 //- /main.rs crate:main deps:alloc
4020 /// Custom `String` type.
4029 //- /alloc.rs crate:alloc
4034 /// This is `alloc::String`.
4051 Custom `String` type.
4057 fn function_doesnt_shadow_crate_in_use_tree() {
4060 //- /main.rs crate:main deps:foo
4063 //- /foo.rs crate:foo
4077 fn hover_feature() {
4079 r#"#![feature(box_syntax$0)]"#,
4089 The tracking issue for this feature is: [#49733]
4091 [#49733]: https://github.com/rust-lang/rust/issues/49733
4093 See also [`box_patterns`](box-patterns.md)
4095 ------------------------
4097 Currently the only stable way to create a `Box` is via the `Box::new` method.
4098 Also it is not possible in stable Rust to destructure a `Box` in a match
4099 pattern. The unstable `box` keyword can be used to create a `Box`. An example
4103 #![feature(box_syntax)]
4117 r#"#![allow(arithmetic_overflow$0)]"#,
4119 *arithmetic_overflow*
4125 arithmetic operation overflows
4131 fn hover_clippy_lint() {
4133 r#"#![allow(clippy::almost_swapped$0)]"#,
4137 clippy::almost_swapped
4141 Checks for `foo = bar; bar = foo` sequences.