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,
35 pub documentation: bool,
38 #[derive(Debug, Clone)]
39 pub enum HoverAction {
41 Implementation(FilePosition),
42 Reference(FilePosition),
43 GoToType(Vec<HoverGotoTypeData>),
46 #[derive(Debug, Clone, Eq, PartialEq)]
47 pub struct HoverGotoTypeData {
49 pub nav: NavigationTarget,
52 /// Contains the results when hovering over an item
53 #[derive(Debug, Default)]
54 pub struct HoverResult {
56 pub actions: Vec<HoverAction>,
61 // Shows additional information, like type of an expression or documentation for definition when "focusing" code.
62 // Focusing is usually hovering with a mouse, but can also be triggered with a shortcut.
64 // image::https://user-images.githubusercontent.com/48062697/113020658-b5f98b80-917a-11eb-9f88-3dbc27320c95.gif[]
67 position: FilePosition,
69 ) -> Option<RangeInfo<HoverResult>> {
70 let sema = hir::Semantics::new(db);
71 let file = sema.parse(position.file_id).syntax().clone();
72 let token = pick_best(file.token_at_offset(position.offset))?;
73 let token = sema.descend_into_macros(token);
75 let mut res = HoverResult::default();
77 let node = token.parent()?;
79 let definition = match_ast! {
81 // we don't use NameClass::referenced_or_defined here as we do not want to resolve
82 // field pattern shorthands to their definition
83 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
84 NameClass::ConstReference(def) => Some(def),
85 def => def.defined(db),
87 ast::NameRef(name_ref) => {
88 NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(db))
90 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime).map_or_else(
91 || NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(db)),
96 if ast::Comment::cast(token.clone()).is_some() {
97 cov_mark::hit!(no_highlight_on_comment_hover);
98 let (attributes, def) = doc_attributes(&sema, &node)?;
99 let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
100 let (idl_range, link, ns) =
101 extract_definitions_from_markdown(docs.as_str()).into_iter().find_map(|(range, link, ns)| {
102 let hir::InFile { file_id, value: range } = doc_mapping.map(range)?;
103 if file_id == position.file_id.into() && range.contains(position.offset) {
104 Some((range, link, ns))
109 range = Some(idl_range);
110 resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef)
111 } else if let res@Some(_) = try_hover_for_attribute(&token) {
120 if let Some(definition) = definition {
121 let famous_defs = match &definition {
122 Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => {
123 Some(FamousDefs(&sema, sema.scope(&node).krate()))
127 if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref(), config) {
128 res.markup = process_markup(
132 config.links_in_hover,
135 if let Some(action) = show_implementations_action(db, definition) {
136 res.actions.push(action);
139 if let Some(action) = show_fn_references_action(db, definition) {
140 res.actions.push(action);
143 if let Some(action) = runnable_action(&sema, definition, position.file_id) {
144 res.actions.push(action);
147 if let Some(action) = goto_type_action(db, definition) {
148 res.actions.push(action);
151 let range = range.unwrap_or_else(|| sema.original_range(&node).range);
152 return Some(RangeInfo::new(range, res));
156 if let res @ Some(_) = hover_for_keyword(&sema, config, &token) {
162 .find(|n| ast::Expr::can_cast(n.kind()) || ast::Pat::can_cast(n.kind()))?;
164 let ty = match_ast! {
166 ast::Expr(it) => sema.type_of_expr(&it)?,
167 ast::Pat(it) => sema.type_of_pat(&it)?,
168 // If this node is a MACRO_CALL, it means that `descend_into_macros` failed to resolve.
169 // (e.g expanding a builtin macro). So we give up here.
170 ast::MacroCall(_it) => return None,
175 res.markup = if config.markdown {
176 Markup::fenced_block(&ty.display(db))
178 ty.display(db).to_string().into()
180 let range = sema.original_range(&node).range;
181 Some(RangeInfo::new(range, res))
184 fn try_hover_for_attribute(token: &SyntaxToken) -> Option<RangeInfo<HoverResult>> {
185 let attr = token.ancestors().find_map(ast::Attr::cast)?;
186 let (path, tt) = attr.as_simple_call()?;
187 if !tt.syntax().text_range().contains(token.text_range().start()) {
190 let (is_clippy, lints) = match &*path {
191 "feature" => (false, FEATURES),
192 "allow" | "deny" | "forbid" | "warn" => {
193 let is_clippy = algo::non_trivia_sibling(token.clone().into(), Direction::Prev)
194 .filter(|t| t.kind() == T![:])
195 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
196 .filter(|t| t.kind() == T![:])
197 .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
199 t.kind() == T![ident] && t.into_token().map_or(false, |t| t.text() == "clippy")
204 (false, DEFAULT_LINTS)
211 let needle = if is_clippy {
212 tmp = format!("clippy::{}", token.text());
219 lints.binary_search_by_key(&needle, |lint| lint.label).ok().map(|idx| &lints[idx])?;
223 markup: Markup::from(format!("```\n{}\n```\n___\n\n{}", lint.label, lint.description)),
229 fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
230 fn to_action(nav_target: NavigationTarget) -> HoverAction {
231 HoverAction::Implementation(FilePosition {
232 file_id: nav_target.file_id,
233 offset: nav_target.focus_or_full_range().start(),
237 let adt = match def {
238 Definition::ModuleDef(hir::ModuleDef::Trait(it)) => {
239 return it.try_to_nav(db).map(to_action)
241 Definition::ModuleDef(hir::ModuleDef::Adt(it)) => Some(it),
242 Definition::SelfType(it) => it.self_ty(db).as_adt(),
245 adt.try_to_nav(db).map(to_action)
248 fn show_fn_references_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
250 Definition::ModuleDef(hir::ModuleDef::Function(it)) => {
251 it.try_to_nav(db).map(|nav_target| {
252 HoverAction::Reference(FilePosition {
253 file_id: nav_target.file_id,
254 offset: nav_target.focus_or_full_range().start(),
263 sema: &hir::Semantics<RootDatabase>,
266 ) -> Option<HoverAction> {
268 Definition::ModuleDef(it) => match it {
269 hir::ModuleDef::Module(it) => runnable_mod(sema, it).map(HoverAction::Runnable),
270 hir::ModuleDef::Function(func) => {
271 let src = func.source(sema.db)?;
272 if src.file_id != file_id.into() {
273 cov_mark::hit!(hover_macro_generated_struct_fn_doc_comment);
274 cov_mark::hit!(hover_macro_generated_struct_fn_doc_attr);
278 runnable_fn(sema, func).map(HoverAction::Runnable)
286 fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
287 let mut targets: Vec<hir::ModuleDef> = Vec::new();
288 let mut push_new_def = |item: hir::ModuleDef| {
289 if !targets.contains(&item) {
294 if let Definition::GenericParam(hir::GenericParam::TypeParam(it)) = def {
295 it.trait_bounds(db).into_iter().for_each(|it| push_new_def(it.into()));
298 Definition::Local(it) => it.ty(db),
299 Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
304 if let Some(adt) = t.as_adt() {
305 push_new_def(adt.into());
306 } else if let Some(trait_) = t.as_dyn_trait() {
307 push_new_def(trait_.into());
308 } else if let Some(traits) = t.as_impl_traits(db) {
309 traits.into_iter().for_each(|it| push_new_def(it.into()));
310 } else if let Some(trait_) = t.as_associated_type_parent_trait(db) {
311 push_new_def(trait_.into());
316 let targets = targets
319 Some(HoverGotoTypeData {
320 mod_path: render_path(db, it.module(db)?, it.name(db).map(|name| name.to_string())),
321 nav: it.try_to_nav(db)?,
326 Some(HoverAction::GoToType(targets))
329 fn hover_markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Option<Markup> {
330 let mut buf = String::new();
332 if let Some(mod_path) = mod_path {
333 if !mod_path.is_empty() {
334 format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
337 format_to!(buf, "```rust\n{}\n```", desc);
339 if let Some(doc) = docs {
340 format_to!(buf, "\n___\n\n{}", doc);
349 links_in_hover: bool,
352 let markup = markup.as_str();
353 let markup = if !markdown {
354 remove_markdown(markup)
355 } else if links_in_hover {
356 rewrite_links(db, markup, &def)
363 fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
365 Definition::Field(f) => Some(f.parent_def(db).name(db)),
366 Definition::Local(l) => l.parent(db).name(db),
367 Definition::ModuleDef(md) => match md {
368 hir::ModuleDef::Function(f) => match f.as_assoc_item(db)?.container(db) {
369 hir::AssocItemContainer::Trait(t) => Some(t.name(db)),
370 hir::AssocItemContainer::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)),
372 hir::ModuleDef::Variant(e) => Some(e.parent_enum(db).name(db)),
377 .map(|name| name.to_string())
380 fn render_path(db: &RootDatabase, module: hir::Module, item_name: Option<String>) -> String {
382 db.crate_graph()[module.krate().into()].display_name.as_ref().map(|it| it.to_string());
383 let module_path = module
387 .flat_map(|it| it.name(db).map(|name| name.to_string()));
388 crate_name.into_iter().chain(module_path).chain(item_name).join("::")
391 fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
392 if let Definition::GenericParam(_) = def {
395 def.module(db).map(|module| render_path(db, module, definition_owner_name(db, def)))
398 fn hover_for_definition(
401 famous_defs: Option<&FamousDefs>,
402 config: &HoverConfig,
403 ) -> Option<Markup> {
404 let mod_path = definition_mod_path(db, &def);
405 let (label, docs) = match def {
406 Definition::Macro(it) => match &it.source(db)?.value {
407 Either::Left(mac) => {
408 let label = macro_label(mac);
409 (label, it.attrs(db).docs())
411 Either::Right(_) => {
416 Definition::Field(def) => label_and_docs(db, def),
417 Definition::ModuleDef(it) => match it {
418 hir::ModuleDef::Module(it) => label_and_docs(db, it),
419 hir::ModuleDef::Function(it) => label_and_docs(db, it),
420 hir::ModuleDef::Adt(it) => label_and_docs(db, it),
421 hir::ModuleDef::Variant(it) => label_and_docs(db, it),
422 hir::ModuleDef::Const(it) => label_and_docs(db, it),
423 hir::ModuleDef::Static(it) => label_and_docs(db, it),
424 hir::ModuleDef::Trait(it) => label_and_docs(db, it),
425 hir::ModuleDef::TypeAlias(it) => label_and_docs(db, it),
426 hir::ModuleDef::BuiltinType(it) => {
428 .and_then(|fd| hover_for_builtin(fd, it))
429 .or_else(|| Some(Markup::fenced_block(&it.name())))
432 Definition::Local(it) => return hover_for_local(it, db),
433 Definition::SelfType(impl_def) => {
434 impl_def.self_ty(db).as_adt().map(|adt| label_and_docs(db, adt))?
436 Definition::GenericParam(it) => label_and_docs(db, it),
437 Definition::Label(it) => return Some(Markup::fenced_block(&it.name(db))),
440 return hover_markup(docs.filter(|_| config.documentation).map(Into::into), label, mod_path);
442 fn label_and_docs<D>(db: &RootDatabase, def: D) -> (String, Option<hir::Documentation>)
444 D: HasAttrs + HirDisplay,
446 let label = def.display(db).to_string();
447 let docs = def.attrs(db).docs();
452 fn hover_for_local(it: hir::Local, db: &RootDatabase) -> Option<Markup> {
454 let ty = ty.display(db);
455 let is_mut = if it.is_mut(db) { "mut " } else { "" };
456 let desc = match it.source(db).value {
457 Either::Left(ident) => {
458 let name = it.name(db).unwrap();
459 let let_kw = if ident
462 .map_or(false, |p| p.kind() == LET_STMT || p.kind() == CONDITION)
468 format!("{}{}{}: {}", let_kw, is_mut, name, ty)
470 Either::Right(_) => format!("{}self: {}", is_mut, ty),
472 hover_markup(None, desc, None)
475 fn hover_for_keyword(
476 sema: &Semantics<RootDatabase>,
477 config: &HoverConfig,
479 ) -> Option<RangeInfo<HoverResult>> {
480 if !token.kind().is_keyword() || !config.documentation {
483 let famous_defs = FamousDefs(sema, sema.scope(&token.parent()?).krate());
484 // std exposes {}_keyword modules with docstrings on the root to document keywords
485 let keyword_mod = format!("{}_keyword", token.text());
486 let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
487 let docs = doc_owner.attrs(sema.db).docs()?;
488 let markup = process_markup(
490 Definition::ModuleDef(doc_owner.into()),
491 &hover_markup(Some(docs.into()), token.text().into(), None)?,
492 config.links_in_hover,
495 Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
498 fn hover_for_builtin(famous_defs: &FamousDefs, builtin: hir::BuiltinType) -> Option<Markup> {
499 // std exposes prim_{} modules with docstrings on the root to document the builtins
500 let primitive_mod = format!("prim_{}", builtin.name());
501 let doc_owner = find_std_module(famous_defs, &primitive_mod)?;
502 let docs = doc_owner.attrs(famous_defs.0.db).docs()?;
503 hover_markup(Some(docs.into()), builtin.name().to_string(), None)
506 fn find_std_module(famous_defs: &FamousDefs, name: &str) -> Option<hir::Module> {
507 let db = famous_defs.0.db;
508 let std_crate = famous_defs.std()?;
509 let std_root_module = std_crate.root_module(db);
512 .find(|module| module.name(db).map_or(false, |module| module.to_string() == name))
515 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
516 return tokens.max_by_key(priority);
518 fn priority(n: &SyntaxToken) -> usize {
520 IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3,
521 T!['('] | T![')'] => 2,
522 kind if kind.is_trivia() => 0,
530 use expect_test::{expect, Expect};
531 use ide_db::base_db::FileLoader;
533 use crate::{fixture, HoverConfig};
535 fn check_hover_no_result(ra_fixture: &str) {
536 let (analysis, position) = fixture::position(ra_fixture);
540 &HoverConfig { links_in_hover: true, markdown: true, documentation: true }
546 fn check(ra_fixture: &str, expect: Expect) {
547 let (analysis, position) = fixture::position(ra_fixture);
551 &HoverConfig { links_in_hover: true, markdown: true, documentation: true },
556 let content = analysis.db.file_text(position.file_id);
557 let hovered_element = &content[hover.range];
559 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
560 expect.assert_eq(&actual)
563 fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
564 let (analysis, position) = fixture::position(ra_fixture);
568 &HoverConfig { links_in_hover: false, markdown: true, documentation: true },
573 let content = analysis.db.file_text(position.file_id);
574 let hovered_element = &content[hover.range];
576 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
577 expect.assert_eq(&actual)
580 fn check_hover_no_markdown(ra_fixture: &str, expect: Expect) {
581 let (analysis, position) = fixture::position(ra_fixture);
585 &HoverConfig { links_in_hover: true, markdown: false, documentation: true },
590 let content = analysis.db.file_text(position.file_id);
591 let hovered_element = &content[hover.range];
593 let actual = format!("*{}*\n{}\n", hovered_element, hover.info.markup);
594 expect.assert_eq(&actual)
597 fn check_actions(ra_fixture: &str, expect: Expect) {
598 let (analysis, position) = fixture::position(ra_fixture);
602 &HoverConfig { links_in_hover: true, markdown: true, documentation: true },
606 expect.assert_debug_eq(&hover.info.actions)
610 fn hover_shows_type_of_an_expression() {
613 pub fn foo() -> u32 { 1 }
616 let foo_test = foo()$0;
629 fn hover_remove_markdown_if_configured() {
630 check_hover_no_markdown(
632 pub fn foo() -> u32 { 1 }
635 let foo_test = foo()$0;
646 fn hover_shows_long_type_of_an_expression() {
649 struct Scan<A, B, C> { a: A, b: B, c: C }
650 struct Iter<I> { inner: I }
651 enum Option<T> { Some(T), None }
653 struct OtherStruct<T> { i: T }
655 fn scan<A, B, C>(a: A, b: B, c: C) -> Iter<Scan<OtherStruct<A>, B, C>> {
656 Iter { inner: Scan { a, b, c } }
661 let closure = |memo: &mut u32, value: &u32, _another: &mut u32| -> Option<u32> {
662 Option::Some(*memo + value)
665 let mut iter$0 = scan(OtherStruct { i: num }, closure, number);
672 let mut iter: Iter<Scan<OtherStruct<OtherStruct<i32>>, |&mut u32, &u32, &mut u32| -> Option<u32>, u32>>
679 fn hover_shows_fn_signature() {
680 // Single file with result
683 pub fn foo() -> u32 { 1 }
685 fn main() { let foo_test = fo$0o(); }
700 // Multiple candidates but results are ambiguous.
704 pub fn foo() -> u32 { 1 }
707 pub fn foo() -> &str { "" }
710 pub fn foo(a: u32, b: u32) {}
717 fn main() { let foo_test = fo$0o(); }
729 fn hover_shows_fn_signature_with_type_params() {
732 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { }
734 fn main() { let foo_test = fo$0o(); }
744 pub fn foo<'a, T>(b: &'a T) -> &'a str
753 fn hover_shows_fn_signature_on_fn_name() {
756 pub fn foo$0(a: u32, b: u32) -> u32 {}
768 pub fn foo(a: u32, b: u32) -> u32
775 fn hover_shows_fn_doc() {
780 /// # use std::path::Path;
782 /// foo(Path::new("hello, world!"))
784 pub fn foo$0(_: &Path) {}
804 # use std::path::Path;
806 foo(Path::new("hello, world!"))
813 fn hover_shows_fn_doc_attr_raw_string() {
816 #[doc = r#"Raw string doc attr"#]
817 pub fn foo$0(_: &Path) {}
840 fn hover_shows_struct_field_info() {
841 // Hovering over the field when instantiating
844 struct Foo { field_a: u32 }
847 let foo = Foo { field_a$0: 0, };
863 // Hovering over the field in the definition
866 struct Foo { field_a$0: u32 }
869 let foo = Foo { field_a: 0 };
887 fn hover_const_static() {
889 r#"const foo$0: u32 = 123;"#,
903 r#"static foo$0: u32 = 456;"#,
919 fn hover_default_generic_types() {
922 struct Test<K, T = u8> { k: K, t: T }
925 let zz$0 = Test { t: 23u8, k: 33 };
931 let zz: Test<i32, u8>
941 enum Option<T> { Some(T) }
944 fn main() { So$0me(12); }
961 enum Option<T> { Some(T) }
964 fn main() { let b$0ar = Some(12); }
977 fn hover_enum_variant() {
1005 /// The Some variant
1009 let s = Option::Som$0e(12);
1031 fn hover_for_local_variable() {
1033 r#"fn func(foo: i32) { fo$0o; }"#,
1045 fn hover_for_local_variable_pat() {
1047 r#"fn func(fo$0o: i32) {}"#,
1059 fn hover_local_var_edge() {
1061 r#"fn func(foo: i32) { if true { $0foo; }; }"#,
1073 fn hover_for_param_edge() {
1075 r#"fn func($0foo: i32) {}"#,
1087 fn hover_for_param_with_multiple_traits() {
1090 type Target: ?Sized;
1093 type Target: ?Sized;
1095 fn f(_x$0: impl Deref<Target=u8> + DerefMut<Target=u8>) {}"#,
1100 _x: impl Deref<Target = u8> + DerefMut<Target = u8>
1107 fn test_hover_infer_associated_method_result() {
1110 struct Thing { x: u32 }
1113 fn new() -> Thing { Thing { x: 0 } }
1116 fn main() { let foo_$0test = Thing::new(); }
1129 fn test_hover_infer_associated_method_exact() {
1133 struct Thing { x: u32 }
1136 fn new() -> Thing { Thing { x: 0 } }
1140 fn main() { let foo_test = wrapper::Thing::new$0(); }
1146 test::wrapper::Thing
1157 fn test_hover_infer_associated_const_in_pattern() {
1188 fn test_hover_self() {
1191 struct Thing { x: u32 }
1193 fn new() -> Self { Self$0 { x: 0 } }
1210 struct Thing { x: u32 }
1212 fn new() -> Self$0 { Self { x: 0 } }
1231 pub fn new() -> Self$0 { Thing::A }
1250 pub fn thing(a: Self$0) {}
1268 fn test_hover_shadowing_pat() {
1289 fn test_hover_macro_invocation() {
1292 macro_rules! foo { () => {} }
1294 fn f() { fo$0o!(); }
1311 fn test_hover_macro2_invocation() {
1319 fn f() { fo$0o!(); }
1342 fn test_hover_tuple_field() {
1344 r#"struct TS(String, i32$0);"#,
1356 fn test_hover_through_macro() {
1359 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1362 fn bar() { fo$0o(); }
1380 fn test_hover_through_expr_in_macro() {
1383 macro_rules! id { ($($tt:tt)*) => { $($tt)* } }
1384 fn foo(bar:u32) { let a = id!(ba$0r); }
1397 fn test_hover_through_expr_in_macro_recursive() {
1400 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1401 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1402 fn foo(bar:u32) { let a = id!(ba$0r); }
1415 fn test_hover_through_func_in_macro_recursive() {
1418 macro_rules! id_deep { ($($tt:tt)*) => { $($tt)* } }
1419 macro_rules! id { ($($tt:tt)*) => { id_deep!($($tt)*) } }
1420 fn bar() -> u32 { 0 }
1421 fn foo() { let a = id!([0u32, bar($0)] ); }
1433 fn test_hover_through_literal_string_in_macro() {
1436 macro_rules! arr { ($($tt:tt)*) => { [$($tt)*)] } }
1438 let mastered_for_itunes = "";
1439 let _ = arr!("Tr$0acks", &mastered_for_itunes);
1452 fn test_hover_through_assert_macro() {
1455 #[rustc_builtin_macro]
1456 macro_rules! assert {}
1458 fn bar() -> bool { true }
1478 fn test_hover_through_literal_string_in_builtin_macro() {
1479 check_hover_no_result(
1481 #[rustc_builtin_macro]
1482 macro_rules! format {}
1485 format!("hel$0lo {}", 0);
1492 fn test_hover_non_ascii_space_doc() {
1495 /// <- `\u{3000}` here
1498 fn bar() { fo$0o(); }
1519 fn test_hover_function_show_qualifiers() {
1521 r#"async fn foo$0() {}"#,
1535 r#"pub const unsafe fn foo$0() {}"#,
1544 pub const unsafe fn foo()
1548 // Top level `pub(crate)` will be displayed as no visibility.
1550 r#"mod m { pub(crate) async unsafe extern "C" fn foo$0() {} }"#,
1559 pub(crate) async unsafe extern "C" fn foo()
1566 fn test_hover_trait_show_qualifiers() {
1568 r"unsafe trait foo$0() {}",
1585 fn test_hover_extern_crate() {
1588 //- /main.rs crate:main deps:std
1590 //- /std/lib.rs crate:std
1591 //! Standard library for this test
1605 Standard library for this test
1613 //- /main.rs crate:main deps:std
1614 extern crate std as ab$0c;
1615 //- /std/lib.rs crate:std
1616 //! Standard library for this test
1630 Standard library for this test
1639 fn test_hover_mod_with_same_name_as_function() {
1642 use self::m$0y::Bar;
1643 mod my { pub struct Bar; }
1662 fn test_hover_struct_doc_comment() {
1665 /// This is an example
1673 /// assert_eq!(6, my_crate::add_one(5));
1677 fn foo() { let bar = Ba$0r; }
1700 assert_eq!(6, my_crate::add_one(5));
1707 fn test_hover_struct_doc_attr() {
1713 fn foo() { let bar = Ba$0r; }
1734 fn test_hover_struct_doc_attr_multiple_and_mixed() {
1738 #[doc = "bar docs 1"]
1739 #[doc = "bar docs 2"]
1742 fn foo() { let bar = Ba$0r; }
1765 fn test_hover_path_link() {
1769 /// [Foo](struct.Foo.html)
1785 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1791 fn test_hover_path_link_no_strip() {
1795 /// [struct Foo](struct.Foo.html)
1811 [struct Foo](https://docs.rs/test/*/test/struct.Foo.html)
1817 fn test_hover_path_link_field() {
1819 // [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1824 /// [Foo](struct.Foo.html)
1841 [Foo](struct.Foo.html)
1847 fn test_hover_intra_link() {
1869 [Foo](https://docs.rs/test/*/test/foo/struct.Foo.html)
1875 fn test_hover_intra_link_html_root_url() {
1878 #![doc(arbitrary_attribute = "test", html_root_url = "https:/example.com", arbitrary_attribute2)]
1899 [Foo](https://example.com/test/foo/struct.Foo.html)
1905 fn test_hover_intra_link_shortlink() {
1925 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1931 fn test_hover_intra_link_shortlink_code() {
1951 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
1957 fn test_hover_intra_link_namespaced() {
1978 [Foo](https://docs.rs/test/*/test/struct.Foo.html)
1984 fn test_hover_intra_link_shortlink_namspaced_code() {
2004 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2010 fn test_hover_intra_link_shortlink_namspaced_code_with_at() {
2030 [`Foo`](https://docs.rs/test/*/test/struct.Foo.html)
2036 fn test_hover_intra_link_reference() {
2058 [my Foo](https://docs.rs/test/*/test/struct.Foo.html)
2063 fn test_hover_intra_link_reference_to_trait_method() {
2071 /// [buzz]: Foo::buzz
2087 [Foo](https://docs.rs/test/*/test/trait.Foo.html#tymethod.buzz)
2093 fn test_hover_external_url() {
2097 /// [external](https://www.google.com)
2113 [external](https://www.google.com)
2118 // Check that we don't rewrite links which we can't identify
2120 fn test_hover_unknown_target() {
2146 fn test_doc_links_enum_variant() {
2167 [E](https://docs.rs/test/*/test/enum.E.html)
2173 fn test_doc_links_field() {
2194 [`S`](https://docs.rs/test/*/test/struct.S.html)
2200 fn test_hover_no_links() {
2201 check_hover_no_links(
2204 /// case 1. bare URL: https://www.example.com/
2205 /// case 2. inline URL with title: [example](https://www.example.com/)
2206 /// case 3. code reference: [`Result`]
2207 /// case 4. code reference but miss footnote: [`String`]
2208 /// case 5. autolink: <http://www.example.com/>
2209 /// case 6. email address: <test@example.com>
2210 /// case 7. reference: [example][example]
2211 /// case 8. collapsed link: [example][]
2212 /// case 9. shortcut link: [example]
2213 /// case 10. inline without URL: [example]()
2214 /// case 11. reference: [foo][foo]
2215 /// case 12. reference: [foo][bar]
2216 /// case 13. collapsed link: [foo][]
2217 /// case 14. shortcut link: [foo]
2218 /// case 15. inline without URL: [foo]()
2219 /// case 16. just escaped text: \[foo]
2220 /// case 17. inline link: [Foo](foo::Foo)
2222 /// [`Result`]: ../../std/result/enum.Result.html
2223 /// [^example]: https://www.example.com/
2240 case 1. bare URL: https://www.example.com/
2241 case 2. inline URL with title: [example](https://www.example.com/)
2242 case 3. code reference: `Result`
2243 case 4. code reference but miss footnote: `String`
2244 case 5. autolink: http://www.example.com/
2245 case 6. email address: test@example.com
2246 case 7. reference: example
2247 case 8. collapsed link: example
2248 case 9. shortcut link: example
2249 case 10. inline without URL: example
2250 case 11. reference: foo
2251 case 12. reference: foo
2252 case 13. collapsed link: foo
2253 case 14. shortcut link: foo
2254 case 15. inline without URL: foo
2255 case 16. just escaped text: \[foo\]
2256 case 17. inline link: Foo
2258 [^example]: https://www.example.com/
2264 fn test_hover_macro_generated_struct_fn_doc_comment() {
2265 cov_mark::check!(hover_macro_generated_struct_fn_doc_comment);
2281 fn foo() { let bar = Bar; bar.fo$0o(); }
2302 fn test_hover_macro_generated_struct_fn_doc_attr() {
2303 cov_mark::check!(hover_macro_generated_struct_fn_doc_attr);
2311 #[doc = "Do the foo"]
2319 fn foo() { let bar = Bar; bar.fo$0o(); }
2340 fn test_hover_trait_has_impl_action() {
2342 r#"trait foo$0() {}"#,
2359 fn test_hover_struct_has_impl_action() {
2361 r"struct foo$0() {}",
2378 fn test_hover_union_has_impl_action() {
2380 r#"union foo$0() {}"#,
2397 fn test_hover_enum_has_impl_action() {
2399 r"enum foo$0() { A, B }",
2416 fn test_hover_self_has_impl_action() {
2418 r#"struct foo where Self$0:;"#,
2435 fn test_hover_test_has_action() {
2453 nav: NavigationTarget {
2458 focus_range: 11..19,
2479 fn test_hover_test_mod_has_action() {
2491 nav: NavigationTarget {
2512 fn test_hover_struct_has_goto_type_action() {
2517 fn main() { let s$0t = S{ f1:0 }; }
2524 mod_path: "test::S",
2525 nav: NavigationTarget {
2533 description: "struct S",
2544 fn test_hover_generic_struct_has_goto_type_actions() {
2548 struct S<T>{ f1: T }
2550 fn main() { let s$0t = S{ f1:Arg(0) }; }
2557 mod_path: "test::S",
2558 nav: NavigationTarget {
2563 focus_range: 24..25,
2566 description: "struct S<T>",
2570 mod_path: "test::Arg",
2571 nav: NavigationTarget {
2579 description: "struct Arg",
2590 fn test_hover_generic_struct_has_flattened_goto_type_actions() {
2594 struct S<T>{ f1: T }
2596 fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
2603 mod_path: "test::S",
2604 nav: NavigationTarget {
2609 focus_range: 24..25,
2612 description: "struct S<T>",
2616 mod_path: "test::Arg",
2617 nav: NavigationTarget {
2625 description: "struct Arg",
2636 fn test_hover_tuple_has_goto_type_actions() {
2645 fn main() { let s$0t = (A(1), B(2), M::C(3) ); }
2652 mod_path: "test::A",
2653 nav: NavigationTarget {
2661 description: "struct A",
2665 mod_path: "test::B",
2666 nav: NavigationTarget {
2671 focus_range: 22..23,
2674 description: "struct B",
2678 mod_path: "test::M::C",
2679 nav: NavigationTarget {
2684 focus_range: 53..54,
2687 description: "pub struct C",
2698 fn test_hover_return_impl_trait_has_goto_type_action() {
2702 fn foo() -> impl Foo {}
2704 fn main() { let s$0t = foo(); }
2711 mod_path: "test::Foo",
2712 nav: NavigationTarget {
2720 description: "trait Foo",
2731 fn test_hover_generic_return_impl_trait_has_goto_type_action() {
2736 fn foo() -> impl Foo<S> {}
2738 fn main() { let s$0t = foo(); }
2745 mod_path: "test::Foo",
2746 nav: NavigationTarget {
2754 description: "trait Foo<T>",
2758 mod_path: "test::S",
2759 nav: NavigationTarget {
2764 focus_range: 23..24,
2767 description: "struct S",
2778 fn test_hover_return_impl_traits_has_goto_type_action() {
2783 fn foo() -> impl Foo + Bar {}
2785 fn main() { let s$0t = foo(); }
2792 mod_path: "test::Foo",
2793 nav: NavigationTarget {
2801 description: "trait Foo",
2805 mod_path: "test::Bar",
2806 nav: NavigationTarget {
2811 focus_range: 19..22,
2814 description: "trait Bar",
2825 fn test_hover_generic_return_impl_traits_has_goto_type_action() {
2833 fn foo() -> impl Foo<S1> + Bar<S2> {}
2835 fn main() { let s$0t = foo(); }
2842 mod_path: "test::Foo",
2843 nav: NavigationTarget {
2851 description: "trait Foo<T>",
2855 mod_path: "test::Bar",
2856 nav: NavigationTarget {
2861 focus_range: 22..25,
2864 description: "trait Bar<T>",
2868 mod_path: "test::S1",
2869 nav: NavigationTarget {
2874 focus_range: 39..41,
2877 description: "struct S1",
2881 mod_path: "test::S2",
2882 nav: NavigationTarget {
2887 focus_range: 52..54,
2890 description: "struct S2",
2901 fn test_hover_arg_impl_trait_has_goto_type_action() {
2905 fn foo(ar$0g: &impl Foo) {}
2912 mod_path: "test::Foo",
2913 nav: NavigationTarget {
2921 description: "trait Foo",
2932 fn test_hover_arg_impl_traits_has_goto_type_action() {
2939 fn foo(ar$0g: &impl Foo + Bar<S>) {}
2946 mod_path: "test::Foo",
2947 nav: NavigationTarget {
2955 description: "trait Foo",
2959 mod_path: "test::Bar",
2960 nav: NavigationTarget {
2965 focus_range: 19..22,
2968 description: "trait Bar<T>",
2972 mod_path: "test::S",
2973 nav: NavigationTarget {
2978 focus_range: 36..37,
2981 description: "struct S",
2992 fn test_hover_async_block_impl_trait_has_goto_type_action() {
2995 //- minicore: future
2998 let fo$0o = async { S };
3006 mod_path: "core::future::Future",
3007 nav: NavigationTarget {
3011 full_range: 251..433,
3012 focus_range: 290..296,
3015 description: "pub trait Future",
3019 mod_path: "test::S",
3020 nav: NavigationTarget {
3028 description: "struct S",
3039 fn test_hover_arg_generic_impl_trait_has_goto_type_action() {
3044 fn foo(ar$0g: &impl Foo<S>) {}
3051 mod_path: "test::Foo",
3052 nav: NavigationTarget {
3060 description: "trait Foo<T>",
3064 mod_path: "test::S",
3065 nav: NavigationTarget {
3070 focus_range: 23..24,
3073 description: "struct S",
3084 fn test_hover_dyn_return_has_goto_type_action() {
3092 fn foo() -> B<dyn Foo> {}
3094 fn main() { let s$0t = foo(); }
3101 mod_path: "test::B",
3102 nav: NavigationTarget {
3107 focus_range: 49..50,
3110 description: "struct B<T>",
3114 mod_path: "test::Foo",
3115 nav: NavigationTarget {
3123 description: "trait Foo",
3134 fn test_hover_dyn_arg_has_goto_type_action() {
3138 fn foo(ar$0g: &dyn Foo) {}
3145 mod_path: "test::Foo",
3146 nav: NavigationTarget {
3154 description: "trait Foo",
3165 fn test_hover_generic_dyn_arg_has_goto_type_action() {
3170 fn foo(ar$0g: &dyn Foo<S>) {}
3177 mod_path: "test::Foo",
3178 nav: NavigationTarget {
3186 description: "trait Foo<T>",
3190 mod_path: "test::S",
3191 nav: NavigationTarget {
3196 focus_range: 23..24,
3199 description: "struct S",
3210 fn test_hover_goto_type_action_links_order() {
3213 trait ImplTrait<T> {}
3214 trait DynTrait<T> {}
3218 fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
3225 mod_path: "test::ImplTrait",
3226 nav: NavigationTarget {
3234 description: "trait ImplTrait<T>",
3238 mod_path: "test::B",
3239 nav: NavigationTarget {
3244 focus_range: 50..51,
3247 description: "struct B<T>",
3251 mod_path: "test::DynTrait",
3252 nav: NavigationTarget {
3257 focus_range: 28..36,
3260 description: "trait DynTrait<T>",
3264 mod_path: "test::S",
3265 nav: NavigationTarget {
3270 focus_range: 65..66,
3273 description: "struct S",
3284 fn test_hover_associated_type_has_goto_type_action() {
3289 fn get(self) -> Self::Item {}
3295 impl Foo for S { type Item = Bar; }
3297 fn test() -> impl Foo { S {} }
3299 fn main() { let s$0t = test().get(); }
3306 mod_path: "test::Foo",
3307 nav: NavigationTarget {
3315 description: "trait Foo",
3326 fn test_hover_const_param_has_goto_type_action() {
3330 struct Foo<const BAR: Bar>;
3332 impl<const BAR: Bar> Foo<BAR$0> {}
3339 mod_path: "test::Bar",
3340 nav: NavigationTarget {
3348 description: "struct Bar",
3359 fn test_hover_type_param_has_goto_type_action() {
3364 fn foo<T: Foo>(t: T$0){}
3371 mod_path: "test::Foo",
3372 nav: NavigationTarget {
3380 description: "trait Foo",
3391 fn test_hover_self_has_go_to_type() {
3404 mod_path: "test::Foo",
3405 nav: NavigationTarget {
3413 description: "struct Foo",
3424 fn hover_displays_normalized_crate_names() {
3427 //- /lib.rs crate:name-with-dashes
3429 pub struct Thing { x: u32 }
3432 pub fn new() -> Thing { Thing { x: 0 } }
3436 //- /main.rs crate:main deps:name-with-dashes
3437 fn main() { let foo_test = name_with_dashes::wrapper::Thing::new$0(); }
3443 name_with_dashes::wrapper::Thing
3447 pub fn new() -> Thing
3454 fn hover_field_pat_shorthand_ref_match_ergonomics() {
3477 fn hover_self_param_shows_type() {
3496 fn hover_self_param_shows_type_for_arbitrary_self_type() {
3502 fn bar(sel$0f: Arc<Foo>) {}
3516 fn hover_doc_outer_inner() {
3523 /// This comment belongs to the function
3547 fn hover_doc_outer_inner_attribue() {
3550 #[doc = "Be quick;"]
3552 #![doc = "time is mana"]
3554 #[doc = "This comment belongs to the function"]
3578 fn hover_doc_block_style_indentend() {
3612 fn hover_comments_dont_highlight_parent() {
3613 cov_mark::check!(no_highlight_on_comment_hover);
3614 check_hover_no_result(
3642 fn hover_lifetime() {
3644 r#"fn foo<'lifetime>(_: &'lifetime$0 ()) {}"#,
3656 fn hover_type_param() {
3663 impl<T: Copy + Clone> Foo<T$0> where T: Sized {}
3669 T: Copy + Clone + Sized
3686 // lifetimes bounds arent being tracked yet
3690 impl<T: 'static> Foo<T$0> {}
3703 fn hover_const_param() {
3706 struct Foo<const LEN: usize>;
3707 impl<const LEN: usize> Foo<LEN$0> {}
3720 fn hover_const_pat() {
3724 const FOO: usize = 3;
3751 fn hover_mod_def() {
3778 fn hover_self_in_use() {
3781 //! This should not appear
3783 /// But this should appear
3786 use foo::bar::{self$0};
3801 But this should appear
3807 fn hover_keyword() {
3810 //- /main.rs crate:main deps:std
3811 fn f() { retur$0n; }
3812 //- /libstd.rs crate:std
3813 /// Docs for return_keyword
3814 mod return_keyword {}
3825 Docs for return_keyword
3831 fn hover_builtin() {
3834 //- /main.rs crate:main deps:std
3835 cosnt _: &str$0 = ""; }
3837 //- /libstd.rs crate:std
3838 /// Docs for prim_str
3856 fn hover_macro_expanded_function() {
3859 struct S<'a, T>(&'a T);
3863 fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where
3865 for<'a> T: Clone + 'a
3884 fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32
3888 for<'a> T: Clone + 'a,
3895 fn hover_intra_doc_links() {
3900 /// This is the item. Cool!
3904 /// Gives you a [`TheItem$0`].
3906 /// [`TheItem`]: theitem::TheItem
3907 pub fn gimme() -> theitem::TheItem {
3924 This is the item. Cool!
3930 fn hover_generic_assoc() {
3933 fn foo<T: A>() where T::Assoc$0: {}
3953 let _: <T>::Assoc$0;
3993 fn string_shadowed_with_inner_items() {
3996 //- /main.rs crate:main deps:alloc
3998 /// Custom `String` type.
4007 //- /alloc.rs crate:alloc
4012 /// This is `alloc::String`.
4029 Custom `String` type.
4035 fn function_doesnt_shadow_crate_in_use_tree() {
4038 //- /main.rs crate:main deps:foo
4041 //- /foo.rs crate:foo
4055 fn hover_feature() {
4057 r#"#![feature(box_syntax$0)]"#,
4067 The tracking issue for this feature is: [#49733]
4069 [#49733]: https://github.com/rust-lang/rust/issues/49733
4071 See also [`box_patterns`](box-patterns.md)
4073 ------------------------
4075 Currently the only stable way to create a `Box` is via the `Box::new` method.
4076 Also it is not possible in stable Rust to destructure a `Box` in a match
4077 pattern. The unstable `box` keyword can be used to create a `Box`. An example
4081 #![feature(box_syntax)]
4095 r#"#![allow(arithmetic_overflow$0)]"#,
4097 *arithmetic_overflow*
4103 arithmetic operation overflows
4109 fn hover_clippy_lint() {
4111 r#"#![allow(clippy::almost_swapped$0)]"#,
4115 clippy::almost_swapped
4119 Checks for `foo = bar; bar = foo` sequences.