Skip to content

Commit

Permalink
feat(config): Allow ignoring words by regex
Browse files Browse the repository at this point in the history
Fixes #852
  • Loading branch information
epage committed Oct 16, 2023
1 parent 35a8bc6 commit be8628f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
14 changes: 14 additions & 0 deletions crates/typos-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ pub struct DictConfig {
#[serde(with = "serde_regex")]
pub extend_ignore_identifiers_re: Vec<regex::Regex>,
pub extend_identifiers: HashMap<kstring::KString, kstring::KString>,
#[serde(with = "serde_regex")]
pub extend_ignore_words_re: Vec<regex::Regex>,
pub extend_words: HashMap<kstring::KString, kstring::KString>,
}

Expand All @@ -435,6 +437,7 @@ impl DictConfig {
locale: Some(empty.locale()),
extend_ignore_identifiers_re: Default::default(),
extend_identifiers: Default::default(),
extend_ignore_words_re: Default::default(),
extend_words: Default::default(),
}
}
Expand All @@ -451,6 +454,8 @@ impl DictConfig {
.iter()
.map(|(key, value)| (key.clone(), value.clone())),
);
self.extend_ignore_words_re
.extend(source.extend_ignore_words_re.iter().cloned());
self.extend_words.extend(
source
.extend_words
Expand All @@ -475,6 +480,10 @@ impl DictConfig {
)
}

pub fn extend_ignore_words_re(&self) -> Box<dyn Iterator<Item = &regex::Regex> + '_> {
Box::new(self.extend_ignore_words_re.iter())
}

pub fn extend_words(&self) -> Box<dyn Iterator<Item = (&str, &str)> + '_> {
Box::new(
self.extend_words
Expand Down Expand Up @@ -503,6 +512,11 @@ impl PartialEq for DictConfig {
.map(|r| r.as_str())
.eq(rhs.extend_ignore_identifiers_re.iter().map(|r| r.as_str()))
&& self.extend_identifiers == rhs.extend_identifiers
&& self
.extend_ignore_words_re
.iter()
.map(|r| r.as_str())
.eq(rhs.extend_ignore_words_re.iter().map(|r| r.as_str()))
&& self.extend_words == rhs.extend_words
}
}
Expand Down
25 changes: 19 additions & 6 deletions crates/typos-cli/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ fn case_correct(correction: &mut Cow<'_, str>, case: Case) {
pub struct Override<'i, 'w, D> {
ignored_identifiers: Vec<regex::Regex>,
identifiers: HashMap<&'i str, Status<'i>, ahash::RandomState>,
ignored_words: Vec<regex::Regex>,
words: HashMap<unicase::UniCase<&'w str>, Status<'w>, ahash::RandomState>,
inner: D,
}
Expand All @@ -225,6 +226,7 @@ impl<'i, 'w, D: typos::Dictionary> Override<'i, 'w, D> {
Self {
ignored_identifiers: Default::default(),
identifiers: Default::default(),
ignored_words: Default::default(),
words: Default::default(),
inner,
}
Expand All @@ -238,6 +240,10 @@ impl<'i, 'w, D: typos::Dictionary> Override<'i, 'w, D> {
self.identifiers = Self::interpret(identifiers).collect();
}

pub fn ignored_words<'r>(&mut self, ignored: impl Iterator<Item = &'r regex::Regex>) {
self.ignored_words.extend(ignored.cloned());
}

pub fn words<I: Iterator<Item = (&'w str, &'w str)>>(&mut self, words: I) {
self.words = Self::interpret(words)
.map(|(k, v)| (UniCase::new(k), v))
Expand Down Expand Up @@ -283,15 +289,22 @@ impl<'i, 'w, D: typos::Dictionary> typos::Dictionary for Override<'i, 'w, D> {
return None;
}

for ignored in &self.ignored_words {
if ignored.is_match(word.token()) {
return Some(Status::Valid);
}
}

// Skip hashing if we can
let custom = if !self.words.is_empty() {
if !self.words.is_empty() {
let w = UniCase::new(word.token());
// HACK: couldn't figure out the lifetime issue with replacing `cloned` with `borrow`
self.words.get(&w).cloned()
} else {
None
};
custom.or_else(|| self.inner.correct_word(word))
if let Some(status) = self.words.get(&w).cloned() {
return Some(status);
}
}

self.inner.correct_word(word)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/typos-cli/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl<'s> ConfigEngine<'s> {
.extend_identifiers()
.map(|(k, v)| (self.storage.get(k), self.storage.get(v))),
);
dict.ignored_words(dict_config.extend_ignore_words_re());
dict.words(
dict_config
.extend_words()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[default.extend-words]
hello = "goodbye"

[type.fail]
extend-glob = ["*.fail"]

[type.ignore]
extend-glob = ["*.ignore"]
extend-ignore-words-re = ["he.*"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
12 changes: 12 additions & 0 deletions crates/typos-cli/tests/cmd/extend-ignore-words-re.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bin.name = "typos"
status.code = 2
stdin = ""
stdout = """
error: `hello` should be `goodbye`
--> ./file.fail:1:1
|
1 | hello
| ^^^^^
|
"""
stderr = ""
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Configuration is read from the following (in precedence order)
| default.extend-identifiers | \- | table of strings | Corrections for [identifiers](./design.md#identifiers-and-words). When the correction is blank, the identifier is never valid. When the correction is the key, the identifier is always valid. |
| default.extend-ignore-identifiers-re | \- | list of [regexes](https://docs.rs/regex/latest/regex/index.html#syntax) | Pattern-match always-valid identifiers |
| default.extend-words | \- | table of strings | Corrections for [words](./design.md#identifiers-and-words). When the correction is blank, the word is never valid. When the correction is the key, the word is always valid. |
| default.extend-ignore-words-re | \- | list of [regexes](https://docs.rs/regex/latest/regex/index.html#syntax) | Pattern-match always-valid words. Note: you must handle case insensitivity yourself |
| type.\<name>.\<field> | \<varied> | \<varied> | See `default.` for child keys. Run with `--type-list` to see available `<name>`s |
| type.\<name>.extend-glob | \- | list of strings | File globs for matching `<name>` |

Expand Down

0 comments on commit be8628f

Please sign in to comment.