Skip to content

Commit 9107fac

Browse files
committed
Merge pull request #719 from matklad/ignore-child-mods
add option to ignore out of line modules
2 parents 47f473d + a70b621 commit 9107fac

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/bin/rustfmt.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, Strin
7979

8080
fn update_config(config: &mut Config, matches: &Matches) {
8181
config.verbose = matches.opt_present("verbose");
82+
config.skip_children = matches.opt_present("skip-children");
8283
}
8384

8485
fn execute() -> i32 {
@@ -90,6 +91,7 @@ fn execute() -> i32 {
9091
"write-mode",
9192
"mode to write in (not usable when piping from stdin)",
9293
"[replace|overwrite|display|diff|coverage]");
94+
opts.optflag("", "skip-children", "don't reformat child modules");
9395

9496
opts.optflag("",
9597
"config-help",

src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ macro_rules! create_config {
259259

260260
create_config! {
261261
verbose: bool, false, "Use verbose output";
262+
skip_children: bool, false, "Don't reformat out of line modules";
262263
max_width: usize, 100, "Maximum width of each line";
263264
ideal_width: usize, 80, "Ideal width of each line";
264265
tab_spaces: usize, 4, "Number of spaces per tab";

src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,15 @@ impl fmt::Display for FormatReport {
298298
// Formatting which depends on the AST.
299299
fn fmt_ast(krate: &ast::Crate,
300300
parse_session: &ParseSess,
301+
main_file: &Path,
301302
config: &Config,
302303
mode: WriteMode)
303304
-> FileMap {
304305
let mut file_map = FileMap::new();
305306
for (path, module) in modules::list_files(krate, parse_session.codemap()) {
307+
if config.skip_children && path.as_path() != main_file {
308+
continue;
309+
}
306310
let path = path.to_str().unwrap();
307311
if config.verbose {
308312
println!("Formatting {}", path);
@@ -431,7 +435,7 @@ pub fn format(file: &Path, config: &Config, mode: WriteMode) -> FileMap {
431435
let emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None));
432436
parse_session.span_diagnostic.handler = Handler::with_emitter(false, emitter);
433437

434-
let mut file_map = fmt_ast(&krate, &parse_session, config, mode);
438+
let mut file_map = fmt_ast(&krate, &parse_session, file, config, mode);
435439

436440
// For some reason, the codemap does not include terminating
437441
// newlines so we must add one on for each file. This is sad.

0 commit comments

Comments
 (0)