Skip to content

Commit

Permalink
feat(config): Show available type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Apr 6, 2021
1 parent a101df9 commit 8f365ee
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<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 |
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ pub(crate) struct Args {
/// Write the current configuration to file with `-` for stdout
pub(crate) dump_config: Option<std::path::PathBuf>,

#[structopt(long, group = "mode")]
/// Show all supported file types.
pub(crate) type_list: bool,

#[structopt(
long,
possible_values(&Format::variants()),
Expand Down
50 changes: 50 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()?;

Expand Down
10 changes: 9 additions & 1 deletion src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ 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)
.expect("`init_dir` must be called first");
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);
Expand Down

0 comments on commit 8f365ee

Please sign in to comment.