@@ -18,6 +18,7 @@ extern crate env_logger;
18
18
extern crate getopts;
19
19
20
20
use rustfmt:: { run, Input , Summary } ;
21
+ use rustfmt:: file_lines:: FileLines ;
21
22
use rustfmt:: config:: { Config , WriteMode } ;
22
23
23
24
use std:: { env, error} ;
@@ -57,6 +58,7 @@ struct CliOptions {
57
58
skip_children : bool ,
58
59
verbose : bool ,
59
60
write_mode : Option < WriteMode > ,
61
+ file_lines : FileLines , // Default is all lines in all files.
60
62
}
61
63
62
64
impl CliOptions {
@@ -73,12 +75,17 @@ impl CliOptions {
73
75
}
74
76
}
75
77
78
+ if let Some ( ref file_lines) = matches. opt_str ( "file-lines" ) {
79
+ options. file_lines = try!( file_lines. parse ( ) ) ;
80
+ }
81
+
76
82
Ok ( options)
77
83
}
78
84
79
- fn apply_to ( & self , config : & mut Config ) {
85
+ fn apply_to ( self , config : & mut Config ) {
80
86
config. skip_children = self . skip_children ;
81
87
config. verbose = self . verbose ;
88
+ config. file_lines = self . file_lines ;
82
89
if let Some ( write_mode) = self . write_mode {
83
90
config. write_mode = write_mode;
84
91
}
@@ -168,6 +175,10 @@ fn make_opts() -> Options {
168
175
"Recursively searches the given path for the rustfmt.toml config file. If not \
169
176
found reverts to the input file path",
170
177
"[Path for the configuration file]" ) ;
178
+ opts. optopt ( "" ,
179
+ "file-lines" ,
180
+ "Format specified line ranges. See README for more detail on the JSON format." ,
181
+ "JSON" ) ;
171
182
172
183
opts
173
184
}
@@ -198,8 +209,12 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
198
209
199
210
Ok ( run ( Input :: Text ( input) , & config) )
200
211
}
201
- Operation :: Format { files, config_path } => {
212
+ Operation :: Format { mut files, config_path } => {
202
213
let options = try!( CliOptions :: from_matches ( & matches) ) ;
214
+
215
+ // Add any additional files that were specified via `--file-lines`.
216
+ files. extend ( options. file_lines . files ( ) . cloned ( ) . map ( PathBuf :: from) ) ;
217
+
203
218
let mut config = Config :: default ( ) ;
204
219
let mut path = None ;
205
220
// Load the config path file if provided
@@ -227,7 +242,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
227
242
config = config_tmp;
228
243
}
229
244
230
- options. apply_to ( & mut config) ;
245
+ options. clone ( ) . apply_to ( & mut config) ;
231
246
error_summary. add ( run ( Input :: File ( file) , & config) ) ;
232
247
}
233
248
Ok ( error_summary)
@@ -306,8 +321,8 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
306
321
Some ( dir)
307
322
} ) ;
308
323
309
- // if no file argument is supplied, read from stdin
310
- if matches. free . is_empty ( ) {
324
+ // if no file argument is supplied and `--file-lines` is not specified , read from stdin
325
+ if matches. free . is_empty ( ) && !matches . opt_present ( "file-lines" ) {
311
326
312
327
let mut buffer = String :: new ( ) ;
313
328
try!( io:: stdin ( ) . read_to_string ( & mut buffer) ) ;
@@ -318,6 +333,7 @@ fn determine_operation(matches: &Matches) -> FmtResult<Operation> {
318
333
} ) ;
319
334
}
320
335
336
+ // We append files from `--file-lines` later in `execute()`.
321
337
let files: Vec < _ > = matches. free . iter ( ) . map ( PathBuf :: from) . collect ( ) ;
322
338
323
339
Ok ( Operation :: Format {
0 commit comments