@@ -3,8 +3,8 @@ use std::{mem, ops::Not};
3
3
4
4
use either:: Either ;
5
5
use hir:: {
6
- Adt , AsAssocItem , CaptureKind , HasSource , HirDisplay , Layout , LayoutError , Name , Semantics ,
7
- Trait , Type , TypeInfo ,
6
+ Adt , AsAssocItem , CaptureKind , HasCrate , HasSource , HirDisplay , Layout , LayoutError , Name ,
7
+ Semantics , Trait , Type , TypeInfo ,
8
8
} ;
9
9
use ide_db:: {
10
10
base_db:: SourceDatabase ,
@@ -25,7 +25,7 @@ use syntax::{
25
25
26
26
use crate :: {
27
27
doc_links:: { remove_links, rewrite_links} ,
28
- hover:: walk_and_push_ty,
28
+ hover:: { notable_traits , walk_and_push_ty} ,
29
29
HoverAction , HoverConfig , HoverResult , Markup , MemoryLayoutHoverConfig ,
30
30
MemoryLayoutHoverRenderKind ,
31
31
} ;
@@ -471,38 +471,8 @@ pub(super) fn definition(
471
471
_ => None ,
472
472
} ;
473
473
474
- let notable_traits = {
475
- let mut desc = String :: new ( ) ;
476
- let mut needs_impl_header = true ;
477
- for ( trait_, assoc_types) in notable_traits {
478
- desc. push_str ( if mem:: take ( & mut needs_impl_header) {
479
- " // notable traits implemented: "
480
- } else {
481
- ", "
482
- } ) ;
483
- format_to ! ( desc, "{}" , trait_. name( db) . display( db) , ) ;
484
- if !assoc_types. is_empty ( ) {
485
- desc. push ( '<' ) ;
486
- format_to ! (
487
- desc,
488
- "{}" ,
489
- assoc_types. into_iter( ) . format_with( ", " , |( ty, name) , f| {
490
- f( & name. display( db) ) ?;
491
- f( & " = " ) ?;
492
- match ty {
493
- Some ( ty) => f( & ty. display( db) ) ,
494
- None => f( & "?" ) ,
495
- }
496
- } )
497
- ) ;
498
- desc. push ( '>' ) ;
499
- }
500
- }
501
- desc. is_empty ( ) . not ( ) . then ( || desc)
502
- } ;
503
-
504
474
let mut desc = String :: new ( ) ;
505
- if let Some ( notable_traits) = notable_traits {
475
+ if let Some ( notable_traits) = render_notable_trait_comment ( db , notable_traits) {
506
476
desc. push_str ( & notable_traits) ;
507
477
desc. push ( '\n' ) ;
508
478
}
@@ -519,6 +489,39 @@ pub(super) fn definition(
519
489
markup ( docs. map ( Into :: into) , desc, mod_path)
520
490
}
521
491
492
+ fn render_notable_trait_comment (
493
+ db : & RootDatabase ,
494
+ notable_traits : & [ ( Trait , Vec < ( Option < Type > , Name ) > ) ] ,
495
+ ) -> Option < String > {
496
+ let mut desc = String :: new ( ) ;
497
+ let mut needs_impl_header = true ;
498
+ for ( trait_, assoc_types) in notable_traits {
499
+ desc. push_str ( if mem:: take ( & mut needs_impl_header) {
500
+ " // notable traits implemented: "
501
+ } else {
502
+ ", "
503
+ } ) ;
504
+ format_to ! ( desc, "{}" , trait_. name( db) . display( db) , ) ;
505
+ if !assoc_types. is_empty ( ) {
506
+ desc. push ( '<' ) ;
507
+ format_to ! (
508
+ desc,
509
+ "{}" ,
510
+ assoc_types. into_iter( ) . format_with( ", " , |( ty, name) , f| {
511
+ f( & name. display( db) ) ?;
512
+ f( & " = " ) ?;
513
+ match ty {
514
+ Some ( ty) => f( & ty. display( db) ) ,
515
+ None => f( & "?" ) ,
516
+ }
517
+ } )
518
+ ) ;
519
+ desc. push ( '>' ) ;
520
+ }
521
+ }
522
+ desc. is_empty ( ) . not ( ) . then ( || desc)
523
+ }
524
+
522
525
fn type_info (
523
526
sema : & Semantics < ' _ , RootDatabase > ,
524
527
config : & HoverConfig ,
@@ -536,8 +539,12 @@ fn type_info(
536
539
}
537
540
} ;
538
541
walk_and_push_ty ( sema. db , & original, & mut push_new_def) ;
539
-
540
- res. markup = if let Some ( adjusted_ty) = adjusted {
542
+ let mut desc = match render_notable_trait_comment ( sema. db , & notable_traits ( sema. db , & original) )
543
+ {
544
+ Some ( desc) => desc + "\n " ,
545
+ None => String :: new ( ) ,
546
+ } ;
547
+ desc += & if let Some ( adjusted_ty) = adjusted {
541
548
walk_and_push_ty ( sema. db , & adjusted_ty, & mut push_new_def) ;
542
549
let original = original. display ( sema. db ) . to_string ( ) ;
543
550
let adjusted = adjusted_ty. display ( sema. db ) . to_string ( ) ;
@@ -549,10 +556,10 @@ fn type_info(
549
556
apad = static_text_diff_len + adjusted. len( ) . max( original. len( ) ) ,
550
557
opad = original. len( ) ,
551
558
)
552
- . into ( )
553
559
} else {
554
- Markup :: fenced_block ( & original. display ( sema. db ) )
560
+ Markup :: fenced_block ( & original. display ( sema. db ) ) . into ( )
555
561
} ;
562
+ res. markup = desc. into ( ) ;
556
563
if let Some ( actions) = HoverAction :: goto_type_from_targets ( sema. db , targets) {
557
564
res. actions . push ( actions) ;
558
565
}
@@ -607,6 +614,9 @@ fn closure_ty(
607
614
{
608
615
format_to ! ( markup, "{layout}" ) ;
609
616
}
617
+ if let Some ( trait_) = c. fn_trait ( sema. db ) . get_id ( sema. db , original. krate ( sema. db ) . into ( ) ) {
618
+ push_new_def ( hir:: Trait :: from ( trait_) . into ( ) )
619
+ }
610
620
format_to ! (
611
621
markup,
612
622
"\n {}\n ```{adjusted}\n \n ## Captures\n {}" ,
0 commit comments