Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Dont correct O_WRONLY #833

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions crates/typos-cli/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/typos-cli/tests/cmd/false-positives.in/sample.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import os

from numpy.typing import NDArray # should work

print(os.O_WRONLY) # should work
4 changes: 2 additions & 2 deletions crates/typos-dict/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod dict_codegen;
mod word_codegen;

pub use crate::dict_codegen::*;
pub use crate::word_codegen::WORD_TRIE;
13 changes: 6 additions & 7 deletions crates/typos-dict/tests/codegen.rs
Original file line number Diff line number Diff line change
@@ -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 {}",
Expand All @@ -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();
Expand Down