2 use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay};
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 implementations: bool,
37 pub goto_type_def: bool,
38 pub links_in_hover: bool,
40 pub documentation: bool,
44 pub const NO_ACTIONS: Self = Self {
45 implementations: false,
55 pub fn any_actions(&self) -> bool {
56 self.implementations || self.references || self.runnable() || self.goto_type_def
59 pub fn no_actions(&self) -> bool {
63 pub fn runnable(&self) -> bool {
64 self.run || self.debug
68 #[derive(Debug, Clone)]
69 pub enum HoverAction {
71 Implementation(FilePosition),
72 Reference(FilePosition),
73 GoToType(Vec<HoverGotoTypeData>),
76 #[derive(Debug, Clone, Eq, PartialEq)]
77 pub struct HoverGotoTypeData {
79 pub nav: NavigationTarget,
82 /// Contains the results when hovering over an item
83 #[derive(Debug, Default)]
84 pub struct HoverResult {
86 pub actions: Vec<HoverAction>,
91 // Shows additional information, like type of an expression or documentation for definition when "focusing" code.
92 // Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.
94 // image::https://user-images.githubusercontent.com/48062697/113020658-b5f98b80-917a-11eb-9f88-3dbc27320c95.gif[]
97 position: FilePosition,
101 ) -> Option<RangeInfo<HoverResult>> {
102 let sema = hir::Semantics::new(db);
103 let file = sema.parse(position.file_id).syntax().clone();
104 let token = pick_best(file.token_at_offset(position.offset))?;
105 let token = sema.descend_into_macros(token);
107 let mut res = HoverResult::default();
109 let node = token.parent()?;
110 let mut range = None;
111 let definition = match_ast! {
113 // we don't use NameClass::referenced_or_defined here as we do not want to resolve
114 // field pattern shorthands to their definition
115 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
116 NameClass::ConstReference(def) => Some(def),
117 def => def.defined(db),
119 ast::NameRef(name_ref) => {
120 NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(db))
122 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime).map_or_else(
123 || NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(db)),
128 if ast::Comment::cast(token.clone()).is_some() {
129 cov_mark::hit!(no_highlight_on_comment_hover);
130 let (attributes, def) = doc_attributes(&sema, &node)?;
131 let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
132 let (idl_range, link, ns) =
133 extract_definitions_from_markdown(docs.as_str()).into_iter().find_map(|(range, link, ns)| {
134 let hir::InFile { file_id, value: range } = doc_mapping.map(range)?;
135 if file_id == position.file_id.into() && range.contains(position.offset) {
136 Some((range, link, ns))
141 range = Some(idl_range);
142 resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef)
143 } else if let res@Some(_) = try_hover_for_attribute(&token) {
152 if let Some(definition) = definition {
153 let famous_defs = match &definition {
154 Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
155 Some(FamousDefs(&sema, sema.scope(&node).krate()))
159 if let Some(markup) =
160 hover_for_definition(db, definition, famous_defs.as_ref(), documentation)
162 res.markup = process_markup(sema.db, definition, &markup, links_in_hover, markdown);
163 if let Some(action) = show_implementations_action(db, definition) {
164 res.actions.push(action);
167 if let Some(action) = show_fn_references_action(db, definition) {
168 res.actions.push(action);
171 if let Some(action) = runnable_action(&sema, definition, position.file_id) {
172 res.actions.push(action);
175 if let Some(action) = goto_type_action(db, definition) {
176 res.actions.push(action);
179 let range = range.unwrap_or_else(|| sema.original_range(&node).range);
180 return Some(RangeInfo::new(range, res));
184 if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, documentation, &token)
191 .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
193 let ty = match_ast! {
195 ast::Expr(it) => sema.type_of_expr(&it)?,
196 ast::Pat(it) => sema.type_of_pat(&it)?,
197 // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
198 // (e.g expanding a builtin macro). So we give up here.
199 ast::MacroCall(_it) => return None,
204 res.markup = if markdown {
205 Markup::fenced_block(&ty.display(db))
207 ty.display(db).to_string().into()
209 let range = sema.original_range(&node).range;
210 Some(RangeInfo::new(range, res))
213 fn try_hover_for_attribute(token: &SyntaxToken) -> Option<RangeInfo<HoverResult>> {
214 let attr = token.ancestors().find_map(ast::Attr::cast)?;
215 let (path, tt) = attr.as_simple_call()?;
216 if !tt.syntax().text_range().contains(token.text_range().start()) {
219 let (is_clippy, lints) = match &*path {
220 "feature" => (false, FEATURES),
221 "allow" | "deny" | "forbid" | "warn" => {
222 let is_clippy = algo::non_trivia_sibling(token.clone().into(), Direction::Prev)
223 .filter(|t| t.kind() == T![:])
224 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
225 .filter(|t| t.kind() == T![:])
226 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
228 t.kind() == T![ident] && t.into_token().map_or(false, |t| t.text() == "clippy")
233 (false, DEFAULT_LINTS)
240 let needle = if is_clippy {
241 tmp = format!("clippy::{}", token.text());
248 lints.binary_search_by_key(&needle, |lint| lint.label).ok().map(|idx| &lints[idx])?;
252 markup: Markup::from(format!("```\n{}\n```\n___\n\n{}", lint.label, lint.description)),
258 fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
259 fn to_action(nav_target: NavigationTarget) -> HoverAction {
260 HoverAction::Implementation(FilePosition {
261 file_id: nav_target.file_id,
262 offset: nav_target.focus_or_full_range().start(),
266 let adt = match def {
267 Definition::ModuleDef(hir::ModuleDef::Trait(it)) => {
268 return it.try_to_nav(db).map(to_action)
270 Definition::ModuleDef(hir::ModuleDef::Adt(it)) => Some(it),
271 Definition::SelfType(it) => it.self_ty(db).as_adt(),
274 adt.try_to_nav(db).map(to_action)
277 fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
279 Definition::ModuleDef(hir::ModuleDef::Function(it)) => {
280 it.try_to_nav(db).map(|nav_target| {
281 HoverAction::Reference(FilePosition {
282 file_id: nav_target.file_id,
283 offset: nav_target.focus_or_full_range().start(),
292 sema: &hir::Semantics<RootDatabase>,
295 ) -> Option<HoverAction> {
297 Definition::ModuleDef(it) => match it {
298 hir::ModuleDef::Module(it) => runnable_mod(sema, it).map(HoverAction::Runnable),
299 hir::ModuleDef::Function(func) => {
300 let src = func.source(sema.db)?;
301 if src.file_id != file_id.into() {
302 cov_mark::hit!(hover_macro_generated_struct_fn_doc_comment);
303 cov_mark::hit!(hover_macro_generated_struct_fn_doc_attr);
307 runnable_fn(sema, func).map(HoverAction::Runnable)
315 fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
316 let mut targets: Vec<hir::ModuleDef> = Vec::new();
317 let mut push_new_def = |item: hir::ModuleDef| {
318 if !targets.contains(&item) {
323 if let Definition::GenericParam(hir::GenericParam::TypeParam(it)) = def {
324 it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
327 Definition::Local(it) => it.ty(db),
328 Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
333 if let Some(adt) = t.as_adt() {
334 push_new_def(adt.into());
335 } else if let Some(trait_) = t.as_dyn_trait() {
336 push_new_def(trait_.into());
337 } else if let Some(traits) = t.as_impl_traits(db) {
338 traits.into_iter().for_each(|it| push_new_def(it.into()));
339 } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
340 push_new_def(trait_.into());
345 let targets = targets
348 Some(HoverGotoTypeData {
349 mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
350 nav: it.try_to_nav(db)?,
355 Some(HoverAction::GoToType(targets))
358 fn hover_markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Option<Markup> {
359 let mut buf = String::new();
361 if let Some(mod_path) = mod_path {
362 if !mod_path.is_empty() {
363 format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
366 format_to!(buf, "```rust\n{}\n```", desc);
368 if let Some(doc) = docs {
369 format_to!(buf, "\n___\n\n{}", doc);
378 links_in_hover: bool,
381 let markup = markup.as_str();
382 let markup = if !markdown {
383 remove_markdown(markup)
384 } else if links_in_hover {
385 rewrite_links(db, markup, &def)
392 fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
394 Definition::Field(f) => Some(f.parent_def(db).name(db)),
395 Definition::Local(l) => l.parent(db).name(db),
396 Definition::ModuleDef(md) => match md {
397 hir::ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
398 hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
399 hir::AssocItemContainer::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
401 hir::ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)),
406 .map(|name| name.to_string())
409 fn render_path(db: &RootDatabase, module: hir::Module, item_name: Option<String>) -> String {
411 db.crate_graph()[module.krate().into()].display_name.as_ref().map(|it| it.to_string());
412 let module_path = module
416 .flat_map(|it| it.name(db).map(|name| name.to_string()));
417 crate_name.into_iter().chain(module_path).chain(item_name).join("::")
420 fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
421 if let Definition::GenericParam(_) = def {
424 def.module(db).map(|module| render_path(db, module, definition_owner_name(db, def)))
427 fn hover_for_definition(
430 famous_defs: Option<&FamousDefs>,
432 ) -> Option<Markup> {
433 let mod_path = definition_mod_path(db, &def);
434 let (label, docs) = match def {
435 Definition::Macro(it) => match &it.source(db)?.value {
436 Either::Left(mac) => {
437 let label = macro_label(mac);
438 (label, it.attrs(db).docs())
440 Either::Right(_) => {
445 Definition::Field(def) => label_and_docs(db, def),
446 Definition::ModuleDef(it) => match it {
447 hir::ModuleDef::Module(it) => label_and_docs(db, it),
448 hir::ModuleDef::Function(it) => label_and_docs(db, it),
449 hir::ModuleDef::Adt(it) => label_and_docs(db, it),
450 hir::ModuleDef::Variant(it) => label_and_docs(db, it),
451 hir::ModuleDef::Const(it) => label_and_docs(db, it),
452 hir::ModuleDef::Static(it) => label_and_docs(db, it),
453 hir::ModuleDef::Trait(it) => label_and_docs(db, it),
454 hir::ModuleDef::TypeAlias(it) => label_and_docs(db, it),
455 hir::ModuleDef::BuiltinType(it) => {
457 .and_then(|fd| hover_for_builtin(fd, it))
458 .or_else(|| Some(Markup::fenced_block(&it.name())))
461 Definition::Local(it) => return hover_for_local(it, db),
462 Definition::SelfType(impl_def) => {
463 impl_def.self_ty(db).as_adt().map(|adt| label_and_docs(db, adt))?
465 Definition::GenericParam(it) => label_and_docs(db, it),
466 Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
469 return hover_markup(docs.filter(|_| documentation).map(Into::into), label, mod_path);
471 fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
473 D: HasAttrs + HirDisplay,
475 let label = def.display(db).to_string();
476 let docs = def.attrs(db).docs();
481 fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option<Markup> {
483 let ty = ty.display(db);
484 let is_mut = if it.is_mut(db) { "mut " } else { "" };
485 let desc = match it.source(db).value {
486 Either::Left(ident) => {
487 let name = it.name(db).unwrap();
488 let let_kw = if ident
491 .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION)
497 format!("{}{}{}: {}", let_kw, is_mut, name, ty)
499 Either::Right(_) => format!("{}self: {}", is_mut, ty),
501 hover_markup(None, desc, None)
504 fn hover_for_keyword(
505 sema: &hir::Semantics<RootDatabase>,
506 links_in_hover: bool,
510 ) -> Option<RangeInfo<HoverResult>> {
511 if !token.kind().is_keyword() || !documentation {
514 let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate());
515 // std exposes {}_keyword modules with docstrings on the root to document keywords
516 let keyword_mod = format!("{}_keyword", token.text());
517 let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
518 let docs = doc_owner.attrs(sema.db).docs()?;
519 let markup = process_markup(
521 Definition::ModuleDef(doc_owner.into()),
522 &hover_markup(Some(docs.into()), token.text().into(), None)?,
526 Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
529 fn hover_for_builtin(famous_defs: &FamousDefs, builtin: hir::BuiltinType) -> Option<Markup> {
530 // std exposes prim_{} modules with docstrings on the root to document the builtins
531 let primitive_mod = format!("prim_{}", builtin.name());
532 let doc_owner = find_std_module(famous_defs, &primitive_mod)?;
533 let docs = doc_owner.attrs(famous_defs.0.db).docs()?;
534 hover_markup(Some(docs.into()), builtin.name().to_string(), None)
537 fn find_std_module(famous_defs: &FamousDefs, name: &str) -> Option<hir::Module> {
538 let db = famous_defs.0.db;
539 let std_crate = famous_defs.std()?;
540 let std_root_module = std_crate.root_module(db);
543 .find(|module| module.name(db).map_or(false, |module| module.to_string() == name))
546 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
547 return tokens.max_by_key(priority);
549 fn priority(n: &SyntaxToken) -> usize {
551 IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3,
552 T!['('] | T![')'] => 2,
553 kind if kind.is_trivia() => 0,
561 use expect_test::{expect, Expect};
562 use ide_db::base_db::FileLoader;
566 fn check_hover_no_result(ra_fixture: &str) {
567 let (analysis, position) = fixture::position(ra_fixture);
568 assert!(analysis.hover(position, true, true, true).unwrap().is_none());
571 fn check(ra_fixture: &str, expect: Expect) {
572 let (analysis, position) = fixture::position(ra_fixture);
573 let hover = analysis.hover(position, true, true, true).unwrap().unwrap();
575 let content = analysis.db.file_text(position.file_id);
576 let hovered_element = &content[hover.range];
578 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
579 expect.assert_eq(&actual)
582 fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
583 let (analysis, position) = fixture::position(ra_fixture);
584 let hover = analysis.hover(position, false, true, true).unwrap().unwrap();
586 let content = analysis.db.file_text(position.file_id);
587 let hovered_element = &content[hover.range];
589 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
590 expect.assert_eq(&actual)
593 fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
594 let (analysis, position) = fixture::position(ra_fixture);
595 let hover = analysis.hover(position, true, true, false).unwrap().unwrap();
597 let content = analysis.db.file_text(position.file_id);
598 let hovered_element = &content[hover.range];
600 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
601 expect.assert_eq(&actual)
604 fn check_actions(ra_fixture: &str, expect: Expect) {
605 let (analysis, position) = fixture::position(ra_fixture);
606 let hover = analysis.hover(position, true, true, true).unwrap().unwrap();
607 expect.assert_debug_eq(&hover.info.actions)
611 fn hover_shows_type_of_an_expression() {
614 pub fn foo() -> u32 { 1 }
617 let foo_test = foo()$0;
630 fn hover_remove_markdown_if_configured() {
631 check_hover_no_markdown(
633 pub fn foo() -> u32 { 1 }
636 let foo_test = foo()$0;
647 fn hover_shows_long_type_of_an_expression() {
650 struct Scan<A, B, C> { a: A, b: B, c: C }
651 struct Iter<I> { inner: I }
652 enum Option<T> { Some(T), None }
654 struct OtherStruct<T> { i: T }
656 fn scan<A, B, C>(a: A, b: B, c: C) -> Iter<Scan<OtherStruct<A>, B, C>> {
657 Iter { inner: Scan { a, b, c } }
662 let closure = |memo: &mut u32, value: &u32, _another: &mut u32| -> Option<u32> {
663 Option::Some(*memo + value)
666 let mut iter$0 = scan(OtherStruct { i: num }, closure, number);
673 let mut iter: Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>>
680 fn hover_shows_fn_signature() {
681 // Single file with result
684 pub fn foo() -> u32 { 1 }
686 fn main() { let foo_test = fo$0o(); }
701 // Multiple candidates but results are ambiguous.
705 pub fn foo() -> u32 { 1 }
708 pub fn foo() -> &str { "" }
711 pub fn foo(a: u32, b: u32) {}
718 fn main() { let foo_test = fo$0o(); }
730 fn hover_shows_fn_signature_with_type_params() {
733 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { }
735 fn main() { let foo_test = fo$0o(); }
745 pub fn foo<'a, T>(b: &'a T) -> &'a str
754 fn hover_shows_fn_signature_on_fn_name() {
757 pub fn foo$0(a: u32, b: u32) -> u32 {}
769 pub fn foo(a: u32, b: u32) -> u32
776 fn hover_shows_fn_doc() {
781 /// # use std::path::Path;
783 /// foo(Path::new("hello, world!"))
785 pub fn foo$0(_: &Path) {}
805 # use std::path::Path;
807 foo(Path::new("hello, world!"))
814 fn hover_shows_fn_doc_attr_raw_string() {
817 #[doc = r#"Raw string doc attr"#]
818 pub fn foo$0(_: &Path) {}
841 fn hover_shows_struct_field_info() {
842 // Hovering over the field when instantiating
845 struct Foo { field_a: u32 }
848 let foo = Foo { field_a$0: 0, };
864 // Hovering over the field in the definition
867 struct Foo { field_a$0: u32 }
870 let foo = Foo { field_a: 0 };
888 fn hover_const_static() {
890 r#"const foo$0: u32 = 123;"#,
904 r#"static foo$0: u32 = 456;"#,
920 fn hover_default_generic_types() {
923 struct Test<K, T = u8> { k: K, t: T }
926 let zz$0 = Test { t: 23u8, k: 33 };
932 let zz: Test<i32, u8>
942 enum Option<T> { Some(T) }
945 fn main() { So$0me(12); }
962 enum Option<T> { Some(T) }
965 fn main() { let b$0ar = Some(12); }
978 fn hover_enum_variant() {
1006 /// The Some variant
1010 let s = Option::Som$0e(12);
1032 fn hover_for_local_variable() {
1034 r#"fn func(foo: i32) { fo$0o; }"#,
1046 fn hover_for_local_variable_pat() {
1048 r#"fn func(fo$0o: i32) {}"#,
1060 fn hover_local_var_edge() {
1062 r#"fn func(foo: i32) { if true { $0foo; }; }"#,
1074 fn hover_for_param_edge() {
1076 r#"fn func($0foo: i32) {}"#,
1088 fn hover_for_param_with_multiple_traits() {
1091 type Target: ?Sized;
1094 type Target: ?Sized;
1096 fn f(_x$0: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#,
1101 _x: impl Deref<Target = u8> + DerefMut<Target = u8>
1108 fn test_hover_infer_associated_method_result() {
1111 struct Thing { x: u32 }
1114 fn new() -> Thing { Thing { x: 0 } }
1117 fn main() { let foo_$0test = Thing::new(); }
1130 fn test_hover_infer_associated_method_exact() {
1134 struct Thing { x: u32 }
1137 fn new() -> Thing { Thing { x: 0 } }
1141 fn main() { let foo_test = wrapper::Thing::new$0(); }
1147 test::wrapper::Thing
1158 fn test_hover_infer_associated_const_in_pattern() {
1189 fn test_hover_self() {
1192 struct Thing { x: u32 }
1194 fn new() -> Self { Self$0 { x: 0 } }
1211 struct Thing { x: u32 }
1213 fn new() -> Self$0 { Self { x: 0 } }
1232 pub fn new() -> Self$0 { Thing::A }
1251 pub fn thing(a: Self$0) {}
1269 fn test_hover_shadowing_pat() {
1290 fn test_hover_macro_invocation() {
1293 macro_rules! foo { () => {} }
1295 fn f() { fo$0o!(); }
1312 fn test_hover_macro2_invocation() {
1320 fn f() { fo$0o!(); }
1343 fn test_hover_tuple_field() {
1345 r#"struct TS(String, i32$0);"#,
1357 fn test_hover_through_macro() {
1360 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1363 fn bar() { fo$0o(); }
1381 fn test_hover_through_expr_in_macro() {
1384 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1385 fn foo(bar:u32) { let a = id!(ba$0r); }
1398 fn test_hover_through_expr_in_macro_recursive() {
1401 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1402 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1403 fn foo(bar:u32) { let a = id!(ba$0r); }
1416 fn test_hover_through_func_in_macro_recursive() {
1419 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1420 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1421 fn bar() -> u32 { 0 }
1422 fn foo() { let a = id!([0u32, bar($0)] ); }
1434 fn test_hover_through_literal_string_in_macro() {
1437 macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } }
1439 let mastered_for_itunes = "";
1440 let _ = arr!("Tr$0acks", &mastered_for_itunes);
1453 fn test_hover_through_assert_macro() {
1456 #[rustc_builtin_macro]
1457 macro_rules! assert {}
1459 fn bar() -> bool { true }
1479 fn test_hover_through_literal_string_in_builtin_macro() {
1480 check_hover_no_result(
1482 #[rustc_builtin_macro]
1483 macro_rules! format {}
1486 format!("hel$0lo {}", 0);
1493 fn test_hover_non_ascii_space_doc() {
1496 /// <- `\u{3000}` here
1499 fn bar() { fo$0o(); }
1520 fn test_hover_function_show_qualifiers() {
1522 r#"async fn foo$0() {}"#,
1536 r#"pub const unsafe fn foo$0() {}"#,
1545 pub const unsafe fn foo()
1549 // Top level `pub(crate)` will be displayed as no visibility.
1551 r#"mod m { pub(crate) async unsafe extern "C" fn foo$0() {} }"#,
1560 pub(crate) async unsafe extern "C" fn foo()
1567 fn test_hover_trait_show_qualifiers() {
1569 r"unsafe trait foo$0() {}",
1586 fn test_hover_extern_crate() {
1589 //- /main.rs crate:main deps:std
1591 //- /std/lib.rs crate:std
1592 //! Standard library for this test
1606 Standard library for this test
1614 //- /main.rs crate:main deps:std
1615 extern crate std as ab$0c;
1616 //- /std/lib.rs crate:std
1617 //! Standard library for this test
1631 Standard library for this test
1640 fn test_hover_mod_with_same_name_as_function() {
1643 use self::m$0y::Bar;
1644 mod my { pub struct Bar; }
1663 fn test_hover_struct_doc_comment() {
1666 /// This is an example
1674 /// assert_eq!(6, my_crate::add_one(5));
1678 fn foo() { let bar = Ba$0r; }
1701 assert_eq!(6, my_crate::add_one(5));
1708 fn test_hover_struct_doc_attr() {
1714 fn foo() { let bar = Ba$0r; }
1735 fn test_hover_struct_doc_attr_multiple_and_mixed() {
1739 #[doc = "bar docs 1"]
1740 #[doc = "bar docs 2"]
1743 fn foo() { let bar = Ba$0r; }
1766 fn test_hover_path_link() {
1770 /// [Foo](struct.Foo.html)
1786 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1792 fn test_hover_path_link_no_strip() {
1796 /// [struct Foo](struct.Foo.html)
1812 [struct Foo](https://docs.rs/test/*/test/struct.Foo.html)
1818 fn test_hover_path_link_field() {
1820 // [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1825 /// [Foo](struct.Foo.html)
1842 [Foo](struct.Foo.html)
1848 fn test_hover_intra_link() {
1870 [Foo](https://docs.rs/test/*/test/foo/struct.Foo.html)
1876 fn test_hover_intra_link_html_root_url() {
1879 #![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
1900 [Foo](https://example.com/test/foo/struct.Foo.html)
1906 fn test_hover_intra_link_shortlink() {
1926 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1932 fn test_hover_intra_link_shortlink_code() {
1952 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1958 fn test_hover_intra_link_namespaced() {
1979 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1985 fn test_hover_intra_link_shortlink_namspaced_code() {
2005 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2011 fn test_hover_intra_link_shortlink_namspaced_code_with_at() {
2031 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2037 fn test_hover_intra_link_reference() {
2059 [my Foo](https://docs.rs/test/*/test/struct.Foo.html)
2064 fn test_hover_intra_link_reference_to_trait_method() {
2072 /// [buzz]: Foo::buzz
2088 [Foo](https://docs.rs/test/*/test/trait.Foo.html#tymethod.buzz)
2094 fn test_hover_external_url() {
2098 /// [external](https://www.google.com)
2114 [external](https://www.google.com)
2119 // Check that we don't rewrite links which we can't identify
2121 fn test_hover_unknown_target() {
2147 fn test_doc_links_enum_variant() {
2168 [E](https://docs.rs/test/*/test/enum.E.html)
2174 fn test_doc_links_field() {
2195 [`S`](https://docs.rs/test/*/test/struct.S.html)
2201 fn test_hover_no_links() {
2202 check_hover_no_links(
2205 /// case 1. bare URL: https://www.example.com/
2206 /// case 2. inline URL with title: [example](https://www.example.com/)
2207 /// case 3. code reference: [`Result`]
2208 /// case 4. code reference but miss footnote: [`String`]
2209 /// case 5. autolink: <http://www.example.com/>
2210 /// case 6. email address: <test@example.com>
2211 /// case 7. reference: [example][example]
2212 /// case 8. collapsed link: [example][]
2213 /// case 9. shortcut link: [example]
2214 /// case 10. inline without URL: [example]()
2215 /// case 11. reference: [foo][foo]
2216 /// case 12. reference: [foo][bar]
2217 /// case 13. collapsed link: [foo][]
2218 /// case 14. shortcut link: [foo]
2219 /// case 15. inline without URL: [foo]()
2220 /// case 16. just escaped text: \[foo]
2221 /// case 17. inline link: [Foo](foo::Foo)
2223 /// [`Result`]: ../../std/result/enum.Result.html
2224 /// [^example]: https://www.example.com/
2241 case 1. bare URL: https://www.example.com/
2242 case 2. inline URL with title: [example](https://www.example.com/)
2243 case 3. code reference: `Result`
2244 case 4. code reference but miss footnote: `String`
2245 case 5. autolink: http://www.example.com/
2246 case 6. email address: test@example.com
2247 case 7. reference: example
2248 case 8. collapsed link: example
2249 case 9. shortcut link: example
2250 case 10. inline without URL: example
2251 case 11. reference: foo
2252 case 12. reference: foo
2253 case 13. collapsed link: foo
2254 case 14. shortcut link: foo
2255 case 15. inline without URL: foo
2256 case 16. just escaped text: \[foo\]
2257 case 17. inline link: Foo
2259 [^example]: https://www.example.com/
2265 fn test_hover_macro_generated_struct_fn_doc_comment() {
2266 cov_mark::check!(hover_macro_generated_struct_fn_doc_comment);
2282 fn foo() { let bar = Bar; bar.fo$0o(); }
2303 fn test_hover_macro_generated_struct_fn_doc_attr() {
2304 cov_mark::check!(hover_macro_generated_struct_fn_doc_attr);
2312 #[doc = "Do the foo"]
2320 fn foo() { let bar = Bar; bar.fo$0o(); }
2341 fn test_hover_trait_has_impl_action() {
2343 r#"trait foo$0() {}"#,
2360 fn test_hover_struct_has_impl_action() {
2362 r"struct foo$0() {}",
2379 fn test_hover_union_has_impl_action() {
2381 r#"union foo$0() {}"#,
2398 fn test_hover_enum_has_impl_action() {
2400 r"enum foo$0() { A, B }",
2417 fn test_hover_self_has_impl_action() {
2419 r#"struct foo where Self$0:;"#,
2436 fn test_hover_test_has_action() {
2454 nav: NavigationTarget {
2459 focus_range: 11..19,
2480 fn test_hover_test_mod_has_action() {
2492 nav: NavigationTarget {
2513 fn test_hover_struct_has_goto_type_action() {
2518 fn main() { let s$0t = S{ f1:0 }; }
2525 mod_path: "test::S",
2526 nav: NavigationTarget {
2534 description: "struct S",
2545 fn test_hover_generic_struct_has_goto_type_actions() {
2549 struct S<T>{ f1: T }
2551 fn main() { let s$0t = S{ f1:Arg(0) }; }
2558 mod_path: "test::S",
2559 nav: NavigationTarget {
2564 focus_range: 24..25,
2567 description: "struct S<T>",
2571 mod_path: "test::Arg",
2572 nav: NavigationTarget {
2580 description: "struct Arg",
2591 fn test_hover_generic_struct_has_flattened_goto_type_actions() {
2595 struct S<T>{ f1: T }
2597 fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
2604 mod_path: "test::S",
2605 nav: NavigationTarget {
2610 focus_range: 24..25,
2613 description: "struct S<T>",
2617 mod_path: "test::Arg",
2618 nav: NavigationTarget {
2626 description: "struct Arg",
2637 fn test_hover_tuple_has_goto_type_actions() {
2646 fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
2653 mod_path: "test::A",
2654 nav: NavigationTarget {
2662 description: "struct A",
2666 mod_path: "test::B",
2667 nav: NavigationTarget {
2672 focus_range: 22..23,
2675 description: "struct B",
2679 mod_path: "test::M::C",
2680 nav: NavigationTarget {
2685 focus_range: 53..54,
2688 description: "pub struct C",
2699 fn test_hover_return_impl_trait_has_goto_type_action() {
2703 fn foo() -> impl Foo {}
2705 fn main() { let s$0t = foo(); }
2712 mod_path: "test::Foo",
2713 nav: NavigationTarget {
2721 description: "trait Foo",
2732 fn test_hover_generic_return_impl_trait_has_goto_type_action() {
2737 fn foo() -> impl Foo<S> {}
2739 fn main() { let s$0t = foo(); }
2746 mod_path: "test::Foo",
2747 nav: NavigationTarget {
2755 description: "trait Foo<T>",
2759 mod_path: "test::S",
2760 nav: NavigationTarget {
2765 focus_range: 23..24,
2768 description: "struct S",
2779 fn test_hover_return_impl_traits_has_goto_type_action() {
2784 fn foo() -> impl Foo + Bar {}
2786 fn main() { let s$0t = foo(); }
2793 mod_path: "test::Foo",
2794 nav: NavigationTarget {
2802 description: "trait Foo",
2806 mod_path: "test::Bar",
2807 nav: NavigationTarget {
2812 focus_range: 19..22,
2815 description: "trait Bar",
2826 fn test_hover_generic_return_impl_traits_has_goto_type_action() {
2834 fn foo() -> impl Foo<S1> + Bar<S2> {}
2836 fn main() { let s$0t = foo(); }
2843 mod_path: "test::Foo",
2844 nav: NavigationTarget {
2852 description: "trait Foo<T>",
2856 mod_path: "test::Bar",
2857 nav: NavigationTarget {
2862 focus_range: 22..25,
2865 description: "trait Bar<T>",
2869 mod_path: "test::S1",
2870 nav: NavigationTarget {
2875 focus_range: 39..41,
2878 description: "struct S1",
2882 mod_path: "test::S2",
2883 nav: NavigationTarget {
2888 focus_range: 52..54,
2891 description: "struct S2",
2902 fn test_hover_arg_impl_trait_has_goto_type_action() {
2906 fn foo(ar$0g: &impl Foo) {}
2913 mod_path: "test::Foo",
2914 nav: NavigationTarget {
2922 description: "trait Foo",
2933 fn test_hover_arg_impl_traits_has_goto_type_action() {
2940 fn foo(ar$0g: &impl Foo + Bar<S>) {}
2947 mod_path: "test::Foo",
2948 nav: NavigationTarget {
2956 description: "trait Foo",
2960 mod_path: "test::Bar",
2961 nav: NavigationTarget {
2966 focus_range: 19..22,
2969 description: "trait Bar<T>",
2973 mod_path: "test::S",
2974 nav: NavigationTarget {
2979 focus_range: 36..37,
2982 description: "struct S",
2993 fn test_hover_async_block_impl_trait_has_goto_type_action() {
2996 //- minicore: future
2999 let fo$0o = async { S };
3007 mod_path: "core::future::Future",
3008 nav: NavigationTarget {
3012 full_range: 251..433,
3013 focus_range: 290..296,
3016 description: "pub trait Future",
3020 mod_path: "test::S",
3021 nav: NavigationTarget {
3029 description: "struct S",
3040 fn test_hover_arg_generic_impl_trait_has_goto_type_action() {
3045 fn foo(ar$0g: &impl Foo<S>) {}
3052 mod_path: "test::Foo",
3053 nav: NavigationTarget {
3061 description: "trait Foo<T>",
3065 mod_path: "test::S",
3066 nav: NavigationTarget {
3071 focus_range: 23..24,
3074 description: "struct S",
3085 fn test_hover_dyn_return_has_goto_type_action() {
3093 fn foo() -> B<dyn Foo> {}
3095 fn main() { let s$0t = foo(); }
3102 mod_path: "test::B",
3103 nav: NavigationTarget {
3108 focus_range: 49..50,
3111 description: "struct B<T>",
3115 mod_path: "test::Foo",
3116 nav: NavigationTarget {
3124 description: "trait Foo",
3135 fn test_hover_dyn_arg_has_goto_type_action() {
3139 fn foo(ar$0g: &dyn Foo) {}
3146 mod_path: "test::Foo",
3147 nav: NavigationTarget {
3155 description: "trait Foo",
3166 fn test_hover_generic_dyn_arg_has_goto_type_action() {
3171 fn foo(ar$0g: &dyn Foo<S>) {}
3178 mod_path: "test::Foo",
3179 nav: NavigationTarget {
3187 description: "trait Foo<T>",
3191 mod_path: "test::S",
3192 nav: NavigationTarget {
3197 focus_range: 23..24,
3200 description: "struct S",
3211 fn test_hover_goto_type_action_links_order() {
3214 trait ImplTrait<T> {}
3215 trait DynTrait<T> {}
3219 fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
3226 mod_path: "test::ImplTrait",
3227 nav: NavigationTarget {
3235 description: "trait ImplTrait<T>",
3239 mod_path: "test::B",
3240 nav: NavigationTarget {
3245 focus_range: 50..51,
3248 description: "struct B<T>",
3252 mod_path: "test::DynTrait",
3253 nav: NavigationTarget {
3258 focus_range: 28..36,
3261 description: "trait DynTrait<T>",
3265 mod_path: "test::S",
3266 nav: NavigationTarget {
3271 focus_range: 65..66,
3274 description: "struct S",
3285 fn test_hover_associated_type_has_goto_type_action() {
3290 fn get(self) -> Self::Item {}
3296 impl Foo for S { type Item = Bar; }
3298 fn test() -> impl Foo { S {} }
3300 fn main() { let s$0t = test().get(); }
3307 mod_path: "test::Foo",
3308 nav: NavigationTarget {
3316 description: "trait Foo",
3327 fn test_hover_const_param_has_goto_type_action() {
3331 struct Foo<const BAR: Bar>;
3333 impl<const BAR: Bar> Foo<BAR$0> {}
3340 mod_path: "test::Bar",
3341 nav: NavigationTarget {
3349 description: "struct Bar",
3360 fn test_hover_type_param_has_goto_type_action() {
3365 fn foo<T: Foo>(t: T$0){}
3372 mod_path: "test::Foo",
3373 nav: NavigationTarget {
3381 description: "trait Foo",
3392 fn test_hover_self_has_go_to_type() {
3405 mod_path: "test::Foo",
3406 nav: NavigationTarget {
3414 description: "struct Foo",
3425 fn hover_displays_normalized_crate_names() {
3428 //- /lib.rs crate:name-with-dashes
3430 pub struct Thing { x: u32 }
3433 pub fn new() -> Thing { Thing { x: 0 } }
3437 //- /main.rs crate:main deps:name-with-dashes
3438 fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
3444 name_with_dashes::wrapper::Thing
3448 pub fn new() -> Thing
3455 fn hover_field_pat_shorthand_ref_match_ergonomics() {
3478 fn hover_self_param_shows_type() {
3497 fn hover_self_param_shows_type_for_arbitrary_self_type() {
3503 fn bar(sel$0f: Arc<Foo>) {}
3517 fn hover_doc_outer_inner() {
3524 /// This comment belongs to the function
3548 fn hover_doc_outer_inner_attribue() {
3551 #[doc = "Be quick;"]
3553 #![doc = "time is mana"]
3555 #[doc = "This comment belongs to the function"]
3579 fn hover_doc_block_style_indentend() {
3613 fn hover_comments_dont_highlight_parent() {
3614 cov_mark::check!(no_highlight_on_comment_hover);
3615 check_hover_no_result(
3643 fn hover_lifetime() {
3645 r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#,
3657 fn hover_type_param() {
3664 impl<T: Copy + Clone> Foo<T$0> where T: Sized {}
3670 T: Copy + Clone + Sized
3687 // lifetimes bounds arent being tracked yet
3691 impl<T: 'static> Foo<T$0> {}
3704 fn hover_const_param() {
3707 struct Foo<const LEN: usize>;
3708 impl<const LEN: usize> Foo<LEN$0> {}
3721 fn hover_const_pat() {
3725 const FOO: usize = 3;
3752 fn hover_mod_def() {
3779 fn hover_self_in_use() {
3782 //! This should not appear
3784 /// But this should appear
3787 use foo::bar::{self$0};
3802 But this should appear
3808 fn hover_keyword() {
3811 //- /main.rs crate:main deps:std
3812 fn f() { retur$0n; }
3813 //- /libstd.rs crate:std
3814 /// Docs for return_keyword
3815 mod return_keyword {}
3826 Docs for return_keyword
3832 fn hover_builtin() {
3835 //- /main.rs crate:main deps:std
3836 cosnt _: &str$0 = ""; }
3838 //- /libstd.rs crate:std
3839 /// Docs for prim_str
3857 fn hover_macro_expanded_function() {
3860 struct S<'a, T>(&'a T);
3864 fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where
3866 for<'a> T: Clone + 'a
3885 fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32
3889 for<'a> T: Clone + 'a,
3896 fn hover_intra_doc_links() {
3901 /// This is the item. Cool!
3905 /// Gives you a [`TheItem$0`].
3907 /// [`TheItem`]: theitem::TheItem
3908 pub fn gimme() -> theitem::TheItem {
3925 This is the item. Cool!
3931 fn hover_generic_assoc() {
3934 fn foo<T: A>() where T::Assoc$0: {}
3954 let _: <T>::Assoc$0;
3994 fn string_shadowed_with_inner_items() {
3997 //- /main.rs crate:main deps:alloc
3999 /// Custom `String` type.
4008 //- /alloc.rs crate:alloc
4013 /// This is `alloc::String`.
4030 Custom `String` type.
4036 fn function_doesnt_shadow_crate_in_use_tree() {
4039 //- /main.rs crate:main deps:foo
4042 //- /foo.rs crate:foo
4056 fn hover_feature() {
4058 r#"#![feature(box_syntax$0)]"#,
4068 The tracking issue for this feature is: [#49733]
4070 [#49733]: https://github.com/rust-lang/rust/issues/49733
4072 See also [`box_patterns`](box-patterns.md)
4074 ------------------------
4076 Currently the only stable way to create a `Box` is via the `Box::new` method.
4077 Also it is not possible in stable Rust to destructure a `Box` in a match
4078 pattern. The unstable `box` keyword can be used to create a `Box`. An example
4082 #![feature(box_syntax)]
4096 r#"#![allow(arithmetic_overflow$0)]"#,
4098 *arithmetic_overflow*
4104 arithmetic operation overflows
4110 fn hover_clippy_lint() {
4112 r#"#![allow(clippy::almost_swapped$0)]"#,
4116 clippy::almost_swapped
4120 Checks for `foo = bar; bar = foo` sequences.