@@ -397,8 +397,11 @@ fn gen_comment<'a>(comment: SyntaxToken, context: &mut Context<'a>) -> PrintItem
397
397
items. extend ( {
398
398
if context. config . comment_force_leading_space {
399
399
let info = get_comment_text_info ( comment. text ( ) ) ;
400
- let after_hash_text = & comment. text ( ) [ info. leading_hashes_count ..] . trim_end ( ) ;
400
+ let after_hash_text = & comment. text ( ) [ info. start_text_index ..] . trim_end ( ) ;
401
401
let mut text = "#" . repeat ( info. leading_hashes_count ) ;
402
+ if info. has_exclamation_point {
403
+ text. push ( '!' ) ;
404
+ }
402
405
if !after_hash_text. is_empty ( ) {
403
406
if !info. has_leading_whitespace {
404
407
text. push ( ' ' ) ;
@@ -416,15 +419,31 @@ fn gen_comment<'a>(comment: SyntaxToken, context: &mut Context<'a>) -> PrintItem
416
419
417
420
struct CommentTextInfo {
418
421
pub has_leading_whitespace : bool ,
422
+ pub has_exclamation_point : bool ,
419
423
pub leading_hashes_count : usize ,
424
+ pub start_text_index : usize ,
420
425
}
421
426
422
427
fn get_comment_text_info ( text : & str ) -> CommentTextInfo {
423
428
let mut leading_hashes_count = 0 ;
424
429
let mut has_leading_whitespace = false ;
425
- for c in text. chars ( ) {
430
+ let mut has_exclamation_point = false ;
431
+ let mut start_text_index = 0 ;
432
+ let mut chars = text. char_indices ( ) ;
433
+ for ( index, c) in chars. by_ref ( ) {
426
434
match c {
427
- '#' => leading_hashes_count += 1 ,
435
+ '#' if !has_exclamation_point => {
436
+ leading_hashes_count += 1 ;
437
+ start_text_index = index + 1 ;
438
+ }
439
+ '!' if leading_hashes_count == 1 => {
440
+ has_exclamation_point = true ;
441
+ start_text_index = index + 1 ;
442
+ if matches ! ( chars. next( ) , Some ( ( _, ' ' | '\t' ) ) ) {
443
+ has_leading_whitespace = true ;
444
+ }
445
+ break ;
446
+ }
428
447
' ' | '\t' => {
429
448
has_leading_whitespace = true ;
430
449
break ;
@@ -434,7 +453,9 @@ fn get_comment_text_info(text: &str) -> CommentTextInfo {
434
453
}
435
454
CommentTextInfo {
436
455
leading_hashes_count,
456
+ has_exclamation_point,
437
457
has_leading_whitespace,
458
+ start_text_index,
438
459
}
439
460
}
440
461
0 commit comments