From a70b621607d62f11f9e9daa51e795bf26d6a6531 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Dec 2015 17:25:49 +0300 Subject: [PATCH] add option to ignore out of line modules --- src/bin/rustfmt.rs | 2 ++ src/config.rs | 1 + src/lib.rs | 6 +++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 1b54d0d4d06..3e369e1ef23 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -79,6 +79,7 @@ fn lookup_and_read_project_file(input_file: &Path) -> io::Result<(PathBuf, Strin fn update_config(config: &mut Config, matches: &Matches) { config.verbose = matches.opt_present("verbose"); + config.skip_children = matches.opt_present("skip-children"); } fn execute() -> i32 { @@ -90,6 +91,7 @@ fn execute() -> i32 { "write-mode", "mode to write in (not usable when piping from stdin)", "[replace|overwrite|display|diff|coverage]"); + opts.optflag("", "skip-children", "don't reformat child modules"); opts.optflag("", "config-help", diff --git a/src/config.rs b/src/config.rs index 4c919191dbd..804ace83c6d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -259,6 +259,7 @@ fn default() -> Config { create_config! { verbose: bool, false, "Use verbose output"; + skip_children: bool, false, "Don't reformat out of line modules"; max_width: usize, 100, "Maximum width of each line"; ideal_width: usize, 80, "Ideal width of each line"; tab_spaces: usize, 4, "Number of spaces per tab"; diff --git a/src/lib.rs b/src/lib.rs index 0ee5f71a355..bae42ed44fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -298,11 +298,15 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { // Formatting which depends on the AST. fn fmt_ast(krate: &ast::Crate, parse_session: &ParseSess, + main_file: &Path, config: &Config, mode: WriteMode) -> FileMap { let mut file_map = FileMap::new(); for (path, module) in modules::list_files(krate, parse_session.codemap()) { + if config.skip_children && path.as_path() != main_file { + continue; + } let path = path.to_str().unwrap(); if config.verbose { println!("Formatting {}", path); @@ -431,7 +435,7 @@ pub fn format(file: &Path, config: &Config, mode: WriteMode) -> FileMap { let emitter = Box::new(EmitterWriter::new(Box::new(Vec::new()), None)); parse_session.span_diagnostic.handler = Handler::with_emitter(false, emitter); - let mut file_map = fmt_ast(&krate, &parse_session, config, mode); + let mut file_map = fmt_ast(&krate, &parse_session, file, config, mode); // For some reason, the codemap does not include terminating // newlines so we must add one on for each file. This is sad. -- 2.44.0