Skip to content

Commit 5b6f643

Browse files
authored
Merge pull request rust-lang#3280 from m-ou-se/global-config
Look for a global rustfmt.toml.
2 parents 203e6d2 + 635a4cd commit 5b6f643

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

Cargo.lock

+56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ failure = "0.1.3"
5555
bytecount = "0.5"
5656
unicode-width = "0.1.5"
5757
unicode_categories = "0.1.1"
58+
dirs = "1.0.4"
5859

5960
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
6061
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`

Configurations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuring Rustfmt
22

3-
Rustfmt is designed to be very configurable. You can create a TOML file called `rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent directory and it will apply the options in that file.
3+
Rustfmt is designed to be very configurable. You can create a TOML file called `rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent directory and it will apply the options in that file. If none of these directories contain such a file, both your home directory and a directory called `rustfmt` in your [global config directory](https://docs.rs/dirs/1.0.4/dirs/fn.config_dir.html) (e.g. `.config/rustfmt/`) are checked as well.
44

55
A possible content of `rustfmt.toml` or `.rustfmt.toml` might look like this:
66

src/config/config_type.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,26 @@ macro_rules! create_config {
345345

346346
// If the current directory has no parent, we're done searching.
347347
if !current.pop() {
348-
return Ok(None);
348+
break;
349349
}
350350
}
351+
352+
// If nothing was found, check in the home directory.
353+
if let Some(home_dir) = dirs::home_dir() {
354+
if let Some(path) = get_toml_path(&home_dir)? {
355+
return Ok(Some(path));
356+
}
357+
}
358+
359+
// If none was found ther either, check in the user's configuration directory.
360+
if let Some(mut config_dir) = dirs::config_dir() {
361+
config_dir.push("rustfmt");
362+
if let Some(path) = get_toml_path(&config_dir)? {
363+
return Ok(Some(path));
364+
}
365+
}
366+
367+
return Ok(None);
351368
}
352369

353370
match resolve_project_file(dir)? {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern crate derive_new;
1313
extern crate atty;
1414
extern crate bytecount;
1515
extern crate diff;
16+
extern crate dirs;
1617
extern crate failure;
1718
extern crate itertools;
1819
#[cfg(test)]

0 commit comments

Comments
 (0)