@@ -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:: {
@@ -171,7 +172,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
171
172
172
173
if skip_rewrite {
173
174
self . push_rewrite ( b. span , None ) ;
174
- self . close_block ( false ) ;
175
+ self . close_block ( false , b . span ) ;
175
176
self . last_pos = source ! ( self , b. span) . hi ( ) ;
176
177
return ;
177
178
}
@@ -188,21 +189,25 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
188
189
189
190
let mut remove_len = BytePos ( 0 ) ;
190
191
if let Some ( stmt) = b. stmts . last ( ) {
191
- let snippet = self . snippet ( mk_sp (
192
+ let span_after_last_stmt = mk_sp (
192
193
stmt. span . hi ( ) ,
193
194
source ! ( self , b. span) . hi ( ) - brace_compensation,
194
- ) ) ;
195
- let len = CommentCodeSlices :: new ( snippet)
196
- . last ( )
197
- . and_then ( |( kind, _, s) | {
198
- if kind == CodeCharKind :: Normal && s. trim ( ) . is_empty ( ) {
199
- Some ( s. len ( ) )
200
- } else {
201
- None
202
- }
203
- } ) ;
204
- if let Some ( len) = len {
205
- remove_len = BytePos :: from_usize ( len) ;
195
+ ) ;
196
+ // if the span is outside of a file_lines range, then do not try to remove anything
197
+ if !out_of_file_lines_range ! ( self , span_after_last_stmt) {
198
+ let snippet = self . snippet ( span_after_last_stmt) ;
199
+ let len = CommentCodeSlices :: new ( snippet)
200
+ . last ( )
201
+ . and_then ( |( kind, _, s) | {
202
+ if kind == CodeCharKind :: Normal && s. trim ( ) . is_empty ( ) {
203
+ Some ( s. len ( ) )
204
+ } else {
205
+ None
206
+ }
207
+ } ) ;
208
+ if let Some ( len) = len {
209
+ remove_len = BytePos :: from_usize ( len) ;
210
+ }
206
211
}
207
212
}
208
213
@@ -221,24 +226,31 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
221
226
if unindent_comment {
222
227
self . block_indent = self . block_indent . block_indent ( self . config ) ;
223
228
}
224
- self . close_block ( unindent_comment) ;
229
+ self . close_block ( unindent_comment, b . span ) ;
225
230
self . last_pos = source ! ( self , b. span) . hi ( ) ;
226
231
}
227
232
228
233
// FIXME: this is a terrible hack to indent the comments between the last
229
234
// item in the block and the closing brace to the block's level.
230
235
// The closing brace itself, however, should be indented at a shallower
231
236
// level.
232
- fn close_block ( & mut self , unindent_comment : bool ) {
233
- let total_len = self . buffer . len ( ) ;
234
- let chars_too_many = if unindent_comment {
235
- 0
236
- } else if self . config . hard_tabs ( ) {
237
- 1
238
- } else {
239
- self . config . tab_spaces ( )
240
- } ;
241
- self . buffer . truncate ( total_len - chars_too_many) ;
237
+ fn close_block ( & mut self , unindent_comment : bool , span : Span ) {
238
+ let file_name: FileName = self . source_map . span_to_filename ( span) . into ( ) ;
239
+ let skip_this_line = !self
240
+ . config
241
+ . file_lines ( )
242
+ . contains_line ( & file_name, self . line_number ) ;
243
+ if !skip_this_line {
244
+ let total_len = self . buffer . len ( ) ;
245
+ let chars_too_many = if unindent_comment {
246
+ 0
247
+ } else if self . config . hard_tabs ( ) {
248
+ 1
249
+ } else {
250
+ self . config . tab_spaces ( )
251
+ } ;
252
+ self . buffer . truncate ( total_len - chars_too_many) ;
253
+ }
242
254
self . push_str ( "}" ) ;
243
255
self . block_indent = self . block_indent . block_unindent ( self . config ) ;
244
256
}
@@ -789,7 +801,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
789
801
self . visit_attrs ( attrs, ast:: AttrStyle :: Inner ) ;
790
802
self . walk_mod_items ( m) ;
791
803
self . format_missing_with_indent ( source ! ( self , m. inner) . hi ( ) - BytePos ( 1 ) ) ;
792
- self . close_block ( false ) ;
804
+ self . close_block ( false , m . inner ) ;
793
805
}
794
806
self . last_pos = source ! ( self , m. inner) . hi ( ) ;
795
807
} else {
0 commit comments