@@ -407,6 +407,31 @@ fn light_rewrite_block_comment_with_bare_lines(
407
407
Some ( format ! ( "{}\n {}{}" , first_line, indent_str, rest) )
408
408
}
409
409
410
+ /// Attributes for code blocks in rustdoc.
411
+ /// See https://doc.rust-lang.org/rustdoc/print.html#attributes
412
+ enum CodeBlockAttribute {
413
+ Rust ,
414
+ Ignore ,
415
+ Text ,
416
+ ShouldPanic ,
417
+ NoRun ,
418
+ CompileFail ,
419
+ }
420
+
421
+ impl CodeBlockAttribute {
422
+ fn new ( attribute : & str ) -> CodeBlockAttribute {
423
+ match attribute {
424
+ "rust" | "" => CodeBlockAttribute :: Rust ,
425
+ "ignore" => CodeBlockAttribute :: Ignore ,
426
+ "text" => CodeBlockAttribute :: Text ,
427
+ "should_panic" => CodeBlockAttribute :: ShouldPanic ,
428
+ "no_run" => CodeBlockAttribute :: NoRun ,
429
+ "compile_fail" => CodeBlockAttribute :: CompileFail ,
430
+ _ => CodeBlockAttribute :: Text ,
431
+ }
432
+ }
433
+ }
434
+
410
435
fn rewrite_comment_inner (
411
436
orig : & str ,
412
437
block_style : bool ,
@@ -466,7 +491,7 @@ fn rewrite_comment_inner(
466
491
result. push_str ( opener) ;
467
492
let mut code_block_buffer = String :: with_capacity ( 128 ) ;
468
493
let mut is_prev_line_multi_line = false ;
469
- let mut inside_code_block = false ;
494
+ let mut code_block_attr = None ;
470
495
let comment_line_separator = format ! ( "{}{}" , indent_str, line_start) ;
471
496
let join_code_block_with_comment_line_separator = |s : & str | {
472
497
let mut result = String :: with_capacity ( s. len ( ) + 128 ) ;
@@ -485,28 +510,36 @@ fn rewrite_comment_inner(
485
510
for ( i, ( line, has_leading_whitespace) ) in lines. enumerate ( ) {
486
511
let is_last = i == count_newlines ( orig) ;
487
512
488
- if inside_code_block {
513
+ if let Some ( ref attr ) = code_block_attr {
489
514
if line. starts_with ( "```" ) {
490
- inside_code_block = false ;
491
- result. push_str ( & comment_line_separator) ;
492
- let code_block = {
493
- let mut config = config. clone ( ) ;
494
- config. set ( ) . wrap_comments ( false ) ;
495
- match :: format_code_block ( & code_block_buffer, & config) {
496
- Some ( ref s) => trim_custom_comment_prefix ( s) ,
497
- None => trim_custom_comment_prefix ( & code_block_buffer) ,
515
+ let code_block = match attr {
516
+ CodeBlockAttribute :: Ignore | CodeBlockAttribute :: Text => {
517
+ trim_custom_comment_prefix ( & code_block_buffer)
518
+ }
519
+ _ if code_block_buffer. is_empty ( ) => String :: new ( ) ,
520
+ _ => {
521
+ let mut config = config. clone ( ) ;
522
+ config. set ( ) . wrap_comments ( false ) ;
523
+ match :: format_code_block ( & code_block_buffer, & config) {
524
+ Some ( ref s) => trim_custom_comment_prefix ( s) ,
525
+ None => trim_custom_comment_prefix ( & code_block_buffer) ,
526
+ }
498
527
}
499
528
} ;
500
- result. push_str ( & join_code_block_with_comment_line_separator ( & code_block) ) ;
529
+ if !code_block. is_empty ( ) {
530
+ result. push_str ( & comment_line_separator) ;
531
+ result. push_str ( & join_code_block_with_comment_line_separator ( & code_block) ) ;
532
+ }
501
533
code_block_buffer. clear ( ) ;
502
534
result. push_str ( & comment_line_separator) ;
503
535
result. push_str ( line) ;
536
+ code_block_attr = None ;
504
537
} else {
505
538
code_block_buffer. push_str ( & hide_sharp_behind_comment ( line) ) ;
506
539
code_block_buffer. push ( '\n' ) ;
507
540
508
541
if is_last {
509
- // There is an code block that is not properly enclosed by backticks.
542
+ // There is a code block that is not properly enclosed by backticks.
510
543
// We will leave them untouched.
511
544
result. push_str ( & comment_line_separator) ;
512
545
result. push_str ( & join_code_block_with_comment_line_separator (
@@ -517,7 +550,11 @@ fn rewrite_comment_inner(
517
550
518
551
continue ;
519
552
} else {
520
- inside_code_block = line. starts_with ( "```" ) ;
553
+ code_block_attr = if line. starts_with ( "```" ) {
554
+ Some ( CodeBlockAttribute :: new ( & line[ 3 ..] ) )
555
+ } else {
556
+ None
557
+ } ;
521
558
522
559
if result == opener {
523
560
let force_leading_whitespace = opener == "/* " && count_newlines ( orig) == 0 ;
0 commit comments