Skip to content

Commit

Permalink
feat: start to support typos config files
Browse files Browse the repository at this point in the history
To ease usability, it now supports loading the relevant fields from a
config file of `typos`. It ignores the irrelevant fields.
  • Loading branch information
ronnychevalier committed Jul 16, 2024
1 parent c09b3f4 commit fde1cb2
Show file tree
Hide file tree
Showing 6 changed files with 584 additions and 39 deletions.
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ lang-yaml = ["dep:tree-sitter-yaml"]
anyhow = "1.0.86"
clap = { version = "4.5.9", features = ["derive"] }
ignore = "0.4.22"
kstring = { version = "2.0.0", features = ["serde"] }
miette = { version = "7.2.0", features = ["fancy"] }
serde = { version = "1.0.204", features = ["derive"] }
thiserror = "1.0.61"
toml = "0.8.14"
tree-sitter = "0.22.6"
tree-sitter-c = { version = "0.21.4", optional = true }
tree-sitter-cpp = { version = "0.22.2", optional = true }
Expand Down
96 changes: 59 additions & 37 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fs::Metadata;
use std::path::Path;
use std::path::PathBuf;

use ignore::{DirEntry, WalkBuilder};
use ignore::DirEntry;

use orthotypos::config;
use orthotypos::config::Config;

#[derive(Copy, Clone, PartialEq, Eq, clap::ValueEnum, Default)]
pub enum Format {
Expand Down Expand Up @@ -39,8 +41,53 @@ pub(crate) struct Args {
walk: WalkArgs,
}

impl Args {
pub fn to_walk<'a>(
&'a self,
config: &'a Config,
) -> anyhow::Result<impl Iterator<Item = DirEntry> + 'a> {
let mut overrides = ignore::overrides::OverrideBuilder::new(".");
for pattern in &config.files.extend_exclude {
overrides.add(&format!("!{}", pattern))?;
}
let overrides = overrides.build()?;

Ok(self.path.iter().flat_map(move |path| {
let mut walk = config.to_walk_builder(path);
if self.sort {
walk.sort_by_file_name(|a, b| a.cmp(b));
}
if !config.files.extend_exclude.is_empty() {
walk.overrides(overrides.clone());
}
walk.build().filter_map(Result::ok).filter(|entry| {
entry
.metadata()
.as_ref()
.map(Metadata::is_file)
.unwrap_or(false)
})
}))
}

pub fn format(&self) -> Format {
self.format
}

pub fn to_config(&self) -> config::Config {
config::Config {
files: self.walk.to_config(),
..Default::default()
}
}
}

#[derive(clap::Args)]
struct WalkArgs {
/// Ignore files and directories matching the glob.
#[arg(long, value_name = "GLOB")]
exclude: Vec<String>,

/// Search hidden files and directories
#[arg(long)]
hidden: bool,
Expand All @@ -66,41 +113,16 @@ struct WalkArgs {
no_ignore_vcs: bool,
}

impl Args {
pub fn to_walk_builder(&self, path: &Path) -> WalkBuilder {
let mut walk = ignore::WalkBuilder::new(path);
walk.skip_stdout(true)
.git_global(
!(self.walk.no_ignore_global || self.walk.no_ignore_vcs || self.walk.no_ignore),
)
.git_ignore(!self.walk.no_ignore_vcs || self.walk.no_ignore)
.git_exclude(!self.walk.no_ignore_vcs || self.walk.no_ignore)
.hidden(self.walk.hidden)
.parents(!(self.walk.no_ignore_parent || self.walk.no_ignore))
.ignore(!(self.walk.no_ignore_dot || self.walk.no_ignore));
if self.sort {
walk.sort_by_file_name(|a, b| a.cmp(b));
impl WalkArgs {
pub fn to_config(&self) -> config::Walk {
config::Walk {
extend_exclude: self.exclude.clone(),
ignore_hidden: Some(self.hidden),
ignore_files: Some(!self.no_ignore),
ignore_dot: Some(!self.no_ignore_dot),
ignore_vcs: Some(!self.no_ignore_vcs),
ignore_global: Some(!self.no_ignore_global),
ignore_parent: Some(!self.no_ignore_parent),
}

walk
}

pub fn to_walk(&self) -> impl Iterator<Item = DirEntry> + '_ {
self.path.iter().flat_map(|path| {
self.to_walk_builder(path)
.build()
.filter_map(Result::ok)
.filter(|entry| {
entry
.metadata()
.as_ref()
.map(Metadata::is_file)
.unwrap_or(false)
})
})
}

pub fn format(&self) -> Format {
self.format
}
}
Loading

0 comments on commit fde1cb2

Please sign in to comment.