@@ -59,11 +59,19 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
59
59
fn vertical_trim ( lines : ~[ ~str ] ) -> ~[ ~str ] {
60
60
let mut i = 0 u;
61
61
let mut j = lines. len ( ) ;
62
+ // first line of all-stars should be omitted
63
+ if lines. len ( ) > 0 && lines[ 0 ] . iter ( ) . all ( |c| c == '*' ) {
64
+ i += 1 ;
65
+ }
62
66
while i < j && lines[ i] . trim ( ) . is_empty ( ) {
63
- i += 1 u;
67
+ i += 1 ;
68
+ }
69
+ // like the first, a last line of all stars should be omitted
70
+ if j > i && lines[ j - 1 ] . iter ( ) . skip ( 1 ) . all ( |c| c == '*' ) {
71
+ j -= 1 ;
64
72
}
65
- while j > i && lines[ j - 1 u ] . trim ( ) . is_empty ( ) {
66
- j -= 1 u ;
73
+ while j > i && lines[ j - 1 ] . trim ( ) . is_empty ( ) {
74
+ j -= 1 ;
67
75
}
68
76
return lines. slice ( i, j) . to_owned ( ) ;
69
77
}
@@ -106,8 +114,12 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
106
114
}
107
115
}
108
116
109
- if comment. starts_with ( "//" ) {
110
- return comment. slice ( 3 u, comment. len ( ) ) . to_owned ( ) ;
117
+ // one-line comments lose their prefix
118
+ static ONLINERS : & ' static [ & ' static str ] = & [ "///!" , "///" , "//!" , "//" ] ;
119
+ for prefix in ONLINERS . iter ( ) {
120
+ if comment. starts_with ( * prefix) {
121
+ return comment. slice_from ( prefix. len ( ) ) . to_owned ( ) ;
122
+ }
111
123
}
112
124
113
125
if comment. starts_with ( "/*" ) {
@@ -384,29 +396,42 @@ mod test {
384
396
385
397
#[ test] fn test_block_doc_comment_1 ( ) {
386
398
let comment = "/**\n * Test \n ** Test\n * Test\n */" ;
387
- let correct_stripped = " Test \n * Test\n Test" ;
388
399
let stripped = strip_doc_comment_decoration ( comment) ;
389
- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
400
+ assert_eq!( stripped, ~" Test \n * Test \n Test " );
390
401
}
391
402
392
403
#[test] fn test_block_doc_comment_2() {
393
404
let comment = " /**\n * Test\n * Test\n*/ ";
394
- let correct_stripped = " Test\n Test" ;
395
405
let stripped = strip_doc_comment_decoration(comment);
396
- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
406
+ assert_eq!(stripped, ~" Test \n Test " );
397
407
}
398
408
399
409
#[test] fn test_block_doc_comment_3() {
400
410
let comment = " /**\n let a: *int;\n *a = 5;\n*/ ";
401
- let correct_stripped = " let a: *int;\n *a = 5;" ;
402
411
let stripped = strip_doc_comment_decoration(comment);
403
- assert_eq ! ( stripped. slice ( 0 , stripped . len ( ) ) , correct_stripped ) ;
412
+ assert_eq!(stripped, ~" let a : * int ; \n * a = 5 ; " );
404
413
}
405
414
406
- #[ test] fn test_line_doc_comment ( ) {
407
- let comment = "/// Test" ;
408
- let correct_stripped = " Test" ;
415
+ #[test] fn test_block_doc_comment_4() {
416
+ let comment = " /*******************\n test\n *********************/ ";
409
417
let stripped = strip_doc_comment_decoration(comment);
410
- assert_eq ! ( stripped. slice( 0 , stripped. len( ) ) , correct_stripped) ;
418
+ assert_eq!(stripped, ~" test");
419
+ }
420
+
421
+ #[test] fn test_line_doc_comment() {
422
+ let stripped = strip_doc_comment_decoration(" /// test");
423
+ assert_eq!( stripped, ~" test");
424
+ let stripped = strip_doc_comment_decoration(" ///! test");
425
+ assert_eq!( stripped, ~" test");
426
+ let stripped = strip_doc_comment_decoration(" // test");
427
+ assert_eq!( stripped, ~" test");
428
+ let stripped = strip_doc_comment_decoration(" // test");
429
+ assert_eq!( stripped, ~" test");
430
+ let stripped = strip_doc_comment_decoration(" ///test");
431
+ assert_eq!( stripped, ~"test");
432
+ let stripped = strip_doc_comment_decoration(" ///!test");
433
+ assert_eq!( stripped, ~"test");
434
+ let stripped = strip_doc_comment_decoration(" //test");
435
+ assert_eq!( stripped, ~"test" ) ;
411
436
}
412
437
}
0 commit comments