@@ -188,64 +188,59 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
188
188
pub ( crate ) fn advance_to_first_block_item (
189
189
& mut self ,
190
190
first_item_pos : Option < BytePos > ,
191
- ) -> String {
192
- let mut result = String :: new ( ) ;
193
- if let Some ( first_item_pos) = first_item_pos {
194
- let missing_span = self . next_span ( first_item_pos) ;
195
- let snippet = self . snippet ( missing_span) ;
196
-
197
- if self . config . preserve_block_start_blank_lines ( ) {
198
- // First we need to find the span to look for blank lines in. This is either the
199
- // - span between the opening brace and first item, or
200
- // - span between the opening brace and a comment before the first item
201
- // We do this so that we get a span of contiguous whitespace, which makes
202
- // processing the blank lines easier.
203
- let blank_lines_snippet = if let Some ( hi) = self
204
- . snippet_provider
205
- . span_to_snippet ( missing_span)
206
- . and_then ( |s| s. find ( '/' ) )
207
- {
208
- self . snippet_provider . span_to_snippet ( mk_sp (
209
- missing_span. lo ( ) ,
210
- missing_span. lo ( ) + BytePos :: from_usize ( hi) ,
211
- ) )
191
+ ) -> Option < String > {
192
+ let missing_span = first_item_pos. map ( |pos| self . next_span ( pos) ) ?;
193
+ let snippet = self . snippet ( missing_span) ;
194
+
195
+ let len = CommentCodeSlices :: new ( snippet)
196
+ . next ( )
197
+ . and_then ( |( kind, _, s) | {
198
+ if kind == CodeCharKind :: Normal {
199
+ s. rfind ( '\n' )
212
200
} else {
213
- self . snippet_provider . span_to_snippet ( missing_span)
214
- } ;
215
-
216
- if let Some ( snippet) = blank_lines_snippet {
217
- let has_multiple_blank_lines =
218
- if let ( Some ( l) , Some ( r) ) = ( snippet. find ( '\n' ) , snippet. rfind ( '\n' ) ) {
219
- l != r
220
- } else {
221
- false
222
- } ;
223
- if has_multiple_blank_lines {
224
- let mut lines = snippet. lines ( ) . map ( & str:: trim) ;
225
- lines. next ( ) ; // Eat block-opening newline
226
- while let Some ( "" ) = lines. next ( ) {
227
- result. push ( '\n' ) ;
228
- }
229
- }
230
- } else {
231
- debug ! ( "Failed to preserve blank lines for {:?}" , missing_span) ;
201
+ None
232
202
}
233
- }
203
+ } ) ;
204
+ if let Some ( len) = len {
205
+ self . last_pos = self . last_pos + BytePos :: from_usize ( len) ;
206
+ }
234
207
235
- let len = CommentCodeSlices :: new ( snippet)
236
- . next ( )
237
- . and_then ( |( kind, _, s) | {
238
- if kind == CodeCharKind :: Normal {
239
- s. rfind ( '\n' )
240
- } else {
241
- None
208
+ if self . config . preserve_block_start_blank_lines ( ) {
209
+ // First we need to find the span to look for blank lines in. This is either the
210
+ // - span between the opening brace and first item, or
211
+ // - span between the opening brace and a comment before the first item
212
+ // We do this so that we get a span of contiguous whitespace, which makes processing the
213
+ // blank lines easier.
214
+ let blank_lines_snippet = if let Some ( hi) = self
215
+ . snippet_provider
216
+ . span_to_snippet ( missing_span)
217
+ . and_then ( |s| s. find ( '/' ) )
218
+ {
219
+ self . snippet_provider . span_to_snippet ( mk_sp (
220
+ missing_span. lo ( ) ,
221
+ missing_span. lo ( ) + BytePos :: from_usize ( hi) ,
222
+ ) )
223
+ } else {
224
+ self . snippet_provider . span_to_snippet ( missing_span)
225
+ } ;
226
+
227
+ if let Some ( snippet) = blank_lines_snippet {
228
+ if snippet. find ( '\n' ) != snippet. rfind ( '\n' ) {
229
+ let mut lines = snippet. lines ( ) . map ( & str:: trim) ;
230
+ lines. next ( ) ; // Eat block-opening newline
231
+ let mut result = String :: new ( ) ;
232
+ while let Some ( "" ) = lines. next ( ) {
233
+ result. push ( '\n' ) ;
242
234
}
243
- } ) ;
244
- if let Some ( len) = len {
245
- self . last_pos = self . last_pos + BytePos :: from_usize ( len) ;
235
+ if !result. is_empty ( ) {
236
+ return Some ( result) ;
237
+ }
238
+ }
239
+ } else {
240
+ debug ! ( "Failed to preserve blank lines for {:?}" , missing_span) ;
246
241
}
247
242
}
248
- result
243
+ None
249
244
}
250
245
251
246
pub ( crate ) fn visit_block (
@@ -269,8 +264,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
269
264
let first_non_ws = inner_attrs
270
265
. and_then ( |attrs| attrs. first ( ) . map ( |attr| attr. span . lo ( ) ) )
271
266
. or_else ( || b. stmts . first ( ) . map ( |s| s. span ( ) . lo ( ) ) ) ;
272
- let opening_nls = & self . advance_to_first_block_item ( first_non_ws) ;
273
- self . push_str ( & opening_nls) ;
267
+ if let Some ( opening_nls) = self . advance_to_first_block_item ( first_non_ws) {
268
+ self . push_str ( & opening_nls) ;
269
+ }
274
270
275
271
// Format inner attributes if available.
276
272
if let Some ( attrs) = inner_attrs {
@@ -995,8 +991,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
995
991
. first ( )
996
992
. map ( |attr| attr. span . lo ( ) )
997
993
. or_else ( || m. items . first ( ) . map ( |s| s. span ( ) . lo ( ) ) ) ;
998
- let opening_nls = & self . advance_to_first_block_item ( first_non_ws) ;
999
- self . push_str ( & opening_nls) ;
994
+ if let Some ( opening_nls) = self . advance_to_first_block_item ( first_non_ws) {
995
+ self . push_str ( & opening_nls) ;
996
+ }
1000
997
1001
998
self . visit_attrs ( attrs, ast:: AttrStyle :: Inner ) ;
1002
999
self . walk_mod_items ( m) ;
0 commit comments