From 31c8fb4e7663687d2822c5e6d41c47f1143aef0f Mon Sep 17 00:00:00 2001 From: Michael Killough Date: Thu, 18 May 2017 12:56:49 +0700 Subject: [PATCH] Add --dump-default-config and --dump-minimal-config. - `--dump-default-config` outputs the default configuration to the specified file as TOML and then exits. - `--dump-minimal-config` is checked after formatting files as normal. If present, any configuration options that were checked during formatting are written to the specified file as TOML. - These options were added only to `rustfmt`, not to `cargo fmt`. They can be specified when using `cargo fmt` by placing them after `--`. - It would have been nice if the filename was optional, so you could run just `rusfmt --dump-minimal-config build.rs` to have it output to `rustfmt.toml`. However, this doesn't do what you might expect: it outputs the config to `build.rs`! --- src/bin/rustfmt.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index dc4087a066a..73c4439ba29 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -44,6 +44,7 @@ enum Operation { Format { files: Vec, config_path: Option, + minimal_config_path: Option, }, /// Print the help message. Help, @@ -51,6 +52,8 @@ enum Operation { Version, /// Print detailed configuration help. ConfigHelp, + /// Output default config to a file + ConfigOutputDefault { path: String }, /// No file specified, read from stdin Stdin { input: String, @@ -186,6 +189,14 @@ fn make_opts() -> Options { opts.optflag("", "config-help", "show details of rustfmt configuration options"); + opts.optopt("", + "dump-default-config", + "Dumps the default configuration to a file and exits.", + "PATH"); + opts.optopt("", + "dump-minimal-config", + "Dumps configuration options that were checked during formatting to a file.", + "PATH"); opts.optopt("", "config-path", "Recursively searches the given path for the rustfmt.toml config file. If not \ @@ -216,6 +227,12 @@ fn execute(opts: &Options) -> FmtResult { Config::print_docs(); Ok(Summary::new()) } + Operation::ConfigOutputDefault { path } => { + let mut file = File::create(path)?; + let toml = Config::default().all_options().to_toml()?; + file.write_all(toml.as_bytes())?; + Ok(Summary::new()) + } Operation::Stdin { input, config_path } => { // try to read config from local directory let (mut config, _) = match_cli_path_or_file(config_path, @@ -236,7 +253,11 @@ fn execute(opts: &Options) -> FmtResult { Ok(run(Input::Text(input), &config)) } - Operation::Format { files, config_path } => { + Operation::Format { + files, + config_path, + minimal_config_path, + } => { let options = CliOptions::from_matches(&matches)?; for f in options.file_lines.files() { @@ -286,6 +307,15 @@ fn execute(opts: &Options) -> FmtResult { error_summary.add(run(Input::File(file), &config)); } } + + // If we were given a path via dump-minimal-config, output any options + // that were used during formatting as TOML. + if let Some(path) = minimal_config_path { + let mut file = File::create(path)?; + let toml = config.used_options().to_toml()?; + file.write_all(toml.as_bytes())?; + } + Ok(error_summary) } } @@ -353,6 +383,10 @@ fn determine_operation(matches: &Matches) -> FmtResult { return Ok(Operation::ConfigHelp); } + if let Some(path) = matches.opt_str("dump-default-config") { + return Ok(Operation::ConfigOutputDefault { path }); + } + if matches.opt_present("version") { return Ok(Operation::Version); } @@ -383,6 +417,9 @@ fn determine_operation(matches: &Matches) -> FmtResult { path @ _ => path, }; + // If no path is given, we won't output a minimal config. + let minimal_config_path = matches.opt_str("dump-minimal-config"); + // if no file argument is supplied, read from stdin if matches.free.is_empty() { let mut buffer = String::new(); @@ -408,5 +445,6 @@ fn determine_operation(matches: &Matches) -> FmtResult { Ok(Operation::Format { files: files, config_path: config_path, + minimal_config_path: minimal_config_path, }) } -- 2.44.0