From e52f6b0b030c6c82c08a769e5851a9b63df08c00 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 25 Aug 2020 20:54:42 -0500 Subject: [PATCH] feat(ignore): Typos-specific ignores THis is to help with cases like a monorepo with vendored dependencies. A user might want to search (`.ignore`) them but not hold the code to the same standards as first-party code. Fixes #134 --- docs/reference.md | 1 + src/config.rs | 37 ++++++++++++++++++++++++++++++++++++- src/main.rs | 10 ++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index bed6f82d2..3233f9aee 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -15,6 +15,7 @@ Configuration is read from the following (in precedence order) | Field | Argument | Format | Description | |------------------------|-------------------|--------|-------------| | files.binary | --binary | bool | Check binary files as text | +| files.ignore-patterns | | list of strings | Typos-specific ignore globs (gitignore syntax) | | files.ignore-hidden | --hidden | bool | Skip hidden files and directories. | | files.ignore-files | --ignore | bool | Respect ignore files. | | files.ignore-dot | --ignore-dot | bool | Respect .ignore files. | diff --git a/src/config.rs b/src/config.rs index e74cf90fa..33f4bc16e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,16 @@ pub trait WalkSource { None } + /// The root for `ignore_patterns` + fn ignore_root(&self) -> Option<&std::path::Path> { + None + } + + /// Ignore the specified patterns (gitignore syntax) + fn ignore_patterns(&self) -> Option<&[String]> { + None + } + /// Skip hidden files and directories. fn ignore_hidden(&self) -> Option { None @@ -101,7 +111,9 @@ impl Config { let mut file = std::fs::File::open(path)?; let mut s = String::new(); file.read_to_string(&mut s)?; - Self::from_toml(&s) + let mut c = Self::from_toml(&s)?; + c.files.ignore_root = path.parent().map(|p| p.to_owned()); + Ok(c) } pub fn from_toml(data: &str) -> Result { @@ -138,6 +150,9 @@ impl ConfigSource for Config { #[serde(rename_all = "kebab-case")] pub struct Walk { pub binary: Option, + #[serde(skip)] + pub ignore_root: Option, + pub ignore_patterns: Option>, pub ignore_hidden: Option, pub ignore_files: Option, pub ignore_dot: Option, @@ -151,6 +166,10 @@ impl Walk { if let Some(source) = source.binary() { self.binary = Some(source); } + if let (Some(root), Some(source)) = (source.ignore_root(), source.ignore_patterns()) { + self.ignore_root = Some(root.to_owned()); + self.ignore_patterns = Some(source.to_owned()); + } if let Some(source) = source.ignore_hidden() { self.ignore_hidden = Some(source); } @@ -180,6 +199,14 @@ impl Walk { self.binary.unwrap_or(false) } + pub fn ignore_root(&self) -> Option<&std::path::Path> { + self.ignore_root.as_ref().map(|v| v.as_path()) + } + + pub fn ignore_patterns(&self) -> Option<&[String]> { + self.ignore_patterns.as_ref().map(|v| v.as_slice()) + } + pub fn ignore_hidden(&self) -> bool { self.ignore_hidden.unwrap_or(true) } @@ -215,6 +242,14 @@ impl WalkSource for Walk { self.binary } + fn ignore_root(&self) -> Option<&std::path::Path> { + self.ignore_root.as_ref().map(|v| v.as_path()) + } + + fn ignore_patterns(&self) -> Option<&[String]> { + self.ignore_patterns.as_ref().map(|v| v.as_slice()) + } + fn ignore_hidden(&self) -> Option { self.ignore_hidden } diff --git a/src/main.rs b/src/main.rs index f45dc1116..8275ec9d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,16 @@ fn run() -> Result { .git_ignore(config.files.ignore_vcs()) .git_exclude(config.files.ignore_vcs()) .parents(config.files.ignore_parent()); + if let (Some(root), Some(patterns)) = + (config.files.ignore_root(), config.files.ignore_patterns()) + { + let mut overrides = ignore::overrides::OverrideBuilder::new(root); + for pattern in patterns { + overrides.add(pattern)?; + } + let overrides = overrides.build()?; + walk.overrides(overrides); + } let mut reporter = args.format.reporter(); let replace_reporter = replace::Replace::new(reporter);