@@ -3,8 +3,8 @@ use std::{mem, ops::Not};
33
44use either:: Either ;
55use 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 ,
88} ;
99use ide_db:: {
1010 base_db:: SourceDatabase ,
@@ -25,7 +25,7 @@ use syntax::{
2525
2626use crate :: {
2727 doc_links:: { remove_links, rewrite_links} ,
28- hover:: walk_and_push_ty,
28+ hover:: { notable_traits , walk_and_push_ty} ,
2929 HoverAction , HoverConfig , HoverResult , Markup , MemoryLayoutHoverConfig ,
3030 MemoryLayoutHoverRenderKind ,
3131} ;
@@ -471,38 +471,8 @@ pub(super) fn definition(
471471 _ => None ,
472472 } ;
473473
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-
504474 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) {
506476 desc. push_str ( & notable_traits) ;
507477 desc. push ( '\n' ) ;
508478 }
@@ -519,6 +489,39 @@ pub(super) fn definition(
519489 markup ( docs. map ( Into :: into) , desc, mod_path)
520490}
521491
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+
522525fn type_info (
523526 sema : & Semantics < ' _ , RootDatabase > ,
524527 config : & HoverConfig ,
@@ -536,8 +539,12 @@ fn type_info(
536539 }
537540 } ;
538541 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 {
541548 walk_and_push_ty ( sema. db , & adjusted_ty, & mut push_new_def) ;
542549 let original = original. display ( sema. db ) . to_string ( ) ;
543550 let adjusted = adjusted_ty. display ( sema. db ) . to_string ( ) ;
@@ -549,10 +556,10 @@ fn type_info(
549556 apad = static_text_diff_len + adjusted. len( ) . max( original. len( ) ) ,
550557 opad = original. len( ) ,
551558 )
552- . into ( )
553559 } else {
554- Markup :: fenced_block ( & original. display ( sema. db ) )
560+ Markup :: fenced_block ( & original. display ( sema. db ) ) . into ( )
555561 } ;
562+ res. markup = desc. into ( ) ;
556563 if let Some ( actions) = HoverAction :: goto_type_from_targets ( sema. db , targets) {
557564 res. actions . push ( actions) ;
558565 }
@@ -607,6 +614,9 @@ fn closure_ty(
607614 {
608615 format_to ! ( markup, "{layout}" ) ;
609616 }
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+ }
610620 format_to ! (
611621 markup,
612622 "\n {}\n ```{adjusted}\n \n ## Captures\n {}" ,
0 commit comments