Multiple config file names feature (#1101)

* Add multiple configuration file names feature

* Add '.rustfmt.toml' in README file

* Clean up configuration file code

* Make config file names constant

* Use only one blank line
This commit is contained in:
Daniel Campoverde 2016-07-31 16:32:35 -05:00 committed by Nick Cameron
parent e76cb6a907
commit 6380937b59
2 changed files with 22 additions and 16 deletions

View file

@ -124,9 +124,10 @@ notes above on running rustfmt.
## Configuring Rustfmt
Rustfmt is designed to be very configurable. You can create a TOML file called
rustfmt.toml, place it in the project directory and it will apply the options
in that file. See `rustfmt --config-help` for the options which are available,
or if you prefer to see source code, [src/config.rs](src/config.rs).
`rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent
directory and it will apply the options in that file. See `rustfmt
--config-help` for the options which are available, or if you prefer to see
source code, [src/config.rs](src/config.rs).
By default, Rustfmt uses a style which (mostly) conforms to the
[Rust style guidelines](https://github.com/rust-lang/rust/tree/master/src/doc/style).
@ -148,8 +149,9 @@ options covering different styles. File an issue, or even better, submit a PR.
#[rustfmt_skip] // requires nightly and #![feature(custom_attribute)] in crate root
#[cfg_attr(rustfmt, rustfmt_skip)] // works in stable
```
* When you run rustfmt, place a file named rustfmt.toml in target file
directory or its parents to override the default settings of rustfmt.
* When you run rustfmt, place a file named `rustfmt.toml` or `.rustfmt.toml` in
target file directory or its parents to override the default settings of
rustfmt.
* After successful compilation, a `rustfmt` executable can be found in the
target directory.
* If you're having issues compiling Rustfmt (or compile errors when trying to

View file

@ -109,20 +109,24 @@ fn lookup_project_file(dir: &Path) -> FmtResult<Option<PathBuf>> {
current = try!(fs::canonicalize(current));
const CONFIG_FILE_NAMES: [&'static str; 2] = [".rustfmt.toml", "rustfmt.toml"];
loop {
let config_file = current.join("rustfmt.toml");
match fs::metadata(&config_file) {
// Only return if it's a file to handle the unlikely situation of a directory named
// `rustfmt.toml`.
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
// Return the error if it's something other than `NotFound`; otherwise we didn't find
// the project file yet, and continue searching.
Err(e) => {
if e.kind() != ErrorKind::NotFound {
return Err(FmtError::from(e));
for config_file_name in &CONFIG_FILE_NAMES {
let config_file = current.join(config_file_name);
match fs::metadata(&config_file) {
// Only return if it's a file to handle the unlikely situation of a directory named
// `rustfmt.toml`.
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
// Return the error if it's something other than `NotFound`; otherwise we didn't
// find the project file yet, and continue searching.
Err(e) => {
if e.kind() != ErrorKind::NotFound {
return Err(FmtError::from(e));
}
}
_ => {}
}
_ => {}
}
// If the current directory has no parent, we're done searching.