Support global excludes (#121)

* Support global excludes

Closes #119

Adds an extra field to the config to support excludes that apply to all formatters

* Move global config into its own toml section
This commit is contained in:
Basile Henry 2021-07-17 12:32:51 +02:00 committed by GitHub
parent 90727a47d3
commit a2db2cc181
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 26 deletions

View File

@ -22,3 +22,11 @@ This section describes the integration between a single formatter and
- `excludes`: A list of glob patterns to deny. If any of these patterns match,
the file will be excluded.
## `[global]`
This section describes the config that applies to every formatters.
- `excludes`: A list of glob patterns to deny. If any of these patterns match,
the file will be excluded. This list is appended to individual formatters'
exclude lists.

View File

@ -11,10 +11,20 @@ pub const FILENAME: &str = "treefmt.toml";
/// treefmt.toml structure
#[derive(Debug, Deserialize)]
pub struct Root {
/// Config that applies to every formatter
pub global: Option<GlobalConfig>,
/// Map of formatters into the config
pub formatter: BTreeMap<String, FmtConfig>,
}
/// Global config which applies to every formatter
#[derive(Debug, Deserialize)]
pub struct GlobalConfig {
/// Global glob to exclude files or folder for all formatters
#[serde(default)]
pub excludes: Vec<String>,
}
/// Config for each formatters
#[derive(Debug, Deserialize)]
pub struct FmtConfig {

View File

@ -73,22 +73,27 @@ pub fn run_treefmt(
// Load the treefmt.toml file
let project_config = config::from_path(&treefmt_toml)?;
let global_excludes = project_config
.global
.map(|g| g.excludes)
.unwrap_or_default();
timed_debug("load config");
// Load all the formatter instances from the config. Ignore the ones that failed.
let formatters =
project_config
.formatter
.iter()
.fold(BTreeMap::new(), |mut sum, (name, fmt_config)| {
match Formatter::from_config(&tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
};
sum
});
let formatters = project_config.formatter.into_iter().fold(
BTreeMap::new(),
|mut sum, (name, mut fmt_config)| {
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(&tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
};
sum
},
);
timed_debug("load formatters");
@ -331,20 +336,25 @@ pub fn run_treefmt_stdin(
// Load the treefmt.toml file
let project_config = config::from_path(&treefmt_toml)?;
let global_excludes = project_config
.global
.map(|g| g.excludes)
.unwrap_or_default();
// Load all the formatter instances from the config. Ignore the ones that failed.
let formatters =
project_config
.formatter
.iter()
.fold(BTreeMap::new(), |mut sum, (name, fmt_config)| {
match Formatter::from_config(&tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
};
sum
});
let formatters = project_config.formatter.into_iter().fold(
BTreeMap::new(),
|mut sum, (name, mut fmt_config)| {
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(&tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
Err(err) => error!("Ignoring formatter #{} due to error: {}", name, err),
};
sum
},
);
// Collect all formatters that match the path
let formatters: Vec<&Formatter> = formatters.values().filter(|f| f.is_match(&path)).collect();