@@ -96,21 +96,6 @@ impl<'a> CommentStyle<'a> {
96
96
pub fn to_str_tuplet ( & self ) -> ( & ' a str , & ' a str , & ' a str ) {
97
97
( self . opener ( ) , self . closer ( ) , self . line_start ( ) )
98
98
}
99
-
100
- pub fn line_with_same_comment_style ( & self , line : & str , normalize_comments : bool ) -> bool {
101
- match * self {
102
- CommentStyle :: DoubleSlash | CommentStyle :: TripleSlash | CommentStyle :: Doc => {
103
- line. trim_left ( ) . starts_with ( self . line_start ( ) . trim_left ( ) )
104
- || comment_style ( line, normalize_comments) == * self
105
- }
106
- CommentStyle :: DoubleBullet | CommentStyle :: SingleBullet | CommentStyle :: Exclamation => {
107
- line. trim_left ( ) . starts_with ( self . closer ( ) . trim_left ( ) )
108
- || line. trim_left ( ) . starts_with ( self . line_start ( ) . trim_left ( ) )
109
- || comment_style ( line, normalize_comments) == * self
110
- }
111
- CommentStyle :: Custom ( opener) => line. trim_left ( ) . starts_with ( opener. trim_right ( ) ) ,
112
- }
113
- }
114
99
}
115
100
116
101
fn comment_style ( orig : & str , normalize_comments : bool ) -> CommentStyle {
@@ -273,19 +258,56 @@ fn identify_comment(
273
258
is_doc_comment : bool ,
274
259
) -> Option < String > {
275
260
let style = comment_style ( orig, false ) ;
276
- let first_group = orig
277
- . lines ( )
278
- . take_while ( |l| style. line_with_same_comment_style ( l, false ) )
279
- . collect :: < Vec < _ > > ( )
280
- . join ( "\n " ) ;
281
- let rest = orig
282
- . lines ( )
283
- . skip ( first_group. lines ( ) . count ( ) )
284
- . collect :: < Vec < _ > > ( )
285
- . join ( "\n " ) ;
261
+ let mut first_group_ending = 0 ;
286
262
263
+ fn compute_len ( orig : & str , line : & str ) -> usize {
264
+ if orig. len ( ) > line. len ( ) {
265
+ if orig. as_bytes ( ) [ line. len ( ) ] == b'\r' {
266
+ line. len ( ) + 2
267
+ } else {
268
+ line. len ( ) + 1
269
+ }
270
+ } else {
271
+ line. len ( )
272
+ }
273
+ }
274
+
275
+ match style {
276
+ CommentStyle :: DoubleSlash | CommentStyle :: TripleSlash | CommentStyle :: Doc => {
277
+ let line_start = style. line_start ( ) . trim_left ( ) ;
278
+ for line in orig. lines ( ) {
279
+ if line. trim_left ( ) . starts_with ( line_start) || comment_style ( line, false ) == style {
280
+ first_group_ending += compute_len ( & orig[ first_group_ending..] , line) ;
281
+ } else {
282
+ break ;
283
+ }
284
+ }
285
+ }
286
+ CommentStyle :: Custom ( opener) => {
287
+ let trimmed_opener = opener. trim_right ( ) ;
288
+ for line in orig. lines ( ) {
289
+ if line. trim_left ( ) . starts_with ( trimmed_opener) {
290
+ first_group_ending += compute_len ( & orig[ first_group_ending..] , line) ;
291
+ } else {
292
+ break ;
293
+ }
294
+ }
295
+ }
296
+ // for a block comment, search for the closing symbol
297
+ CommentStyle :: DoubleBullet | CommentStyle :: SingleBullet | CommentStyle :: Exclamation => {
298
+ let closer = style. closer ( ) . trim_left ( ) ;
299
+ for line in orig. lines ( ) {
300
+ first_group_ending += compute_len ( & orig[ first_group_ending..] , line) ;
301
+ if line. trim_left ( ) . ends_with ( closer) {
302
+ break ;
303
+ }
304
+ }
305
+ }
306
+ }
307
+
308
+ let ( first_group, rest) = orig. split_at ( first_group_ending) ;
287
309
let first_group_str = rewrite_comment_inner (
288
- & first_group,
310
+ first_group,
289
311
block_style,
290
312
style,
291
313
shape,
@@ -295,7 +317,7 @@ fn identify_comment(
295
317
if rest. is_empty ( ) {
296
318
Some ( first_group_str)
297
319
} else {
298
- identify_comment ( & rest, block_style, shape, config, is_doc_comment) . map ( |rest_str| {
320
+ identify_comment ( rest, block_style, shape, config, is_doc_comment) . map ( |rest_str| {
299
321
format ! (
300
322
"{}\n {}{}" ,
301
323
first_group_str,
@@ -427,8 +449,12 @@ fn rewrite_comment_inner(
427
449
}
428
450
} else if is_prev_line_multi_line && !line. is_empty ( ) {
429
451
result. push ( ' ' )
430
- } else if is_last && !closer. is_empty ( ) && line. is_empty ( ) {
431
- result. push_str ( & indent_str) ;
452
+ } else if is_last && line. is_empty ( ) {
453
+ // trailing blank lines are unwanted
454
+ if !closer. is_empty ( ) {
455
+ result. push_str ( & indent_str) ;
456
+ }
457
+ break ;
432
458
} else {
433
459
result. push_str ( & comment_line_separator) ;
434
460
if !has_leading_whitespace && result. ends_with ( ' ' ) {
0 commit comments