@@ -496,32 +496,71 @@ pub fn make_test(s: &str,
496
496
497
497
// FIXME(aburka): use a real parser to deal with multiline attributes
498
498
fn partition_source ( s : & str ) -> ( String , String , String ) {
499
- let mut after_header = false ;
499
+ #[ derive( Copy , Clone , PartialEq ) ]
500
+ enum PartitionState {
501
+ Attrs ,
502
+ Crates ,
503
+ Other ,
504
+ }
505
+ let mut state = PartitionState :: Attrs ;
500
506
let mut before = String :: new ( ) ;
501
507
let mut crates = String :: new ( ) ;
502
508
let mut after = String :: new ( ) ;
503
509
504
510
for line in s. lines ( ) {
505
511
let trimline = line. trim ( ) ;
506
- let header = trimline. chars ( ) . all ( |c| c. is_whitespace ( ) ) ||
507
- trimline. starts_with ( "#![" ) ||
508
- trimline. starts_with ( "#[macro_use] extern crate" ) ||
509
- trimline. starts_with ( "extern crate" ) ;
510
- if !header || after_header {
511
- after_header = true ;
512
- after. push_str ( line) ;
513
- after. push_str ( "\n " ) ;
514
- } else {
515
- if trimline. starts_with ( "#[macro_use] extern crate" )
516
- || trimline. starts_with ( "extern crate" ) {
512
+
513
+ // FIXME(misdreavus): if a doc comment is placed on an extern crate statement, it will be
514
+ // shunted into "everything else"
515
+ match state {
516
+ PartitionState :: Attrs => {
517
+ state = if trimline. starts_with ( "#![" ) ||
518
+ trimline. chars ( ) . all ( |c| c. is_whitespace ( ) ) ||
519
+ ( trimline. starts_with ( "//" ) && !trimline. starts_with ( "///" ) )
520
+ {
521
+ PartitionState :: Attrs
522
+ } else if trimline. starts_with ( "extern crate" ) ||
523
+ trimline. starts_with ( "#[macro_use] extern crate" )
524
+ {
525
+ PartitionState :: Crates
526
+ } else {
527
+ PartitionState :: Other
528
+ } ;
529
+ }
530
+ PartitionState :: Crates => {
531
+ state = if trimline. starts_with ( "extern crate" ) ||
532
+ trimline. starts_with ( "#[macro_use] extern crate" ) ||
533
+ trimline. chars ( ) . all ( |c| c. is_whitespace ( ) ) ||
534
+ ( trimline. starts_with ( "//" ) && !trimline. starts_with ( "///" ) )
535
+ {
536
+ PartitionState :: Crates
537
+ } else {
538
+ PartitionState :: Other
539
+ } ;
540
+ }
541
+ PartitionState :: Other => { }
542
+ }
543
+
544
+ match state {
545
+ PartitionState :: Attrs => {
546
+ before. push_str ( line) ;
547
+ before. push_str ( "\n " ) ;
548
+ }
549
+ PartitionState :: Crates => {
517
550
crates. push_str ( line) ;
518
551
crates. push_str ( "\n " ) ;
519
552
}
520
- before. push_str ( line) ;
521
- before. push_str ( "\n " ) ;
553
+ PartitionState :: Other => {
554
+ after. push_str ( line) ;
555
+ after. push_str ( "\n " ) ;
556
+ }
522
557
}
523
558
}
524
559
560
+ debug ! ( "before:\n {}" , before) ;
561
+ debug ! ( "crates:\n {}" , crates) ;
562
+ debug ! ( "after:\n {}" , after) ;
563
+
525
564
( before, after, crates)
526
565
}
527
566
@@ -1038,8 +1077,8 @@ fn main() {
1038
1077
assert_eq!(2+2, 4);" ;
1039
1078
let expected =
1040
1079
"#![allow(unused)]
1041
- fn main() {
1042
1080
//Ceci n'est pas une `fn main`
1081
+ fn main() {
1043
1082
assert_eq!(2+2, 4);
1044
1083
}" . to_string ( ) ;
1045
1084
let output = make_test ( input, None , false , & opts) ;
@@ -1086,8 +1125,8 @@ assert_eq!(2+2, 4);";
1086
1125
1087
1126
let expected =
1088
1127
"#![allow(unused)]
1089
- fn main() {
1090
1128
// fn main
1129
+ fn main() {
1091
1130
assert_eq!(2+2, 4);
1092
1131
}" . to_string ( ) ;
1093
1132
0 commit comments