1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![allow(dead_code)]

mod char_ext;
mod char_string;
mod document;
mod lexing;
mod linting;
pub mod parsers;
mod punctuation;
mod span;
mod spell;
mod token;

pub use char_string::{CharString, CharStringExt};
pub use document::Document;
pub use linting::{Lint, LintGroup, LintGroupConfig, LintKind, Linter, Suggestion};
pub use punctuation::{Punctuation, Quote};
pub use span::Span;
pub use spell::{Dictionary, FullDictionary, MergedDictionary};
pub use token::{FatToken, Token, TokenKind, TokenStringExt};

/// A utility function that removes overlapping lints in a vector,
/// keeping the more important ones.
///
/// Note: this function will change the ordering of the lints.
pub fn remove_overlaps(lints: &mut Vec<Lint>) {
    if lints.len() < 2 {
        return;
    }

    lints.sort_by_key(|l| l.span.start);

    let mut remove_indices = Vec::new();

    for i in 0..lints.len() - 1 {
        let cur = &lints[i];
        let next = &lints[i + 1];

        if cur.span.overlaps_with(next.span) {
            // Remember, lower priority means higher importance.
            if next.priority < cur.priority {
                remove_indices.push(i);
            } else {
                remove_indices.push(i + 1);
            }
        }
    }

    let mut index = 0;
    lints.retain(|_| {
        index += 1;
        !remove_indices.contains(&(index - 1))
    })
}