diff --git a/docs/reference.md b/docs/reference.md index 9ebf8c2..0cfabf5 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -31,4 +31,4 @@ Configuration is read from the following (in precedence order) | default.locale | --locale | en, en-us, en-gb, en-ca, en-au | English dialect to correct to. | | default.extend-identifiers | \- | table of strings | Corrections for identifiers. When the correction is blank, the word is never valid. When the correction is the key, the word is always valid. | | default.extend-words | \- | table of strings | Corrections for identifiers. When the correction is blank, the word is never valid. When the correction is the key, the word is always valid. | -| type..binary | | | See `default.` for child keys. | +| type..binary | | | See `default.` for child keys. Run with `--type-list` to see available ``s | diff --git a/src/args.rs b/src/args.rs index 1dd4863..dcec8dc 100644 --- a/src/args.rs +++ b/src/args.rs @@ -79,6 +79,10 @@ pub(crate) struct Args { /// Write the current configuration to file with `-` for stdout pub(crate) dump_config: Option, + #[structopt(long, group = "mode")] + /// Show all supported file types. + pub(crate) type_list: bool, + #[structopt( long, possible_values(&Format::variants()), diff --git a/src/main.rs b/src/main.rs index 4b9c00b..0d9139d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,8 @@ fn run() -> proc_exit::ExitResult { if let Some(output_path) = args.dump_config.as_ref() { run_dump_config(&args, output_path) + } else if args.type_list { + run_type_list(&args) } else { run_checks(&args) } @@ -85,6 +87,54 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi Ok(()) } +fn run_type_list(args: &args::Args) -> proc_exit::ExitResult { + let global_cwd = std::env::current_dir()?; + + let path = &args.path[0]; + let path = if path == std::path::Path::new("-") { + path.to_owned() + } else { + path.canonicalize().with_code(proc_exit::Code::USAGE_ERR)? + }; + let cwd = if path == std::path::Path::new("-") { + global_cwd.as_path() + } else if path.is_file() { + path.parent().unwrap() + } else { + path.as_path() + }; + + let storage = typos_cli::policy::ConfigStorage::new(); + let mut engine = typos_cli::policy::ConfigEngine::new(&storage); + engine.set_isolated(args.isolated); + + let mut overrides = config::Config::default(); + if let Some(path) = args.custom_config.as_ref() { + let custom = config::Config::from_file(path).with_code(proc_exit::Code::CONFIG_ERR)?; + overrides.update(&custom); + } + overrides.update(&args.config.to_config()); + engine.set_overrides(overrides); + + engine + .init_dir(cwd) + .with_code(proc_exit::Code::CONFIG_ERR)?; + let definitions = engine.file_types(cwd); + + let stdout = std::io::stdout(); + let mut handle = stdout.lock(); + for def in definitions { + writeln!( + handle, + "{}: {}", + def.name(), + itertools::join(def.globs(), ", ") + )?; + } + + Ok(()) +} + fn run_checks(args: &args::Args) -> proc_exit::ExitResult { let global_cwd = std::env::current_dir()?; diff --git a/src/policy.rs b/src/policy.rs index cbb474d..bb0d896 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -67,7 +67,7 @@ impl<'s> ConfigEngine<'s> { self } - pub fn walk(&mut self, cwd: &std::path::Path) -> &crate::config::Walk { + pub fn walk(&self, cwd: &std::path::Path) -> &crate::config::Walk { let dir = self .configs .get(cwd) @@ -75,6 +75,14 @@ impl<'s> ConfigEngine<'s> { self.get_walk(dir) } + pub fn file_types(&self, cwd: &std::path::Path) -> &[ignore::types::FileTypeDef] { + let dir = self + .configs + .get(cwd) + .expect("`init_dir` must be called first"); + dir.type_matcher.definitions() + } + pub fn policy(&self, path: &std::path::Path) -> Policy<'_, '_> { let dir = self.get_dir(path).expect("`walk()` should be called first"); let file_config = dir.get_file_config(path);