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;
9pub mod expr;
10mod fat_token;
11mod ignored_lints;
12pub mod language_detection;
13mod lexing;
14pub mod linting;
15mod mask;
16mod number;
17pub mod parsers;
18pub mod patterns;
19mod punctuation;
20mod render_markdown;
21mod span;
22pub mod spell;
23mod sync;
24mod title_case;
25mod token;
26mod token_kind;
27mod token_string_ext;
28mod vec_ext;
29mod word_metadata;
30
31use render_markdown::render_markdown;
32use std::collections::VecDeque;
33
34pub use char_string::{CharString, CharStringExt};
35pub use currency::Currency;
36pub use document::Document;
37pub use fat_token::{FatStringToken, FatToken};
38pub use ignored_lints::{IgnoredLints, LintContext};
39use linting::Lint;
40pub use mask::{Mask, Masker};
41pub use number::{Number, OrdinalSuffix};
42pub use punctuation::{Punctuation, Quote};
43pub use span::Span;
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, DeterminerData, Dialect, NounData, PronounData, VerbData,
52    VerbForm, 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::spell::FstDictionary;
88    use crate::{
89        Dialect, Document,
90        linting::{LintGroup, Linter},
91        remove_overlaps,
92    };
93
94    #[test]
95    fn keeps_space_lint() {
96        let doc = Document::new_plain_english_curated("Ths  tet");
97
98        let mut linter = LintGroup::new_curated(FstDictionary::curated(), Dialect::American);
99
100        let mut lints = linter.lint(&doc);
101
102        dbg!(&lints);
103        remove_overlaps(&mut lints);
104        dbg!(&lints);
105
106        assert_eq!(lints.len(), 3);
107    }
108}