From 86c4c9fb4a37cfeb121e4a1e98dcdb6b4b34e68e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Dec 2023 08:34:49 -0600 Subject: [PATCH 1/2] test(cli): Add reproduction case --- .../tests/cmd/extend-words-case.in/_typos.toml | 2 ++ .../tests/cmd/extend-words-case.in/file.txt | 1 + crates/typos-cli/tests/cmd/extend-words-case.toml | 13 +++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 crates/typos-cli/tests/cmd/extend-words-case.in/_typos.toml create mode 100644 crates/typos-cli/tests/cmd/extend-words-case.in/file.txt create mode 100644 crates/typos-cli/tests/cmd/extend-words-case.toml diff --git a/crates/typos-cli/tests/cmd/extend-words-case.in/_typos.toml b/crates/typos-cli/tests/cmd/extend-words-case.in/_typos.toml new file mode 100644 index 0000000..97a66db --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-words-case.in/_typos.toml @@ -0,0 +1,2 @@ +[default.extend-words] +"trailling" = "trailing" diff --git a/crates/typos-cli/tests/cmd/extend-words-case.in/file.txt b/crates/typos-cli/tests/cmd/extend-words-case.in/file.txt new file mode 100644 index 0000000..5a479ac --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-words-case.in/file.txt @@ -0,0 +1 @@ +public function noErrorOnTraillingSemicolonAndWhitespace(Connection $connection) diff --git a/crates/typos-cli/tests/cmd/extend-words-case.toml b/crates/typos-cli/tests/cmd/extend-words-case.toml new file mode 100644 index 0000000..cef3b95 --- /dev/null +++ b/crates/typos-cli/tests/cmd/extend-words-case.toml @@ -0,0 +1,13 @@ +bin.name = "typos" +args = "" +status.code = 2 +stdin = "" +stdout = """ +error: `Trailling` should be `trailing` + --> ./file.txt:1:26 + | +1 | public function noErrorOnTraillingSemicolonAndWhitespace(Connection $connection) + | ^^^^^^^^^ + | +""" +stderr = "" From 55d802d0efeff83d9bc4527ff1ca35d9d079eab2 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 13 Dec 2023 08:38:14 -0600 Subject: [PATCH 2/2] fix(dict): Mirror original case for custom dict Fixes #781 --- crates/typos-cli/src/dict.rs | 17 ++++++++++------- .../typos-cli/tests/cmd/extend-words-case.toml | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/typos-cli/src/dict.rs b/crates/typos-cli/src/dict.rs index 5adf584..25e3128 100644 --- a/crates/typos-cli/src/dict.rs +++ b/crates/typos-cli/src/dict.rs @@ -284,27 +284,30 @@ impl<'i, 'w, D: typos::Dictionary> typos::Dictionary for Override<'i, 'w, D> { self.inner.correct_ident(ident) } - fn correct_word<'s>(&'s self, word: typos::tokens::Word<'_>) -> Option> { - if word.case() == typos::tokens::Case::None { + fn correct_word<'s>(&'s self, word_token: typos::tokens::Word<'_>) -> Option> { + if word_token.case() == typos::tokens::Case::None { return None; } for ignored in &self.ignored_words { - if ignored.is_match(word.token()) { + if ignored.is_match(word_token.token()) { return Some(Status::Valid); } } // Skip hashing if we can if !self.words.is_empty() { - let w = UniCase::new(word.token()); + let w = UniCase::new(word_token.token()); // HACK: couldn't figure out the lifetime issue with replacing `cloned` with `borrow` - if let Some(status) = self.words.get(&w).cloned() { - return Some(status); + if let Some(mut corrections) = self.words.get(&w).cloned() { + for s in corrections.corrections_mut() { + case_correct(s, word_token.case()) + } + return Some(corrections); } } - self.inner.correct_word(word) + self.inner.correct_word(word_token) } } diff --git a/crates/typos-cli/tests/cmd/extend-words-case.toml b/crates/typos-cli/tests/cmd/extend-words-case.toml index cef3b95..1396158 100644 --- a/crates/typos-cli/tests/cmd/extend-words-case.toml +++ b/crates/typos-cli/tests/cmd/extend-words-case.toml @@ -3,7 +3,7 @@ args = "" status.code = 2 stdin = "" stdout = """ -error: `Trailling` should be `trailing` +error: `Trailling` should be `Trailing` --> ./file.txt:1:26 | 1 | public function noErrorOnTraillingSemicolonAndWhitespace(Connection $connection)