@@ -153,11 +153,10 @@ pub fn combine_strs_with_missing_comments(
153
153
let mut one_line_width =
154
154
last_line_width ( prev_str) + first_line_width ( next_str) + first_sep. len ( ) ;
155
155
156
- let original_snippet = context. snippet ( span) ;
157
- let trimmed_snippet = original_snippet. trim ( ) ;
158
156
let indent_str = shape. indent . to_string ( context. config ) ;
157
+ let missing_comment = try_opt ! ( rewrite_missing_comment( span, shape, context) ) ;
159
158
160
- if trimmed_snippet . is_empty ( ) {
159
+ if missing_comment . is_empty ( ) {
161
160
if allow_extend && prev_str. len ( ) + first_sep. len ( ) + next_str. len ( ) <= shape. width {
162
161
return Some ( format ! ( "{}{}{}" , prev_str, first_sep, next_str) ) ;
163
162
} else {
@@ -175,18 +174,13 @@ pub fn combine_strs_with_missing_comments(
175
174
// Peek the the original source code and find out whether there is a newline between the first
176
175
// expression and the second expression or the missing comment. We will preserve the orginal
177
176
// layout whenever possible.
177
+ let original_snippet = context. snippet ( span) ;
178
178
let prefer_same_line = if let Some ( pos) = original_snippet. chars ( ) . position ( |c| c == '/' ) {
179
179
!original_snippet[ ..pos] . contains ( '\n' )
180
180
} else {
181
181
!original_snippet. contains ( '\n' )
182
182
} ;
183
183
184
- let missing_comment = try_opt ! ( rewrite_comment(
185
- trimmed_snippet,
186
- false ,
187
- shape,
188
- context. config
189
- ) ) ;
190
184
one_line_width -= first_sep. len ( ) ;
191
185
let first_sep = if prev_str. is_empty ( ) || missing_comment. is_empty ( ) {
192
186
String :: new ( )
@@ -365,6 +359,50 @@ fn rewrite_comment_inner(
365
359
Some ( result)
366
360
}
367
361
362
+ /// Given the span, rewrite the missing comment inside it if available.
363
+ /// Note that the given span must only include comments (or leading/trailing whitespaces).
364
+ pub fn rewrite_missing_comment (
365
+ span : Span ,
366
+ shape : Shape ,
367
+ context : & RewriteContext ,
368
+ ) -> Option < String > {
369
+ let missing_snippet = context. snippet ( span) ;
370
+ let trimmed_snippet = missing_snippet. trim ( ) ;
371
+ if !trimmed_snippet. is_empty ( ) {
372
+ rewrite_comment ( trimmed_snippet, false , shape, context. config )
373
+ } else {
374
+ Some ( String :: new ( ) )
375
+ }
376
+ }
377
+
378
+ /// Recover the missing comments in the specified span, if available.
379
+ /// The layout of the comments will be preserved as long as it does not break the code
380
+ /// and its total width does not exceed the max width.
381
+ pub fn recover_missing_comment_in_span (
382
+ span : Span ,
383
+ shape : Shape ,
384
+ context : & RewriteContext ,
385
+ used_width : usize ,
386
+ ) -> Option < String > {
387
+ let missing_comment = try_opt ! ( rewrite_missing_comment( span, shape, context) ) ;
388
+ if missing_comment. is_empty ( ) {
389
+ Some ( String :: new ( ) )
390
+ } else {
391
+ let missing_snippet = context. snippet ( span) ;
392
+ let pos = missing_snippet. chars ( ) . position ( |c| c == '/' ) . unwrap_or ( 0 ) ;
393
+ // 1 = ` `
394
+ let total_width = missing_comment. len ( ) + used_width + 1 ;
395
+ let force_new_line_before_comment =
396
+ missing_snippet[ ..pos] . contains ( '\n' ) || total_width > context. config . max_width ( ) ;
397
+ let sep = if force_new_line_before_comment {
398
+ format ! ( "\n {}" , shape. indent. to_string( context. config) )
399
+ } else {
400
+ String :: from ( " " )
401
+ } ;
402
+ Some ( format ! ( "{}{}" , sep, missing_comment) )
403
+ }
404
+ }
405
+
368
406
/// Trims whitespace and aligns to indent, but otherwise does not change comments.
369
407
fn light_rewrite_comment ( orig : & str , offset : Indent , config : & Config ) -> Option < String > {
370
408
let lines: Vec < & str > = orig. lines ( )
0 commit comments