From d5f1200ed6a8e375f963e0c59a8bee45c0018c55 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 1 May 2024 17:44:25 +0200 Subject: [PATCH] Add documentation for negated ignored files and add a test for it as well --- Configurations.md | 9 +++++++++ src/ignore_path.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/Configurations.md b/Configurations.md index 2d01fb3bb3b..f52c2573154 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1306,6 +1306,15 @@ If you want to ignore every file under the directory where you put your rustfmt. ignore = ["/"] ``` +If you want to allow specific paths that would otherwise be ignored, prefix those paths with a `!`: + +```toml +ignore = ["bar_dir/*", "!bar_dir/*/what.rs"] +``` + +In this case, all files under `bar_dir` will be ignored, except files like `bar_dir/sub/what.rs` +or `bar_dir/another/what.rs`. + ## `imports_indent` Indent style of imports diff --git a/src/ignore_path.rs b/src/ignore_path.rs index 7b5697bec3e..5c25f233ce3 100644 --- a/src/ignore_path.rs +++ b/src/ignore_path.rs @@ -49,4 +49,22 @@ mod test { assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz.rs")))); assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/bar.rs")))); } + + #[nightly_only_test] + #[test] + fn test_negated_ignore_path_set() { + use crate::config::{Config, FileName}; + use crate::ignore_path::IgnorePathSet; + use std::path::{Path, PathBuf}; + + let config = Config::from_toml( + r#"ignore = ["foo.rs", "bar_dir/*", "!bar_dir/*/what.rs"]"#, + Path::new(""), + ) + .unwrap(); + let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap(); + assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/what.rs")))); + assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/a.rs")))); + assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/what.rs")))); + } }