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