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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use std::path::PathBuf;

use anyhow::{bail, Context, Result};
use clap::Parser;
use colored::*;

use skyspell_core::Checker;
use skyspell_core::Dictionary;
use skyspell_core::EnchantDictionary;
use skyspell_core::IgnoreConfig;
use skyspell_core::SkipFile;
use skyspell_core::StorageBackend;
use skyspell_core::TokenProcessor;
use skyspell_core::{get_default_db_path, SQLRepository};
use skyspell_core::{Project, ProjectPath, SKYSPELL_IGNORE_FILE};

mod checkers;
pub mod interactor;
pub use checkers::{InteractiveChecker, NonInteractiveChecker};
pub use interactor::{ConsoleInteractor, Interactor};

#[macro_export]
macro_rules! info_1 {
    ($($arg:tt)*) => ({
        println!("{} {}", "::".bold().blue(), format!($($arg)*));
    })
}

#[macro_export]
macro_rules! info_2 {
    ($($arg:tt)*) => ({
        println!("{} {}", "=>".bold().blue(), format!($($arg)*));
    })
}

#[macro_export]
macro_rules! info_3 {
    ($($arg:tt)*) => ({
        println!("{} {}", "*".bold().blue(), format!($($arg)*));
    })
}

#[macro_export]
macro_rules! print_error {
    ($($arg:tt)*) => ({
    eprintln!("{} {}", "Error:".red(), format!($($arg)*));
    })
}

#[derive(Debug, PartialEq, Eq, clap::ValueEnum, Clone, Copy)]
#[derive(Default)]
pub enum OutputFormat {
    #[default]
    Text,
    Json,
}



impl OutputFormat {
    fn is_text(&self) -> bool {
        matches!(self, OutputFormat::Text)
    }
}

#[derive(Parser)]
#[clap(version)]
pub struct Opts {
    #[clap(long, help = "Language to use")]
    pub lang: Option<String>,

    #[clap(long, help = "Path of the ignore repository")]
    pub db_path: Option<String>,

    #[clap(long, help = "Project path")]
    project_path: Option<PathBuf>,

    #[clap(long, value_enum, short = 'o', help = "Output format")]
    output_format: Option<OutputFormat>,

    #[clap(subcommand)]
    action: Action,
}

impl Opts {
    fn text_output(&self) -> bool {
        self.output_format.unwrap_or_default() == OutputFormat::Text
    }
}

#[derive(Parser)]
enum Action {
    #[clap(about = "Add word to one of the ignore lists")]
    Add(AddOpts),
    #[clap(about = "Remove word from one of the ignore lists")]
    Remove(RemoveOpts),
    #[clap(about = "Check files for spelling errors")]
    Check(CheckOpts),
    #[clap(about = "Clean repository")]
    Clean,
    #[clap(about = "Suggest replacements for the given error")]
    Suggest(SuggestOpts),
    #[clap(about = "Undo last operation")]
    Undo,
}

#[derive(Parser)]
struct AddOpts {
    #[clap(help = "The word to add")]
    word: String,

    #[clap(long, help = "Add word to the ignore list for the current project")]
    project: bool,

    #[clap(long, help = "Add word to the ignore list for the given extension")]
    extension: Option<String>,

    #[clap(long, help = "Add word to the ignore list for the given path")]
    relative_path: Option<PathBuf>,
}

#[derive(Parser)]
struct CheckOpts {
    #[clap(
        long,
        help = "Don't ask what to do for each unknown word, instead just print the whole list - useful for continuous integration and other scripts"
    )]
    non_interactive: bool,

    #[clap(help = "List of paths to check")]
    paths: Vec<PathBuf>,
}

#[derive(Parser)]
struct SuggestOpts {
    word: String,
}

#[derive(Parser)]
struct RemoveOpts {
    #[clap(help = "The word to remove")]
    word: String,

    #[clap(
        long,
        help = "Remove word from the ignore list for the current project"
    )]
    project: bool,

    #[clap(
        long,
        help = "Remove word from the ignore list for the given extension"
    )]
    extension: Option<String>,

    #[clap(long, help = "Remove word from the ignore list for the given path")]
    relative_path: Option<PathBuf>,
}

fn add(project: Project, mut storage_backend: StorageBackend, opts: &AddOpts) -> Result<()> {
    let word = &opts.word;
    match (&opts.relative_path, &opts.extension, &opts.project) {
        (None, None, false) => storage_backend.ignore(word),
        (None, Some(e), _) => storage_backend
            .ignore_store_mut()
            .ignore_for_extension(word, e),
        (Some(relative_path), None, _) => {
            let relative_path = project.get_relative_path(relative_path)?;
            storage_backend
                .ignore_store_mut()
                .ignore_for_path(word, project.id(), &relative_path)
        }
        (None, None, true) => storage_backend
            .ignore_store_mut()
            .ignore_for_project(word, project.id()),
        (Some(_), Some(_), _) => bail!("Cannot use both --relative-path and --extension"),
    }
}

fn remove(project: Project, mut storage_backend: StorageBackend, opts: &RemoveOpts) -> Result<()> {
    let word = &opts.word;
    match (&opts.relative_path, &opts.extension, &opts.project) {
        (None, None, false) => storage_backend.remove_ignored(word),
        (None, Some(e), _) => storage_backend.remove_ignored_for_extension(word, e),
        (Some(relative_path), None, _) => {
            let relative_path = project.get_relative_path(relative_path)?;
            storage_backend.remove_ignored_for_path(word, project.id(), &relative_path)
        }
        (None, None, true) => storage_backend
            .ignore_store_mut()
            .remove_ignored_for_project(word, project.id()),
        (Some(_), Some(_), _) => bail!("Cannot use both --relative-path and --extension"),
    }
}

fn check(
    project: Project,
    storage_backend: StorageBackend,
    dictionary: impl Dictionary,
    opts: &CheckOpts,
    output_format: OutputFormat,
) -> Result<()> {
    let interactive = !opts.non_interactive;

    match interactive {
        false => {
            let mut checker =
                NonInteractiveChecker::new(project, dictionary, storage_backend, output_format)?;
            check_with(&mut checker, &opts.paths, output_format)
        }
        true => {
            let interactor = ConsoleInteractor;
            let mut checker =
                InteractiveChecker::new(project, interactor, dictionary, storage_backend)?;
            check_with(&mut checker, &opts.paths, output_format)
        }
    }
}

fn check_with<C>(checker: &mut C, paths: &[PathBuf], output_format: OutputFormat) -> Result<()>
where
    C: Checker<Context = (usize, usize)>,
{
    let project = checker.project();
    let skip_file = SkipFile::new(project)?;
    let mut paths = paths.to_vec();
    if paths.is_empty() {
        let walker = project.walk()?;
        for dir_entry in walker {
            let dir_entry = dir_entry?;
            let file_type = dir_entry.file_type().expect("walker yielded stdin");
            if !file_type.is_file() {
                continue;
            }
            let path = dir_entry.path();
            paths.push(path.to_path_buf());
        }
    }

    let mut checked = 0;
    let mut skipped = 0;
    for path in paths {
        let relative_path = checker.to_relative_path(&path)?;
        if skip_file.is_skipped(&relative_path) {
            skipped += 1;
        } else {
            let token_processor = TokenProcessor::new(&path);
            token_processor.each_token(|word, line, column| {
                checker.handle_token(word, &relative_path, &(line, column))
            })?;
            checked += 1;
        }
    }

    if output_format.is_text() {
        info_3!("Checked {checked} files - {skipped} skipped");
    }

    checker.success()
}

fn clean(mut storage_backend: StorageBackend) -> Result<()> {
    storage_backend.clean()
}

fn undo(mut storage_backend: StorageBackend) -> Result<()> {
    storage_backend.undo()
}

fn suggest(dictionary: impl Dictionary, opts: &SuggestOpts) -> Result<()> {
    let word = &opts.word;
    if dictionary.check(word)? {
        return Ok(());
    }

    let suggestions = dictionary.suggest(word);

    for suggestion in suggestions.iter() {
        println!("{}", suggestion);
    }

    Ok(())
}

// NOTE: we use this function to test the cli using a FakeDictionary
fn run<D: Dictionary>(
    project: Project,
    opts: &Opts,
    dictionary: D,
    storage_backend: StorageBackend,
) -> Result<()> {
    let output_format = opts.output_format.unwrap_or_default();
    match &opts.action {
        Action::Add(opts) => add(project, storage_backend, opts),
        Action::Remove(opts) => remove(project, storage_backend, opts),
        Action::Check(opts) => check(project, storage_backend, dictionary, opts, output_format),
        Action::Suggest(opts) => suggest(dictionary, opts),
        Action::Undo => undo(storage_backend),
        Action::Clean => clean(storage_backend),
    }
}
pub fn main() -> Result<()> {
    let opts: Opts = Opts::parse();
    let lang = match &opts.lang {
        Some(s) => s,
        None => "en_US",
    };

    let project_path = match opts.project_path.clone() {
        Some(p) => p,
        None => std::env::current_dir().context("Could not get current working directory")?,
    };

    let ignore_path = project_path.join(SKYSPELL_IGNORE_FILE);
    let mut ignore_config = None;

    if ignore_path.exists() {
        let kdl = std::fs::read_to_string(&ignore_path)
            .with_context(|| format!("While reading {SKYSPELL_IGNORE_FILE}"))?;
        ignore_config = Some(IgnoreConfig::parse(Some(ignore_path), &kdl)?);
    }

    let dictionary = EnchantDictionary::new(lang)?;
    let current_provider = dictionary.provider();

    let provider_in_config = ignore_config.as_ref().and_then(|c| c.provider());
    if let Some(provider_in_config) = provider_in_config {
        if current_provider != provider_in_config {
            bail!("Using '{current_provider}' as provider but should be '{provider_in_config}'")
        }
    }

    let use_db = ignore_config.as_ref().map(|c| c.use_db()).unwrap_or(true);
    let mut storage_backend = if use_db {
        let db_path = match opts.db_path.as_ref() {
            Some(s) => Ok(s.to_string()),
            None => get_default_db_path(lang),
        }?;
        if opts.text_output() {
            info_1!("Using {db_path} as storage");
        }
        let repository = SQLRepository::new(&db_path)?;
        StorageBackend::Repository(Box::new(repository))
    } else {
        let ignore_config =
            ignore_config.expect("ignore_config should not be None when use_db is false");
        if opts.text_output() {
            info_1!("Using {SKYSPELL_IGNORE_FILE} as storage");
        }
        StorageBackend::IgnoreStore(Box::new(ignore_config))
    };

    let project_path = ProjectPath::new(&project_path)?;
    let project = storage_backend.ensure_project(&project_path)?;

    let outcome = run(project, &opts, dictionary, storage_backend);
    if let Err(e) = outcome {
        print_error!("{}", e);
        std::process::exit(1);
    }
    Ok(())
}

#[cfg(test)]
mod tests;