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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::{Dictionary, Document, Lint};

use super::{spaces::Spaces, Linter};
use paste::paste;

use super::{
    long_sentences::LongSentences, matcher::Matcher, repeated_words::RepeatedWords,
    sentence_capitalization::SentenceCapitalization, spell_check::SpellCheck,
    unclosed_quotes::UnclosedQuotes, wrong_quotes::WrongQuotes,
};

pub struct LintSet<'a> {
    pub(super) linters: Vec<Box<dyn Linter + 'a>>,
}

impl<'a> Linter for LintSet<'a> {
    fn lint(&mut self, document: &Document) -> Vec<Lint> {
        let mut lints = Vec::new();

        for linter in &mut self.linters {
            lints.append(&mut linter.lint(document));
        }

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

        lints
    }
}

impl<'a> LintSet<'a> {
    pub fn new() -> Self {
        Self {
            linters: Vec::new(),
        }
    }

    pub fn add_standard(&mut self, dictionary: impl Dictionary + 'a) -> &mut Self {
        self.add_repeated_words()
            .add_long_sentences()
            .add_unclosed_quotes()
            .add_sentence_capitalization()
            .add_matcher()
            .add_spell_check(dictionary)
            .add_spaces();
        self
    }

    pub fn with_standard(mut self, dictionary: impl Dictionary + 'a) -> Self {
        self.add_standard(dictionary);
        self
    }

    pub fn add_spell_check(&mut self, dictionary: impl Dictionary + 'a) -> &mut Self {
        self.linters.push(Box::new(SpellCheck::new(dictionary)));
        self
    }

    pub fn with_spell_check(mut self, dictionary: impl Dictionary + 'a) -> Self {
        self.add_spell_check(dictionary);
        self
    }
}

impl Default for LintSet<'_> {
    fn default() -> Self {
        Self::new()
    }
}

/// Create builder methods for the linters that do not take any arguments.
macro_rules! create_simple_builder_methods {
    ($($linter:ident),*) => {
        impl LintSet<'_> {
            paste! {
                $(
                    #[doc = "Modifies self, adding the `" $linter "` linter to the set."]
                    pub fn [<add_$linter:snake>](&mut self) -> &mut Self{
                        self.linters.push(Box::<$linter>::default());
                        self
                    }
                )*
            }

            paste! {
                $(
                    #[doc = "Consumes self, adding the `" $linter "` linter to the set."]
                    pub fn [<with_$linter:snake>](mut self) -> Self{
                        self.[<add_$linter:snake>]();
                        self
                    }
                )*
            }
        }
    };
}

create_simple_builder_methods!(
    SentenceCapitalization,
    UnclosedQuotes,
    WrongQuotes,
    LongSentences,
    RepeatedWords,
    Spaces,
    Matcher
);