feat(config): Show available type definitions

This commit is contained in:
Ed Page 2021-04-05 21:15:41 -05:00
parent a101df95c2
commit 8f365ee155
4 changed files with 64 additions and 2 deletions

View file

@ -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.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-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. | | 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.<name>.binary | <varied> | <varied> | See `default.` for child keys. | | type.<name>.binary | <varied> | <varied> | See `default.` for child keys. Run with `--type-list` to see available `<name>`s |

View file

@ -79,6 +79,10 @@ pub(crate) struct Args {
/// Write the current configuration to file with `-` for stdout /// Write the current configuration to file with `-` for stdout
pub(crate) dump_config: Option<std::path::PathBuf>, pub(crate) dump_config: Option<std::path::PathBuf>,
#[structopt(long, group = "mode")]
/// Show all supported file types.
pub(crate) type_list: bool,
#[structopt( #[structopt(
long, long,
possible_values(&Format::variants()), possible_values(&Format::variants()),

View file

@ -35,6 +35,8 @@ fn run() -> proc_exit::ExitResult {
if let Some(output_path) = args.dump_config.as_ref() { if let Some(output_path) = args.dump_config.as_ref() {
run_dump_config(&args, output_path) run_dump_config(&args, output_path)
} else if args.type_list {
run_type_list(&args)
} else { } else {
run_checks(&args) run_checks(&args)
} }
@ -85,6 +87,54 @@ fn run_dump_config(args: &args::Args, output_path: &std::path::Path) -> proc_exi
Ok(()) 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 { fn run_checks(args: &args::Args) -> proc_exit::ExitResult {
let global_cwd = std::env::current_dir()?; let global_cwd = std::env::current_dir()?;

View file

@ -67,7 +67,7 @@ impl<'s> ConfigEngine<'s> {
self 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 let dir = self
.configs .configs
.get(cwd) .get(cwd)
@ -75,6 +75,14 @@ impl<'s> ConfigEngine<'s> {
self.get_walk(dir) 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<'_, '_> { pub fn policy(&self, path: &std::path::Path) -> Policy<'_, '_> {
let dir = self.get_dir(path).expect("`walk()` should be called first"); let dir = self.get_dir(path).expect("`walk()` should be called first");
let file_config = dir.get_file_config(path); let file_config = dir.get_file_config(path);