@@ -26,6 +26,7 @@ use comment::rewrite_comment;
26
26
use macros:: { rewrite_macro, MacroPosition } ;
27
27
use items:: { rewrite_static, rewrite_associated_type, rewrite_associated_impl_type,
28
28
rewrite_type_alias, format_impl, format_trait} ;
29
+ use lists:: { itemize_list, write_list, DefinitiveListTactic , ListFormatting , SeparatorTactic } ;
29
30
30
31
fn is_use_item ( item : & ast:: Item ) -> bool {
31
32
match item. node {
@@ -637,6 +638,81 @@ impl<'a> FmtVisitor<'a> {
637
638
}
638
639
}
639
640
641
+ impl Rewrite for ast:: NestedMetaItem {
642
+ fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
643
+ match self . node {
644
+ ast:: NestedMetaItemKind :: MetaItem ( ref meta_item) => meta_item. rewrite ( context, shape) ,
645
+ ast:: NestedMetaItemKind :: Literal ( ..) => Some ( context. snippet ( self . span ) ) ,
646
+ }
647
+ }
648
+ }
649
+
650
+ impl Rewrite for ast:: MetaItem {
651
+ fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
652
+ Some ( match self . node {
653
+ ast:: MetaItemKind :: Word => context. snippet ( self . span ) ,
654
+ ast:: MetaItemKind :: List ( ref list) => {
655
+ let name = self . name . as_str ( ) ;
656
+ // 3 = `#[` and `(`, 2 = `]` and `)`
657
+ let item_shape = try_opt ! ( shape
658
+ . shrink_left( name. len( ) + 3 )
659
+ . and_then( |s| s. sub_width( 1 ) ) ) ;
660
+ let items = itemize_list ( context. codemap ,
661
+ list. iter ( ) ,
662
+ ")" ,
663
+ |nested_meta_item| nested_meta_item. span . lo ,
664
+ |nested_meta_item| nested_meta_item. span . hi ,
665
+ |nested_meta_item| {
666
+ nested_meta_item. rewrite ( context, item_shape)
667
+ } ,
668
+ self . span . lo ,
669
+ self . span . hi ) ;
670
+ let item_vec = items. collect :: < Vec < _ > > ( ) ;
671
+ let fmt = ListFormatting {
672
+ tactic : DefinitiveListTactic :: Mixed ,
673
+ separator : "," ,
674
+ trailing_separator : SeparatorTactic :: Never ,
675
+ shape : item_shape,
676
+ ends_with_newline : false ,
677
+ config : context. config ,
678
+ } ;
679
+ format ! ( "{}({})" , name, try_opt!( write_list( & item_vec, & fmt) ) )
680
+ }
681
+ ast:: MetaItemKind :: NameValue ( ref literal) => {
682
+ let name = self . name . as_str ( ) ;
683
+ let value = context. snippet ( literal. span ) ;
684
+ if & * name == "doc" && value. starts_with ( "///" ) {
685
+ let doc_shape = Shape {
686
+ width : cmp:: min ( shape. width , context. config . comment_width ( ) )
687
+ . checked_sub ( shape. indent . width ( ) )
688
+ . unwrap_or ( 0 ) ,
689
+ ..shape
690
+ } ;
691
+ format ! ( "{}" ,
692
+ try_opt!( rewrite_comment( & value,
693
+ false ,
694
+ doc_shape,
695
+ context. config) ) )
696
+ } else {
697
+ format ! ( "{} = {}" , name, value)
698
+ }
699
+ }
700
+ } )
701
+ }
702
+ }
703
+
704
+ impl Rewrite for ast:: Attribute {
705
+ fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
706
+ self . value
707
+ . rewrite ( context, shape)
708
+ . map ( |rw| if rw. starts_with ( "///" ) {
709
+ rw
710
+ } else {
711
+ format ! ( "#[{}]" , rw)
712
+ } )
713
+ }
714
+ }
715
+
640
716
impl < ' a > Rewrite for [ ast:: Attribute ] {
641
717
fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
642
718
let mut result = String :: new ( ) ;
@@ -646,7 +722,7 @@ impl<'a> Rewrite for [ast::Attribute] {
646
722
let indent = shape. indent . to_string ( context. config ) ;
647
723
648
724
for ( i, a) in self . iter ( ) . enumerate ( ) {
649
- let mut a_str = context . snippet ( a. span ) ;
725
+ let a_str = try_opt ! ( a. rewrite ( context , shape ) ) ;
650
726
651
727
// Write comments and blank lines between attributes.
652
728
if i > 0 {
@@ -673,15 +749,6 @@ impl<'a> Rewrite for [ast::Attribute] {
673
749
result. push_str ( & indent) ;
674
750
}
675
751
676
- if a_str. starts_with ( "//" ) {
677
- a_str = try_opt ! ( rewrite_comment( & a_str,
678
- false ,
679
- Shape :: legacy( context. config. comment_width( ) -
680
- shape. indent. width( ) ,
681
- shape. indent) ,
682
- context. config) ) ;
683
- }
684
-
685
752
// Write the attribute itself.
686
753
result. push_str ( & a_str) ;
687
754
0 commit comments