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 render_markdown;
20mod span;
21pub mod spell;
22mod sync;
23mod title_case;
24mod token;
25mod token_kind;
26mod token_string_ext;
27mod vec_ext;
28mod word_metadata;
29
30use render_markdown::render_markdown;
31use std::collections::VecDeque;
32
33pub use char_string::{CharString, CharStringExt};
34pub use currency::Currency;
35pub use document::Document;
36pub use fat_token::{FatStringToken, FatToken};
37pub use ignored_lints::{IgnoredLints, LintContext};
38use linting::Lint;
39pub use mask::{Mask, Masker};
40pub use number::{Number, OrdinalSuffix};
41pub use punctuation::{Punctuation, Quote};
42pub use span::Span;
43pub use spell::{Dictionary, FstDictionary, MergedDictionary, MutableDictionary, WordId};
44pub use sync::{LSend, Lrc};
45pub use title_case::{make_title_case, make_title_case_str};
46pub use token::Token;
47pub use token_kind::TokenKind;
48pub use token_string_ext::TokenStringExt;
49pub use vec_ext::VecExt;
50pub use word_metadata::{
51    AdverbData, ConjunctionData, Degree, Dialect, NounData, PronounData, Tense, VerbData,
52    WordMetadata,
53};
54
55/// Return harper-core version
56pub fn core_version() -> &'static str {
57    env!("CARGO_PKG_VERSION")
58}
59
60/// A utility function that removes overlapping lints in a vector,
61/// keeping the more important ones.
62///
63/// Note: this function will change the ordering of the lints.
64pub fn remove_overlaps(lints: &mut Vec<Lint>) {
65    if lints.len() < 2 {
66        return;
67    }
68
69    let mut remove_indices = VecDeque::new();
70    lints.sort_by_key(|l| (l.span.start, !0 - l.span.end));
71
72    let mut cur = 0;
73
74    for (i, lint) in lints.iter().enumerate() {
75        if lint.span.start < cur {
76            remove_indices.push_back(i);
77            continue;
78        }
79        cur = lint.span.end;
80    }
81
82    lints.remove_indices(remove_indices);
83}
84
85#[cfg(test)]
86mod tests {
87    use crate::{
88        Dialect, Document, FstDictionary,
89        linting::{LintGroup, Linter},
90        remove_overlaps,
91    };
92
93    #[test]
94    fn keeps_space_lint() {
95        let doc = Document::new_plain_english_curated("Ths  tet");
96
97        let mut linter = LintGroup::new_curated(FstDictionary::curated(), Dialect::American);
98
99        let mut lints = linter.lint(&doc);
100
101        dbg!(&lints);
102        remove_overlaps(&mut lints);
103        dbg!(&lints);
104
105        assert_eq!(lints.len(), 3);
106    }
107}