Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(core): make French spaces configurable (#1693)

Elijah Potter0585910f4582
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 2cf336ce3..6c9b741b0 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -68,6 +68,7 @@ use super::months::Months;
use super::most_number::MostNumber;
use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
use super::nail_on_the_head::NailOnTheHead;
+use super::no_french_spaces::NoFrenchSpaces;
use super::no_match_for::NoMatchFor;
use super::nobody::Nobody;
use super::nominal_wants::NominalWants;
@@ -409,6 +410,7 @@ impl LintGroup {
insert_expr_rule!(BoringWords, false);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_expr_rule!(ChockFull, true);
+ insert_struct_rule!(NoFrenchSpaces, true);
insert_struct_rule!(CommaFixes, true);
insert_struct_rule!(CompoundNouns, true);
insert_expr_rule!(Confident, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index ef6c858a8..b895f6edb 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -74,6 +74,7 @@ mod months;
mod most_number;
mod multiple_sequential_pronouns;
mod nail_on_the_head;
+mod no_french_spaces;
mod no_match_for;
mod no_oxford_comma;
mod nobody;
@@ -195,6 +196,7 @@ pub use months::Months;
pub use most_number::MostNumber;
pub use multiple_sequential_pronouns::MultipleSequentialPronouns;
pub use nail_on_the_head::NailOnTheHead;
+pub use no_french_spaces::NoFrenchSpaces;
pub use no_match_for::NoMatchFor;
pub use no_oxford_comma::NoOxfordComma;
pub use nobody::Nobody;
diff --git a/harper-core/src/linting/no_french_spaces.rs b/harper-core/src/linting/no_french_spaces.rs
new file mode 100644
index 000000000..1822fa3e7
--- /dev/null
+++ b/harper-core/src/linting/no_french_spaces.rs
@@ -0,0 +1,50 @@
+use super::{Lint, LintKind, Linter, Suggestion};
+use crate::Document;
+use crate::TokenStringExt;
+
+#[derive(Debug, Default)]
+pub struct NoFrenchSpaces;
+
+impl Linter for NoFrenchSpaces {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ let mut output = Vec::new();
+
+ for sentence in document.iter_sentences() {
+ if let Some(space_idx) = sentence.iter_space_indices().next() {
+ let space = &sentence[space_idx];
+
+ if space_idx == 0 && space.span.len() != 1 {
+ output.push(Lint {
+ span: space.span,
+ lint_kind: LintKind::Formatting,
+ suggestions: vec![Suggestion::ReplaceWith(vec![' '])],
+ message: "French spaces are generally not recommended.".to_owned(),
+ priority: 15,
+ })
+ }
+ }
+ }
+
+ output
+ }
+
+ fn description(&self) -> &str {
+ "Stops users from accidentally inserting French spaces."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::assert_suggestion_result;
+
+ use super::NoFrenchSpaces;
+
+ #[test]
+ fn fixes_basic() {
+ assert_suggestion_result(
+ "This is a short sentence. This is another short sentence.",
+ NoFrenchSpaces::default(),
+ "This is a short sentence. This is another short sentence.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/spaces.rs b/harper-core/src/linting/spaces.rs
index 642e06653..34082b479 100644
--- a/harper-core/src/linting/spaces.rs
+++ b/harper-core/src/linting/spaces.rs
@@ -10,7 +10,13 @@ impl Linter for Spaces {
let mut output = Vec::new();
for sentence in document.iter_sentences() {
- for space in sentence.iter_spaces() {
+ for space_idx in sentence.iter_space_indices() {
+ if space_idx == 0 {
+ continue;
+ }
+
+ let space = &sentence[space_idx];
+
let TokenKind::Space(count) = space.kind else {
panic!("The space iterator should only return spaces.")
};
@@ -69,7 +75,7 @@ impl Linter for Spaces {
#[cfg(test)]
mod tests {
use super::Spaces;
- use crate::linting::tests::assert_lint_count;
+ use crate::linting::tests::{assert_lint_count, assert_no_lints};
#[test]
fn detects_space_before_period() {
@@ -84,4 +90,12 @@ mod tests {
assert_lint_count(source, Spaces, 0)
}
+
+ #[test]
+ fn ignores_french_spacing() {
+ assert_no_lints(
+ "This is a short sentence. This is another short sentence.",
+ Spaces,
+ );
+ }
}