From fce92e4e5f3e943842549e93beb049446daf83df Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 21 Jun 2023 14:31:04 -0500 Subject: [PATCH] fix(config): User file types override default file types This also ensures `typos --type-list` will report the glob in only one place. Fixes #754 --- crates/typos-cli/src/file_type.rs | 30 ++++++++++++++----- .../tests/cmd/override-default-type.toml | 2 +- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/typos-cli/src/file_type.rs b/crates/typos-cli/src/file_type.rs index 4b044bc..be08999 100644 --- a/crates/typos-cli/src/file_type.rs +++ b/crates/typos-cli/src/file_type.rs @@ -4,7 +4,7 @@ use kstring::KString; #[derive(Default, Clone, Debug)] pub struct TypesBuilder { - definitions: BTreeMap>, + definitions: BTreeMap>, } impl TypesBuilder { @@ -18,7 +18,7 @@ impl TypesBuilder { .iter() .map(|(name, glob)| { let name = KString::from(*name); - let globs = glob.iter().map(|s| KString::from(*s)).collect(); + let globs = glob.iter().map(|s| (KString::from(*s), 0)).collect(); (name, globs) }), ); @@ -31,7 +31,11 @@ impl TypesBuilder { pub fn add(&mut self, name: impl Into, glob: impl Into) { let name = name.into(); let glob = glob.into(); - self.definitions.entry(name).or_default().push(glob); + let weight = self.definitions.len(); + self.definitions + .entry(name) + .or_default() + .push((glob, weight)); } pub fn build(self) -> Result { @@ -39,17 +43,29 @@ impl TypesBuilder { .definitions .iter() .flat_map(|(name, globs)| { - globs.iter().map(move |glob| { + globs.iter().map(move |(glob, weight)| { let sort = sort_key(glob); - (sort, name, glob) + (sort, weight, name, glob) }) }) .collect::>(); definitions.sort(); + let rev_definitions = definitions + .iter() + .map(|(_, _, name, glob)| (*glob, *name)) + .collect::>(); + let mut unique_definitions = BTreeMap::>::new(); + for (glob, name) in rev_definitions { + unique_definitions + .entry(name.clone()) + .or_default() + .push(glob.clone()); + } + let mut glob_to_name = Vec::new(); let mut build_set = globset::GlobSetBuilder::new(); - for (_, name, glob) in definitions { + for (_, _, name, glob) in definitions { glob_to_name.push(name.clone()); build_set.add( globset::GlobBuilder::new(glob) @@ -60,7 +76,7 @@ impl TypesBuilder { let set = build_set.build()?; Ok(Types { - definitions: self.definitions, + definitions: unique_definitions, glob_to_name, set, matches: std::sync::Arc::new(thread_local::ThreadLocal::default()), diff --git a/crates/typos-cli/tests/cmd/override-default-type.toml b/crates/typos-cli/tests/cmd/override-default-type.toml index 90ff659..64d4293 100644 --- a/crates/typos-cli/tests/cmd/override-default-type.toml +++ b/crates/typos-cli/tests/cmd/override-default-type.toml @@ -6,6 +6,6 @@ stdin = ''' Destory ''' stdout = """ -./key.asc:asciidoc +./key.asc:asc """ stderr = ""