harper_core/
lib.rs

1#![doc = include_str!("../README.md")]
2#![allow(dead_code)]
3
4mod char_ext;
5mod char_string;
6mod currency;
7mod document;
8mod edit_distance;
9mod fat_token;
10mod ignored_lints;
11pub mod language_detection;
12mod lexing;
13pub mod linting;
14mod mask;
15mod number;
16pub mod parsers;
17pub mod patterns;
18mod punctuation;
19mod span;
20pub mod spell;
21mod sync;
22mod title_case;
23mod token;
24mod token_kind;
25mod token_string_ext;
26mod vec_ext;
27mod word_metadata;
28
29use std::collections::VecDeque;
30
31pub use char_string::{CharString, CharStringExt};
32pub use currency::Currency;
33pub use document::Document;
34pub use fat_token::FatToken;
35pub use ignored_lints::IgnoredLints;
36use linting::Lint;
37pub use mask::{Mask, Masker};
38pub use number::{Number, NumberSuffix};
39pub use punctuation::{Punctuation, Quote};
40pub use span::Span;
41pub use spell::{Dictionary, FstDictionary, MergedDictionary, MutableDictionary};
42pub use sync::Lrc;
43pub use title_case::{make_title_case, make_title_case_str};
44pub use token::Token;
45pub use token_kind::TokenKind;
46pub use token_string_ext::TokenStringExt;
47pub use vec_ext::VecExt;
48pub use word_metadata::{AdverbData, ConjunctionData, NounData, Tense, VerbData, WordMetadata};
49
50/// A utility function that removes overlapping lints in a vector,
51/// keeping the more important ones.
52///
53/// Note: this function will change the ordering of the lints.
54pub fn remove_overlaps(lints: &mut Vec<Lint>) {
55    if lints.len() < 2 {
56        return;
57    }
58
59    let mut remove_indices = VecDeque::new();
60    lints.sort_by_key(|l| (l.span.start, !0 - l.span.end));
61
62    let mut cur = 0;
63
64    for (i, lint) in lints.iter().enumerate() {
65        if lint.span.start < cur {
66            remove_indices.push_back(i);
67            continue;
68        }
69        cur = lint.span.end;
70    }
71
72    lints.remove_indices(remove_indices);
73}
74
75#[cfg(test)]
76mod tests {
77    use crate::{
78        Document, FstDictionary,
79        linting::{LintGroup, Linter},
80        remove_overlaps,
81    };
82
83    #[test]
84    fn keeps_space_lint() {
85        let doc = Document::new_plain_english_curated("Ths  tet");
86
87        let mut linter = LintGroup::new_curated(FstDictionary::curated());
88
89        let mut lints = linter.lint(&doc);
90
91        dbg!(&lints);
92        remove_overlaps(&mut lints);
93        dbg!(&lints);
94
95        assert_eq!(lints.len(), 3);
96    }
97}