@@ -273,7 +273,7 @@ pub(super) fn keyword(
273
273
let markup = process_markup (
274
274
sema. db ,
275
275
Definition :: Module ( doc_owner) ,
276
- & markup ( Some ( docs. into ( ) ) , description, None ) ,
276
+ & markup ( Some ( docs. into ( ) ) , description, None , None ) ,
277
277
config,
278
278
) ;
279
279
Some ( HoverResult { markup, actions } )
@@ -419,6 +419,7 @@ pub(super) fn definition(
419
419
famous_defs : Option < & FamousDefs < ' _ , ' _ > > ,
420
420
notable_traits : & [ ( Trait , Vec < ( Option < Type > , Name ) > ) ] ,
421
421
macro_arm : Option < u32 > ,
422
+ hovered_definition : bool ,
422
423
config : & HoverConfig ,
423
424
edition : Edition ,
424
425
) -> Markup {
@@ -456,7 +457,7 @@ pub(super) fn definition(
456
457
_ => def. label ( db, edition) ,
457
458
} ;
458
459
let docs = def. docs ( db, famous_defs, edition) ;
459
- let value = ( || match def {
460
+ let value = || match def {
460
461
Definition :: Variant ( it) => {
461
462
if !it. parent_enum ( db) . is_data_carrying ( db) {
462
463
match it. eval ( db) {
@@ -494,9 +495,9 @@ pub(super) fn definition(
494
495
Some ( body. to_string ( ) )
495
496
}
496
497
_ => None ,
497
- } ) ( ) ;
498
+ } ;
498
499
499
- let layout_info = match def {
500
+ let layout_info = || match def {
500
501
Definition :: Field ( it) => render_memory_layout (
501
502
config. memory_layout ,
502
503
|| it. layout ( db) ,
@@ -529,34 +530,38 @@ pub(super) fn definition(
529
530
_ => None ,
530
531
} ;
531
532
532
- let dyn_compatibility_info = if let Definition :: Trait ( it) = def {
533
- let mut dyn_compatibility_info = String :: new ( ) ;
534
- render_dyn_compatibility ( db, & mut dyn_compatibility_info, it. dyn_compatibility ( db) ) ;
535
- Some ( dyn_compatibility_info)
536
- } else {
537
- None
533
+ let dyn_compatibility_info = || match def {
534
+ Definition :: Trait ( it) => {
535
+ let mut dyn_compatibility_info = String :: new ( ) ;
536
+ render_dyn_compatibility ( db, & mut dyn_compatibility_info, it. dyn_compatibility ( db) ) ;
537
+ Some ( dyn_compatibility_info)
538
+ }
539
+ _ => None ,
538
540
} ;
539
541
540
- let mut desc = String :: new ( ) ;
541
- if let Some ( notable_traits) = render_notable_trait_comment ( db, notable_traits, edition) {
542
- desc. push_str ( & notable_traits) ;
543
- desc. push ( '\n' ) ;
544
- }
545
- if let Some ( layout_info) = layout_info {
546
- desc. push_str ( & layout_info) ;
547
- desc. push ( '\n' ) ;
548
- }
549
- if let Some ( dyn_compatibility_info) = dyn_compatibility_info {
550
- desc. push_str ( & dyn_compatibility_info) ;
551
- desc. push ( '\n' ) ;
542
+ let mut extra = String :: new ( ) ;
543
+ if hovered_definition {
544
+ if let Some ( notable_traits) = render_notable_trait ( db, notable_traits, edition) {
545
+ extra. push_str ( "\n ___\n " ) ;
546
+ extra. push_str ( & notable_traits) ;
547
+ }
548
+ if let Some ( layout_info) = layout_info ( ) {
549
+ extra. push_str ( "\n ___\n " ) ;
550
+ extra. push_str ( & layout_info) ;
551
+ }
552
+ if let Some ( dyn_compatibility_info) = dyn_compatibility_info ( ) {
553
+ extra. push_str ( "\n ___\n " ) ;
554
+ extra. push_str ( & dyn_compatibility_info) ;
555
+ }
552
556
}
557
+ let mut desc = String :: new ( ) ;
553
558
desc. push_str ( & label) ;
554
- if let Some ( value) = value {
559
+ if let Some ( value) = value ( ) {
555
560
desc. push_str ( " = " ) ;
556
561
desc. push_str ( & value) ;
557
562
}
558
563
559
- markup ( docs. map ( Into :: into) , desc, mod_path)
564
+ markup ( docs. map ( Into :: into) , desc, extra . is_empty ( ) . not ( ) . then_some ( extra ) , mod_path)
560
565
}
561
566
562
567
pub ( super ) fn literal (
@@ -626,7 +631,7 @@ pub(super) fn literal(
626
631
Some ( s. into ( ) )
627
632
}
628
633
629
- fn render_notable_trait_comment (
634
+ fn render_notable_trait (
630
635
db : & RootDatabase ,
631
636
notable_traits : & [ ( Trait , Vec < ( Option < Type > , Name ) > ) ] ,
632
637
edition : Edition ,
@@ -635,7 +640,7 @@ fn render_notable_trait_comment(
635
640
let mut needs_impl_header = true ;
636
641
for ( trait_, assoc_types) in notable_traits {
637
642
desc. push_str ( if mem:: take ( & mut needs_impl_header) {
638
- "// Implements notable traits: "
643
+ "Implements notable traits: "
639
644
} else {
640
645
", "
641
646
} ) ;
@@ -728,13 +733,12 @@ fn type_info(
728
733
)
729
734
. into ( )
730
735
} else {
731
- let mut desc =
732
- match render_notable_trait_comment ( db, & notable_traits ( db, & original) , edition) {
733
- Some ( desc) => desc + "\n " ,
734
- None => String :: new ( ) ,
735
- } ;
736
- format_to ! ( desc, "{}" , original. display( db, edition) ) ;
737
- Markup :: fenced_block ( & desc)
736
+ let mut desc = format ! ( "```rust\n {}\n ```" , original. display( db, edition) ) ;
737
+ if let Some ( extra) = render_notable_trait ( db, & notable_traits ( db, & original) , edition) {
738
+ desc. push_str ( "\n ___\n " ) ;
739
+ desc. push_str ( & extra) ;
740
+ } ;
741
+ desc. into ( )
738
742
} ;
739
743
if let Some ( actions) = HoverAction :: goto_type_from_targets ( db, targets, edition) {
740
744
res. actions . push ( actions) ;
@@ -786,20 +790,16 @@ fn closure_ty(
786
790
} ;
787
791
let mut markup = format ! ( "```rust\n {}" , c. display_with_id( sema. db, edition) ) ;
788
792
793
+ if let Some ( trait_) = c. fn_trait ( sema. db ) . get_id ( sema. db , original. krate ( sema. db ) . into ( ) ) {
794
+ push_new_def ( hir:: Trait :: from ( trait_) . into ( ) )
795
+ }
796
+ format_to ! ( markup, "\n {}\n ```" , c. display_with_impl( sema. db, edition) , ) ;
789
797
if let Some ( layout) =
790
798
render_memory_layout ( config. memory_layout , || original. layout ( sema. db ) , |_| None , |_| None )
791
799
{
792
- format_to ! ( markup, " {layout}" ) ;
800
+ format_to ! ( markup, "\n ___ \n {layout}" ) ;
793
801
}
794
- if let Some ( trait_) = c. fn_trait ( sema. db ) . get_id ( sema. db , original. krate ( sema. db ) . into ( ) ) {
795
- push_new_def ( hir:: Trait :: from ( trait_) . into ( ) )
796
- }
797
- format_to ! (
798
- markup,
799
- "\n {}\n ```{adjusted}\n \n ## Captures\n {}" ,
800
- c. display_with_impl( sema. db, edition) ,
801
- captures_rendered,
802
- ) ;
802
+ format_to ! ( markup, "{adjusted}\n \n ## Captures\n {}" , captures_rendered, ) ;
803
803
804
804
let mut res = HoverResult :: default ( ) ;
805
805
if let Some ( actions) = HoverAction :: goto_type_from_targets ( sema. db , targets, edition) {
@@ -824,15 +824,24 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
824
824
. map ( |module| path ( db, module, definition_owner_name ( db, def, edition) , edition) )
825
825
}
826
826
827
- fn markup ( docs : Option < String > , desc : String , mod_path : Option < String > ) -> Markup {
827
+ fn markup (
828
+ docs : Option < String > ,
829
+ rust : String ,
830
+ extra : Option < String > ,
831
+ mod_path : Option < String > ,
832
+ ) -> Markup {
828
833
let mut buf = String :: new ( ) ;
829
834
830
835
if let Some ( mod_path) = mod_path {
831
836
if !mod_path. is_empty ( ) {
832
837
format_to ! ( buf, "```rust\n {}\n ```\n \n " , mod_path) ;
833
838
}
834
839
}
835
- format_to ! ( buf, "```rust\n {}\n ```" , desc) ;
840
+ format_to ! ( buf, "```rust\n {}\n ```" , rust) ;
841
+
842
+ if let Some ( extra) = extra {
843
+ buf. push_str ( & extra) ;
844
+ }
836
845
837
846
if let Some ( doc) = docs {
838
847
format_to ! ( buf, "\n ___\n \n {}" , doc) ;
@@ -862,7 +871,7 @@ fn render_memory_layout(
862
871
let config = config?;
863
872
let layout = layout ( ) . ok ( ) ?;
864
873
865
- let mut label = String :: from ( "// " ) ;
874
+ let mut label = String :: new ( ) ;
866
875
867
876
if let Some ( render) = config. size {
868
877
let size = match tag ( & layout) {
@@ -994,55 +1003,53 @@ fn render_dyn_compatibility(
994
1003
safety : Option < DynCompatibilityViolation > ,
995
1004
) {
996
1005
let Some ( osv) = safety else {
997
- buf. push_str ( "// Dyn Compatible: Yes " ) ;
1006
+ buf. push_str ( "Is Dyn compatible " ) ;
998
1007
return ;
999
1008
} ;
1000
- buf. push_str ( "// Dyn Compatible: No \n // - Reason: " ) ;
1009
+ buf. push_str ( "Is not Dyn compatible due to " ) ;
1001
1010
match osv {
1002
1011
DynCompatibilityViolation :: SizedSelf => {
1003
- buf. push_str ( "has a `Self: Sized` bound" ) ;
1012
+ buf. push_str ( "having a `Self: Sized` bound" ) ;
1004
1013
}
1005
1014
DynCompatibilityViolation :: SelfReferential => {
1006
- buf. push_str ( "has a bound that references `Self`" ) ;
1015
+ buf. push_str ( "having a bound that references `Self`" ) ;
1007
1016
}
1008
1017
DynCompatibilityViolation :: Method ( func, mvc) => {
1009
1018
let name = hir:: Function :: from ( func) . name ( db) ;
1010
- format_to ! (
1011
- buf,
1012
- "has a method `{}` that is non dispatchable because of:\n // - " ,
1013
- name. as_str( )
1014
- ) ;
1019
+ format_to ! ( buf, "having a method `{}` that is not dispatchable due to " , name. as_str( ) ) ;
1015
1020
let desc = match mvc {
1016
1021
MethodViolationCode :: StaticMethod => "missing a receiver" ,
1017
- MethodViolationCode :: ReferencesSelfInput => "a parameter references `Self`" ,
1018
- MethodViolationCode :: ReferencesSelfOutput => "the return type references `Self`" ,
1022
+ MethodViolationCode :: ReferencesSelfInput => "having a parameter referencing `Self`" ,
1023
+ MethodViolationCode :: ReferencesSelfOutput => "the return type referencing `Self`" ,
1019
1024
MethodViolationCode :: ReferencesImplTraitInTrait => {
1020
- "the return type contains `impl Trait`"
1025
+ "the return type containing `impl Trait`"
1021
1026
}
1022
1027
MethodViolationCode :: AsyncFn => "being async" ,
1023
1028
MethodViolationCode :: WhereClauseReferencesSelf => {
1024
- "a where clause references `Self`"
1029
+ "a where clause referencing `Self`"
1030
+ }
1031
+ MethodViolationCode :: Generic => "having a const or type generic parameter" ,
1032
+ MethodViolationCode :: UndispatchableReceiver => {
1033
+ "having a non-dispatchable receiver type"
1025
1034
}
1026
- MethodViolationCode :: Generic => "a non-lifetime generic parameter" ,
1027
- MethodViolationCode :: UndispatchableReceiver => "a non-dispatchable receiver type" ,
1028
1035
} ;
1029
1036
buf. push_str ( desc) ;
1030
1037
}
1031
1038
DynCompatibilityViolation :: AssocConst ( const_) => {
1032
1039
let name = hir:: Const :: from ( const_) . name ( db) ;
1033
1040
if let Some ( name) = name {
1034
- format_to ! ( buf, "has an associated constant `{}`" , name. as_str( ) ) ;
1041
+ format_to ! ( buf, "having an associated constant `{}`" , name. as_str( ) ) ;
1035
1042
} else {
1036
- buf. push_str ( "has an associated constant" ) ;
1043
+ buf. push_str ( "having an associated constant" ) ;
1037
1044
}
1038
1045
}
1039
1046
DynCompatibilityViolation :: GAT ( alias) => {
1040
1047
let name = hir:: TypeAlias :: from ( alias) . name ( db) ;
1041
- format_to ! ( buf, "has a generic associated type `{}`" , name. as_str( ) ) ;
1048
+ format_to ! ( buf, "having a generic associated type `{}`" , name. as_str( ) ) ;
1042
1049
}
1043
1050
DynCompatibilityViolation :: HasNonCompatibleSuperTrait ( super_trait) => {
1044
1051
let name = hir:: Trait :: from ( super_trait) . name ( db) ;
1045
- format_to ! ( buf, "has a dyn incompatible supertrait `{}`" , name. as_str( ) ) ;
1052
+ format_to ! ( buf, "having a dyn incompatible supertrait `{}`" , name. as_str( ) ) ;
1046
1053
}
1047
1054
}
1048
1055
}
0 commit comments