@@ -287,12 +287,13 @@ fn rewrite_comment_inner(
287
287
. checked_sub ( closer. len ( ) + opener. len ( ) )
288
288
. unwrap_or ( 1 ) ;
289
289
let indent_str = shape. indent . to_string ( config) ;
290
- let fmt = StringFormat {
290
+ let fmt_indent = shape. indent + ( opener. len ( ) - line_start. len ( ) ) ;
291
+ let mut fmt = StringFormat {
291
292
opener : "" ,
292
293
closer : "" ,
293
294
line_start : line_start,
294
295
line_end : "" ,
295
- shape : Shape :: legacy ( max_chars, shape . indent + ( opener . len ( ) - line_start . len ( ) ) ) ,
296
+ shape : Shape :: legacy ( max_chars, fmt_indent ) ,
296
297
trim_end : true ,
297
298
config : config,
298
299
} ;
@@ -317,26 +318,69 @@ fn rewrite_comment_inner(
317
318
} ) ;
318
319
319
320
let mut result = opener. to_owned ( ) ;
321
+ let mut is_prev_line_multi_line = false ;
322
+ let comment_line_separator = format ! ( "\n {}{}" , indent_str, line_start) ;
320
323
for line in lines {
321
324
if result == opener {
322
325
if line. is_empty ( ) {
323
326
continue ;
324
327
}
325
328
} else {
326
- result. push ( '\n' ) ;
327
- result. push_str ( & indent_str) ;
328
- result. push_str ( line_start) ;
329
+ if is_prev_line_multi_line && !line. is_empty ( ) {
330
+ result. push ( ' ' )
331
+ } else {
332
+ result. push_str ( & comment_line_separator) ;
333
+ }
329
334
}
330
335
331
- if config. wrap_comments ( ) && line. len ( ) > max_chars {
332
- let rewrite = rewrite_string ( line, & fmt) . unwrap_or_else ( || line. to_owned ( ) ) ;
333
- result. push_str ( & rewrite) ;
336
+ if config. wrap_comments ( ) && line. len ( ) > fmt. shape . width && !has_url ( line) {
337
+ match rewrite_string ( line, & fmt, Some ( max_chars) ) {
338
+ Some ( ref s) => {
339
+ is_prev_line_multi_line = s. contains ( '\n' ) ;
340
+ result. push_str ( s) ;
341
+ }
342
+ None if is_prev_line_multi_line => {
343
+ // We failed to put the current `line` next to the previous `line`.
344
+ // Remove the trailing space, then start rewrite on the next line.
345
+ result. pop ( ) ;
346
+ result. push_str ( & comment_line_separator) ;
347
+ fmt. shape = Shape :: legacy ( max_chars, fmt_indent) ;
348
+ match rewrite_string ( line, & fmt, Some ( max_chars) ) {
349
+ Some ( ref s) => {
350
+ is_prev_line_multi_line = s. contains ( '\n' ) ;
351
+ result. push_str ( s) ;
352
+ }
353
+ None => {
354
+ is_prev_line_multi_line = false ;
355
+ result. push_str ( line) ;
356
+ }
357
+ }
358
+ }
359
+ None => {
360
+ is_prev_line_multi_line = false ;
361
+ result. push_str ( line) ;
362
+ }
363
+ }
364
+
365
+ fmt. shape = if is_prev_line_multi_line {
366
+ // 1 = " "
367
+ let offset = 1 + last_line_width ( & result) - line_start. len ( ) ;
368
+ Shape {
369
+ width : max_chars. checked_sub ( offset) . unwrap_or ( 0 ) ,
370
+ indent : fmt_indent,
371
+ offset : fmt. shape . offset + offset,
372
+ }
373
+ } else {
374
+ Shape :: legacy ( max_chars, fmt_indent)
375
+ } ;
334
376
} else {
335
377
if line. is_empty ( ) && result. ends_with ( ' ' ) {
336
378
// Remove space if this is an empty comment or a doc comment.
337
379
result. pop ( ) ;
338
380
}
339
381
result. push_str ( line) ;
382
+ fmt. shape = Shape :: legacy ( max_chars, fmt_indent) ;
383
+ is_prev_line_multi_line = false ;
340
384
}
341
385
}
342
386
0 commit comments