@@ -195,10 +195,13 @@ fn rustfmt_args(config: &Config, config_path: &Path) -> Vec<String> {
195
195
"--quiet" . into( ) ,
196
196
] ;
197
197
198
- args. push ( "--file-lines" . into ( ) ) ;
199
- let file_lines_json = config. file_lines ( ) . to_json_spans ( ) ;
200
- let lines: String = serde_json:: to_string ( & file_lines_json) . unwrap ( ) ;
201
- args. push ( lines) ;
198
+ // Otherwise --file-lines [] are treated as no lines rather than FileLines::all()
199
+ if config. file_lines ( ) . files ( ) . count ( ) > 0 {
200
+ args. push ( "--file-lines" . into ( ) ) ;
201
+ let file_lines_json = config. file_lines ( ) . to_json_spans ( ) ;
202
+ let lines = serde_json:: to_string ( & file_lines_json) . unwrap ( ) ;
203
+ args. push ( lines) ;
204
+ }
202
205
203
206
args. push ( "--config-path" . into ( ) ) ;
204
207
args. push ( config_path. to_str ( ) . map ( ToOwned :: to_owned) . unwrap ( ) ) ;
@@ -211,6 +214,8 @@ mod tests {
211
214
use super :: * ;
212
215
use crate :: config:: FmtConfig ;
213
216
use lsp_types:: { Position , Range , TextEdit } ;
217
+ use rustfmt_nightly:: FileLines ;
218
+ use std:: str:: FromStr ;
214
219
215
220
#[ test]
216
221
fn calc_text_edits ( ) {
@@ -244,4 +249,20 @@ mod tests {
244
249
vec ! [ ( 0 , 0 , 1 , 0 , "struct Upper;\n " ) , ( 2 , 0 , 3 , 0 , "struct Lower;\n " ) ] ,
245
250
) ;
246
251
}
252
+
253
+ #[ test]
254
+ fn no_empty_file_lines ( ) {
255
+ let config_with_lines = {
256
+ let mut config = Config :: default ( ) ;
257
+ config. set ( ) . file_lines (
258
+ FileLines :: from_str ( r#"[{ "file": "stdin", "range": [0, 5] }]"# ) . unwrap ( ) ,
259
+ ) ;
260
+ config
261
+ } ;
262
+ let args = rustfmt_args ( & config_with_lines, Path :: new ( "dummy" ) ) ;
263
+ assert ! ( args. join( " " ) . find( "--file-lines" ) . is_some( ) ) ;
264
+
265
+ let args = rustfmt_args ( & Config :: default ( ) , Path :: new ( "dummy" ) ) ;
266
+ assert_eq ! ( args. join( " " ) . find( "--file-lines" ) , None ) ;
267
+ }
247
268
}
0 commit comments