-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
173 additions
and
127 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "typos-dict" | ||
version = "0.1.0" | ||
authors = ["Ed Page <[email protected]>"] | ||
description = "Source Code Spelling Correction" | ||
repository = "https://github.com/epage/typos" | ||
documentation = "https://docs.rs/typos-dict" | ||
readme = "README.md" | ||
categories = ["development-tools", "text-processing"] | ||
keywords = ["development", "spelling"] | ||
license = "MIT" | ||
edition = "2018" | ||
|
||
[badges] | ||
travis-ci = { repository = "epage/typos" } | ||
appveyor = { repository = "epage/typos" } | ||
|
||
[features] | ||
# Support quickly iterating | ||
iterate_unstable = [] | ||
|
||
[dependencies] | ||
typos = { version = "0.1", path = "../typos" } | ||
phf = { version = "0.7", features = ["unicase"] } | ||
unicase = "1.1" | ||
log = "0.4" | ||
|
||
[build-dependencies] | ||
phf_codegen = "0.7" | ||
csv = "1.0" | ||
unicase = "1.1" |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::borrow::Cow; | ||
|
||
use unicase::UniCase; | ||
|
||
use typos::tokens::Case; | ||
|
||
#[derive(Default)] | ||
pub struct BuiltIn {} | ||
|
||
impl BuiltIn { | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
pub fn correct_ident<'s, 'w>( | ||
&'s self, | ||
_ident: typos::tokens::Identifier<'w>, | ||
) -> Option<Cow<'s, str>> { | ||
None | ||
} | ||
|
||
pub fn correct_word<'s, 'w>(&'s self, word: typos::tokens::Word<'w>) -> Option<Cow<'s, str>> { | ||
map_lookup(&crate::dict_codegen::WORD_DICTIONARY, word.token()) | ||
.map(|s| case_correct(s, word.case())) | ||
} | ||
} | ||
|
||
impl typos::Dictionary for BuiltIn { | ||
fn correct_ident<'s, 'w>( | ||
&'s self, | ||
ident: typos::tokens::Identifier<'w>, | ||
) -> Option<Cow<'s, str>> { | ||
BuiltIn::correct_ident(self, ident) | ||
} | ||
|
||
fn correct_word<'s, 'w>(&'s self, word: typos::tokens::Word<'w>) -> Option<Cow<'s, str>> { | ||
BuiltIn::correct_word(self, word) | ||
} | ||
} | ||
|
||
fn map_lookup( | ||
map: &'static phf::Map<UniCase<&'static str>, &'static str>, | ||
key: &str, | ||
) -> Option<&'static str> { | ||
// This transmute should be safe as `get` will not store the reference with | ||
// the expanded lifetime. This is due to `Borrow` being overly strict and | ||
// can't have an impl for `&'static str` to `Borrow<&'a str>`. | ||
// | ||
// | ||
// See https://github.com/rust-lang/rust/issues/28853#issuecomment-158735548 | ||
unsafe { | ||
let key = ::std::mem::transmute::<_, &'static str>(key); | ||
map.get(&UniCase(key)).cloned() | ||
} | ||
} | ||
|
||
fn case_correct(correction: &str, case: Case) -> Cow<'_, str> { | ||
match case { | ||
Case::Lower | Case::None => correction.into(), | ||
Case::Title => { | ||
let mut title = String::with_capacity(correction.as_bytes().len()); | ||
let mut char_indices = correction.char_indices(); | ||
if let Some((_, c)) = char_indices.next() { | ||
title.extend(c.to_uppercase()); | ||
if let Some((i, _)) = char_indices.next() { | ||
title.push_str(&correction[i..]); | ||
} | ||
} | ||
title.into() | ||
} | ||
Case::Scream => correction | ||
.chars() | ||
.flat_map(|c| c.to_uppercase()) | ||
.collect::<String>() | ||
.into(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_case_correct() { | ||
let cases = [ | ||
("foo", Case::Lower, "foo"), | ||
("foo", Case::None, "foo"), | ||
("foo", Case::Title, "Foo"), | ||
("foo", Case::Scream, "FOO"), | ||
("fOo", Case::None, "fOo"), | ||
]; | ||
for (correction, case, expected) in cases.iter() { | ||
let actual = case_correct(correction, *case); | ||
assert_eq!(*expected, actual); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod dict; | ||
mod dict_codegen; | ||
|
||
pub use crate::dict::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.