@@ -6,6 +6,7 @@ use syntax::{ast, visit};
6
6
7
7
use crate :: attr:: * ;
8
8
use crate :: comment:: { CodeCharKind , CommentCodeSlices , FindUncommented } ;
9
+ use crate :: config:: file_lines:: FileName ;
9
10
use crate :: config:: { BraceStyle , Config , Version } ;
10
11
use crate :: expr:: { format_expr, ExprType } ;
11
12
use crate :: items:: {
@@ -170,7 +171,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
170
171
171
172
if skip_rewrite {
172
173
self . push_rewrite ( b. span , None ) ;
173
- self . close_block ( false ) ;
174
+ self . close_block ( false , b . span ) ;
174
175
self . last_pos = source ! ( self , b. span) . hi ( ) ;
175
176
return ;
176
177
}
@@ -187,21 +188,25 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
187
188
188
189
let mut remove_len = BytePos ( 0 ) ;
189
190
if let Some ( stmt) = b. stmts . last ( ) {
190
- let snippet = self . snippet ( mk_sp (
191
+ let span_after_last_stmt = mk_sp (
191
192
stmt. span . hi ( ) ,
192
193
source ! ( self , b. span) . hi ( ) - brace_compensation,
193
- ) ) ;
194
- let len = CommentCodeSlices :: new ( snippet)
195
- . last ( )
196
- . and_then ( |( kind, _, s) | {
197
- if kind == CodeCharKind :: Normal && s. trim ( ) . is_empty ( ) {
198
- Some ( s. len ( ) )
199
- } else {
200
- None
201
- }
202
- } ) ;
203
- if let Some ( len) = len {
204
- remove_len = BytePos :: from_usize ( len) ;
194
+ ) ;
195
+ // if the span is outside of a file_lines range, then do not try to remove anything
196
+ if !out_of_file_lines_range ! ( self , span_after_last_stmt) {
197
+ let snippet = self . snippet ( span_after_last_stmt) ;
198
+ let len = CommentCodeSlices :: new ( snippet)
199
+ . last ( )
200
+ . and_then ( |( kind, _, s) | {
201
+ if kind == CodeCharKind :: Normal && s. trim ( ) . is_empty ( ) {
202
+ Some ( s. len ( ) )
203
+ } else {
204
+ None
205
+ }
206
+ } ) ;
207
+ if let Some ( len) = len {
208
+ remove_len = BytePos :: from_usize ( len) ;
209
+ }
205
210
}
206
211
}
207
212
@@ -220,24 +225,31 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
220
225
if unindent_comment {
221
226
self . block_indent = self . block_indent . block_indent ( self . config ) ;
222
227
}
223
- self . close_block ( unindent_comment) ;
228
+ self . close_block ( unindent_comment, b . span ) ;
224
229
self . last_pos = source ! ( self , b. span) . hi ( ) ;
225
230
}
226
231
227
232
// FIXME: this is a terrible hack to indent the comments between the last
228
233
// item in the block and the closing brace to the block's level.
229
234
// The closing brace itself, however, should be indented at a shallower
230
235
// level.
231
- fn close_block ( & mut self , unindent_comment : bool ) {
232
- let total_len = self . buffer . len ( ) ;
233
- let chars_too_many = if unindent_comment {
234
- 0
235
- } else if self . config . hard_tabs ( ) {
236
- 1
237
- } else {
238
- self . config . tab_spaces ( )
239
- } ;
240
- self . buffer . truncate ( total_len - chars_too_many) ;
236
+ fn close_block ( & mut self , unindent_comment : bool , span : Span ) {
237
+ let file_name: FileName = self . source_map . span_to_filename ( span) . into ( ) ;
238
+ let skip_this_line = !self
239
+ . config
240
+ . file_lines ( )
241
+ . contains_line ( & file_name, self . line_number ) ;
242
+ if !skip_this_line {
243
+ let total_len = self . buffer . len ( ) ;
244
+ let chars_too_many = if unindent_comment {
245
+ 0
246
+ } else if self . config . hard_tabs ( ) {
247
+ 1
248
+ } else {
249
+ self . config . tab_spaces ( )
250
+ } ;
251
+ self . buffer . truncate ( total_len - chars_too_many) ;
252
+ }
241
253
self . push_str ( "}" ) ;
242
254
self . block_indent = self . block_indent . block_unindent ( self . config ) ;
243
255
}
@@ -759,7 +771,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
759
771
self . visit_attrs ( attrs, ast:: AttrStyle :: Inner ) ;
760
772
self . walk_mod_items ( m) ;
761
773
self . format_missing_with_indent ( source ! ( self , m. inner) . hi ( ) - BytePos ( 1 ) ) ;
762
- self . close_block ( false ) ;
774
+ self . close_block ( false , m . inner ) ;
763
775
}
764
776
self . last_pos = source ! ( self , m. inner) . hi ( ) ;
765
777
} else {
0 commit comments