harper_core/linting/
no_french_spaces.rs

1use super::{Lint, LintKind, Linter, Suggestion};
2use crate::TokenStringExt;
3use crate::{Document, TokenKind};
4
5#[derive(Debug, Default)]
6pub struct NoFrenchSpaces;
7
8impl Linter for NoFrenchSpaces {
9    fn lint(&mut self, document: &Document) -> Vec<Lint> {
10        let mut output = Vec::new();
11
12        for sentence in document.iter_sentences() {
13            if let Some(space_idx) = sentence.iter_space_indices().next() {
14                let space = &sentence[space_idx];
15
16                if matches!(space.kind, TokenKind::Space(0)) {
17                    continue;
18                }
19                if space_idx == 0 && space.span.len() != 1 {
20                    output.push(Lint {
21                        span: space.span,
22                        lint_kind: LintKind::Formatting,
23                        suggestions: vec![Suggestion::ReplaceWith(vec![' '])],
24                        message: "French spaces are generally not recommended.".to_owned(),
25                        priority: 15,
26                    })
27                }
28            }
29        }
30
31        output
32    }
33
34    fn description(&self) -> &str {
35        "Stops users from accidentally inserting French spaces."
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::linting::tests::assert_suggestion_result;
42
43    use super::NoFrenchSpaces;
44
45    #[test]
46    fn fixes_basic() {
47        assert_suggestion_result(
48            "This is a short sentence.  This is another short sentence.",
49            NoFrenchSpaces::default(),
50            "This is a short sentence. This is another short sentence.",
51        );
52    }
53}