mirror of
https://github.com/crate-ci/typos.git
synced 2025-01-23 23:18:57 -05:00
commit
28cd50a414
5 changed files with 35 additions and 14 deletions
|
@ -20,9 +20,10 @@ impl BuiltIn {
|
|||
|
||||
pub fn correct_ident<'s>(
|
||||
&'s self,
|
||||
_ident: typos::tokens::Identifier<'_>,
|
||||
ident_token: typos::tokens::Identifier<'_>,
|
||||
) -> Option<Status<'s>> {
|
||||
None
|
||||
let ident = ident_token.token();
|
||||
self.correct_ident_with_dict(ident)
|
||||
}
|
||||
|
||||
pub fn correct_word<'s>(&'s self, word_token: typos::tokens::Word<'_>) -> Option<Status<'s>> {
|
||||
|
@ -32,7 +33,7 @@ impl BuiltIn {
|
|||
|
||||
let word = word_token.token();
|
||||
let word_case = unicase::UniCase::new(word);
|
||||
let mut corrections = if let Some(corrections) = self.correct_with_dict(word_case) {
|
||||
let mut corrections = if let Some(corrections) = self.correct_word_with_dict(word_case) {
|
||||
if corrections.is_empty() {
|
||||
Status::Invalid
|
||||
} else {
|
||||
|
@ -50,15 +51,32 @@ impl BuiltIn {
|
|||
|
||||
#[cfg(feature = "dict")]
|
||||
impl BuiltIn {
|
||||
fn correct_ident_with_dict<'s>(&self, ident: &str) -> Option<Status<'s>> {
|
||||
match ident {
|
||||
"O_WRONLY" => Some(Status::Valid),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Not using `Status` to avoid the allocations
|
||||
fn correct_with_dict(&self, word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> {
|
||||
fn correct_word_with_dict(
|
||||
&self,
|
||||
word: unicase::UniCase<&str>,
|
||||
) -> Option<&'static [&'static str]> {
|
||||
typos_dict::WORD_TRIE.find(&word).copied()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "dict"))]
|
||||
impl BuiltIn {
|
||||
fn correct_with_dict(&self, _word: unicase::UniCase<&str>) -> Option<&'static [&'static str]> {
|
||||
fn correct_ident_with_dict<'s>(&self, _ident: &str) -> Option<Status<'s>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn correct_word_with_dict(
|
||||
&self,
|
||||
_word: unicase::UniCase<&str>,
|
||||
) -> Option<&'static [&'static str]> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
import os
|
||||
|
||||
from numpy.typing import NDArray # should work
|
||||
|
||||
print(os.O_WRONLY) # should work
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
mod dict_codegen;
|
||||
mod word_codegen;
|
||||
|
||||
pub use crate::dict_codegen::*;
|
||||
pub use crate::word_codegen::WORD_TRIE;
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
const DICT: &[u8] = include_bytes!("../assets/words.csv");
|
||||
|
||||
#[test]
|
||||
fn codegen() {
|
||||
let mut content = vec![];
|
||||
generate(&mut content);
|
||||
const DICT: &[u8] = include_bytes!("../assets/words.csv");
|
||||
generate(&mut content, "WORD", DICT);
|
||||
|
||||
let content = String::from_utf8(content).unwrap();
|
||||
let content = codegenrs::rustfmt(&content, None).unwrap();
|
||||
snapbox::assert_eq_path("./src/dict_codegen.rs", content);
|
||||
snapbox::assert_eq_path("./src/word_codegen.rs", content);
|
||||
}
|
||||
|
||||
fn generate<W: std::io::Write>(file: &mut W) {
|
||||
fn generate<W: std::io::Write>(file: &mut W, prefix: &str, dict: &[u8]) {
|
||||
writeln!(
|
||||
file,
|
||||
"// This file is @generated by {}",
|
||||
|
@ -23,13 +22,13 @@ fn generate<W: std::io::Write>(file: &mut W) {
|
|||
let records: Vec<_> = csv::ReaderBuilder::new()
|
||||
.has_headers(false)
|
||||
.flexible(true)
|
||||
.from_reader(DICT)
|
||||
.from_reader(dict)
|
||||
.records()
|
||||
.map(|r| r.unwrap())
|
||||
.collect();
|
||||
dictgen::generate_trie(
|
||||
file,
|
||||
"WORD",
|
||||
prefix,
|
||||
"&'static [&'static str]",
|
||||
records.iter().map(|record| {
|
||||
let mut record_fields = record.iter();
|
||||
|
|
Loading…
Add table
Reference in a new issue