Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: work towards #212 for asian commans
hippietraild4c5f9cae9e1
diff --git a/harper-core/src/linting/comma_fixes.rs b/harper-core/src/linting/comma_fixes.rsnew file mode 100644index 000000000..3c1655152--- /dev/null+++ b/harper-core/src/linting/comma_fixes.rs@@ -0,0 +1,72 @@+use itertools::Itertools;++use super::{Lint, LintKind, Linter, Suggestion};+use crate::TokenStringExt;++/// A linter that fixes common comma errors such as no space after, erroneous+/// space before, etc, Asian commas instead of English commas, etc.+#[derive(Debug, Default)]+pub struct CommaFixes;++impl Linter for CommaFixes {+ fn lint(&mut self, document: &crate::Document) -> Vec<Lint> {+ let mut lints: Vec<Lint> = Vec::new();++ for tok in document.iter_commas() {+ let tok_content = document.get_span_content(tok.span);++ if tok_content.is_empty() || tok_content.first().cloned() == Some(',') {+ continue;+ }++ lints.push(Lint {+ span: tok.span,+ lint_kind: LintKind::Formatting,+ suggestions: vec![Suggestion::ReplaceWith(vec![','])],+ message: "Avoid East Asian commas in English contexts.".to_string(),+ priority: 32,+ })+ }++ lints+ }++ fn description(&self) -> &'static str {+ "Fix common comma errors such as no space after, erroneous space before, etc, Asian commas instead of English commas, etc."+ }+}++#[cfg(test)]+mod tests {+ use super::CommaFixes;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn allows_english_comma_atomic() {+ assert_lint_count(",", CommaFixes, 0);+ }++ #[test]+ fn flags_fullwidth_comma_atomic() {+ assert_lint_count(",", CommaFixes, 1);+ }++ #[test]+ fn flags_ideographic_comma_atomic() {+ assert_lint_count("、", CommaFixes, 1);+ }++ #[test]+ fn corrects_fullwidth_comma_real_world() {+ assert_suggestion_result(+ "higher 2 bits of the number of nodes, whether abandoned or not decided by .index section",+ CommaFixes,+ "higher 2 bits of the number of nodes, whether abandoned or not decided by .index section",+ );+ }++ #[test]+ fn corrects_ideographic_comma_real_world() {+ assert_suggestion_result("cout、endl、string", CommaFixes, "cout,endl,string")+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 9eeba6f89..793802edc 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -17,6 +17,7 @@ use super::back_in_the_day::BackInTheDay;use super::boring_words::BoringWords;use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;use super::chock_full::ChockFull;+use super::comma_fixes::CommaFixes;use super::compound_nouns::CompoundNouns;use super::confident::Confident;use super::correct_number_suffix::CorrectNumberSuffix;@@ -321,6 +322,7 @@ impl LintGroup {insert_struct_rule!(AvoidCurses, true);insert_pattern_rule!(TerminatingConjunctions, true);insert_struct_rule!(EllipsisLength, true);+ insert_struct_rule!(CommaFixes, true);insert_pattern_rule!(DotInitialisms, true);insert_pattern_rule!(BoringWords, false);insert_pattern_rule!(UseGenitive, false);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 5a3728412..8e9ce8070 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -9,6 +9,7 @@ mod boring_words;mod capitalize_personal_pronouns;mod chock_full;mod closed_compounds;+mod comma_fixes;mod compound_nouns;mod confident;mod correct_number_suffix;@@ -72,6 +73,7 @@ pub use back_in_the_day::BackInTheDay;pub use boring_words::BoringWords;pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;pub use chock_full::ChockFull;+pub use comma_fixes::CommaFixes;pub use compound_nouns::CompoundNouns;pub use confident::Confident;pub use correct_number_suffix::CorrectNumberSuffix;