From ef48d1ca3bfb512c245d9e9bdc73d0d5a5f79740 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 14 Mar 2021 01:07:05 +0800 Subject: Add test for hover of macro expanded function --- crates/ide/src/hover.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index ea45086ce..f872b68cf 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3657,4 +3657,42 @@ cosnt _: &str$0 = ""; }"#; "#]], ); } + + #[test] + fn hover_macro_expanded_function() { + check( + r#" +struct S<'a, T>(&'a T); +trait Clone {} +macro_rules! foo { + () => { + fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where + 't: 't + 't, + for<'a> T: Clone + 'a + { 0 as _ } + }; +} + +foo!(); + +fn main() { + bar$0; +} +"#, + expect![[r#" + *bar* + + ```rust + test + ``` + + ```rust + fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 + where + 't: 't + 't, + for<'a> T: Clone + 'a, + ``` + "#]], + ) + } } -- cgit v1.2.3 From ef416e0154767619fcbfa0d1682b28bd338a8ce9 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 14 Mar 2021 20:03:39 +0800 Subject: Impl HirDisplay for function hover message --- crates/ide/src/hover.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index f872b68cf..1e4c247c0 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -354,7 +354,7 @@ fn hover_for_definition( }, mod_path, ), - ModuleDef::Function(it) => from_def_source(db, it, mod_path), + ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path), ModuleDef::Adt(Adt::Enum(it)) => from_def_source(db, it, mod_path), @@ -383,6 +383,14 @@ fn hover_for_definition( }, }; + fn from_hir_fmt(db: &RootDatabase, def: D, mod_path: Option) -> Option + where + D: HasAttrs + HirDisplay, + { + let label = def.display(db).to_string(); + from_def_source_labeled(db, def, Some(label), mod_path) + } + fn from_def_source(db: &RootDatabase, def: D, mod_path: Option) -> Option where D: HasSource + HasAttrs + Copy, -- cgit v1.2.3 From 87171238c6c528c421f06de8cd7e41ed3b6ff57a Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 16 Mar 2021 00:05:03 +0800 Subject: Use hir formatter more --- crates/ide/src/hover.rs | 59 ++++++++++++++----------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 1e4c247c0..7328f410a 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -1,7 +1,7 @@ use either::Either; use hir::{ - Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, - HirDisplay, Module, ModuleDef, ModuleSource, Semantics, + Adt, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, + ModuleDef, Semantics, }; use ide_db::{ base_db::SourceDatabase, @@ -14,7 +14,7 @@ use stdx::format_to; use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; use crate::{ - display::{macro_label, ShortLabel, TryToNav}, + display::{macro_label, TryToNav}, doc_links::{remove_links, rewrite_links}, markdown_remove::remove_markdown, markup::Markup, @@ -335,34 +335,18 @@ fn hover_for_definition( let label = macro_label(&it.source(db)?.value); from_def_source_labeled(db, it, Some(label), mod_path) } - Definition::Field(def) => { - let src = def.source(db)?.value; - if let FieldSource::Named(it) = src { - from_def_source_labeled(db, def, it.short_label(), mod_path) - } else { - None - } - } + Definition::Field(def) => from_hir_fmt(db, def, mod_path), Definition::ModuleDef(it) => match it { - ModuleDef::Module(it) => from_def_source_labeled( - db, - it, - match it.definition_source(db).value { - ModuleSource::Module(it) => it.short_label(), - ModuleSource::SourceFile(it) => it.short_label(), - ModuleSource::BlockExpr(it) => it.short_label(), - }, - mod_path, - ), + ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path), ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path), - ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), - ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path), - ModuleDef::Adt(Adt::Enum(it)) => from_def_source(db, it, mod_path), - ModuleDef::Variant(it) => from_def_source(db, it, mod_path), - ModuleDef::Const(it) => from_def_source(db, it, mod_path), - ModuleDef::Static(it) => from_def_source(db, it, mod_path), - ModuleDef::Trait(it) => from_def_source(db, it, mod_path), - ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), + ModuleDef::Adt(Adt::Struct(it)) => from_hir_fmt(db, it, mod_path), + ModuleDef::Adt(Adt::Union(it)) => from_hir_fmt(db, it, mod_path), + ModuleDef::Adt(Adt::Enum(it)) => from_hir_fmt(db, it, mod_path), + ModuleDef::Variant(it) => from_hir_fmt(db, it, mod_path), + ModuleDef::Const(it) => from_hir_fmt(db, it, mod_path), + ModuleDef::Static(it) => from_hir_fmt(db, it, mod_path), + ModuleDef::Trait(it) => from_hir_fmt(db, it, mod_path), + ModuleDef::TypeAlias(it) => from_hir_fmt(db, it, mod_path), ModuleDef::BuiltinType(it) => famous_defs .and_then(|fd| hover_for_builtin(fd, it)) .or_else(|| Some(Markup::fenced_block(&it.name()))), @@ -370,16 +354,16 @@ fn hover_for_definition( Definition::Local(it) => hover_for_local(it, db), Definition::SelfType(impl_def) => { impl_def.target_ty(db).as_adt().and_then(|adt| match adt { - Adt::Struct(it) => from_def_source(db, it, mod_path), - Adt::Union(it) => from_def_source(db, it, mod_path), - Adt::Enum(it) => from_def_source(db, it, mod_path), + Adt::Struct(it) => from_hir_fmt(db, it, mod_path), + Adt::Union(it) => from_hir_fmt(db, it, mod_path), + Adt::Enum(it) => from_hir_fmt(db, it, mod_path), }) } Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), Definition::GenericParam(it) => match it { GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))), GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), - GenericParam::ConstParam(it) => from_def_source(db, it, None), + GenericParam::ConstParam(it) => Some(Markup::fenced_block(&it.display(db))), }, }; @@ -391,15 +375,6 @@ fn hover_for_definition( from_def_source_labeled(db, def, Some(label), mod_path) } - fn from_def_source(db: &RootDatabase, def: D, mod_path: Option) -> Option - where - D: HasSource + HasAttrs + Copy, - A: ShortLabel, - { - let short_label = def.source(db)?.value.short_label(); - from_def_source_labeled(db, def, short_label, mod_path) - } - fn from_def_source_labeled( db: &RootDatabase, def: D, -- cgit v1.2.3 From 455b418263df80da5cd80e2af1df27b7e165c0c3 Mon Sep 17 00:00:00 2001 From: oxalica Date: Tue, 16 Mar 2021 01:24:26 +0800 Subject: Update tests --- crates/ide/src/hover.rs | 85 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 28 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 7328f410a..a35805c5e 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -653,7 +653,9 @@ fn main() { let foo_test = fo$0o(); } ``` ```rust - pub fn foo<'a, T: AsRef>(b: &'a T) -> &'a str + pub fn foo<'a, T>(b: &'a T) -> &'a str + where + T: AsRef, ``` "#]], ); @@ -861,7 +863,7 @@ fn main() { So$0me(12); } ``` ```rust - Some + Some(T) ``` "#]], ); @@ -927,7 +929,7 @@ fn main() { ``` ```rust - Some + Some(T) ``` --- @@ -1424,13 +1426,14 @@ fn bar() { fo$0o(); } ``` "#]], ); + // Top level `pub(crate)` will be displayed as no visibility. check( - r#"pub(crate) async unsafe extern "C" fn foo$0() {}"#, + r#"mod m { pub(crate) async unsafe extern "C" fn foo$0() {} }"#, expect![[r#" *foo* ```rust - test + test::m ``` ```rust @@ -1472,11 +1475,18 @@ extern crate st$0d; //! abc123 "#, expect![[r#" - *std* - Standard library for this test + *std* + + ```rust + extern crate std + ``` - Printed? - abc123 + --- + + Standard library for this test + + Printed? + abc123 "#]], ); check( @@ -1490,11 +1500,18 @@ extern crate std as ab$0c; //! abc123 "#, expect![[r#" - *abc* - Standard library for this test + *abc* - Printed? - abc123 + ```rust + extern crate std + ``` + + --- + + Standard library for this test + + Printed? + abc123 "#]], ); } @@ -2004,7 +2021,7 @@ enum E { ``` ```rust - V + V { field: i32 } ``` --- @@ -2400,7 +2417,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S", + description: "struct S", }, }, HoverGotoTypeData { @@ -2446,7 +2463,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } focus_range: 24..25, name: "S", kind: Struct, - description: "struct S", + description: "struct S", }, }, HoverGotoTypeData { @@ -2588,7 +2605,7 @@ fn main() { let s$0t = foo(); } focus_range: 6..9, name: "Foo", kind: Trait, - description: "trait Foo", + description: "trait Foo", }, }, HoverGotoTypeData { @@ -2685,7 +2702,7 @@ fn main() { let s$0t = foo(); } focus_range: 6..9, name: "Foo", kind: Trait, - description: "trait Foo", + description: "trait Foo", }, }, HoverGotoTypeData { @@ -2698,7 +2715,7 @@ fn main() { let s$0t = foo(); } focus_range: 22..25, name: "Bar", kind: Trait, - description: "trait Bar", + description: "trait Bar", }, }, HoverGotoTypeData { @@ -2802,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar) {} focus_range: 19..22, name: "Bar", kind: Trait, - description: "trait Bar", + description: "trait Bar", }, }, HoverGotoTypeData { @@ -2899,7 +2916,7 @@ fn foo(ar$0g: &impl Foo) {} focus_range: 6..9, name: "Foo", kind: Trait, - description: "trait Foo", + description: "trait Foo", }, }, HoverGotoTypeData { @@ -2949,7 +2966,7 @@ fn main() { let s$0t = foo(); } focus_range: 49..50, name: "B", kind: Struct, - description: "struct B", + description: "struct B", }, }, HoverGotoTypeData { @@ -3025,7 +3042,7 @@ fn foo(ar$0g: &dyn Foo) {} focus_range: 6..9, name: "Foo", kind: Trait, - description: "trait Foo", + description: "trait Foo", }, }, HoverGotoTypeData { @@ -3073,7 +3090,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 6..15, name: "ImplTrait", kind: Trait, - description: "trait ImplTrait", + description: "trait ImplTrait", }, }, HoverGotoTypeData { @@ -3086,7 +3103,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 50..51, name: "B", kind: Struct, - description: "struct B", + description: "struct B", }, }, HoverGotoTypeData { @@ -3099,7 +3116,7 @@ fn foo(a$0rg: &impl ImplTrait>>>) {} focus_range: 28..36, name: "DynTrait", kind: Trait, - description: "trait DynTrait", + description: "trait DynTrait", }, }, HoverGotoTypeData { @@ -3565,6 +3582,17 @@ mod foo$0; "#, expect![[r#" *foo* + + ```rust + test + ``` + + ```rust + mod foo + ``` + + --- + For the horde! "#]], ); @@ -3589,7 +3617,7 @@ use foo::bar::{self$0}; ``` ```rust - pub mod bar + mod bar ``` --- @@ -3670,8 +3698,9 @@ fn main() { ``` ```rust - fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 + fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32 where + T: Clone + 't, 't: 't + 't, for<'a> T: Clone + 'a, ``` -- cgit v1.2.3