Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

8.8 KB
Raw Blame
use std::{collections::BTreeMap, path::Path, sync::Arc};

use hashbrown::HashMap;

use harper_core::{
    linting::{Lint, LintGroup, LintKind},
    parsers::MarkdownOptions,
    remove_overlaps_map,
    spell::MergedDictionary,
    weirpack::Weirpack,
};

use crate::{
    LintOptions,
    input::single_input::SingleInputTrait,
    lint::{
        FullInputInfo, InputInfo, JsonFileResult, JsonLint, JsonSpan, LintOneResult, ReportStyle,
        file_dict_name, load_dict,
    },
    lint_reporter::{char_index_to_line_col, single_input_report},
};

#[allow(clippy::too_many_arguments)]
pub fn lint_one_input(
    // Common properties of harper-cli
    markdown_options: MarkdownOptions,
    curated_plus_user_dict: &MergedDictionary,
    report_mode: &ReportStyle,
    // Options passed from the user specific to the `lint` subcommand
    lint_options: &LintOptions,
    weirpacks: &[Weirpack],
    file_dict_path: &Path,
    // Are we linting multiple inputs?
    batch_mode: bool,
    // For the current input
    current: InputInfo,
) -> anyhow::Result<LintOneResult> {
    let LintOptions {
        count: _,
        ignore,
        only,
        keep_overlapping_lints,
        dialect,
        weirpack_inputs: _,
        color: _,
        format: _,
        quiet: _,
        no_parallel: _,
    } = lint_options;

    let mut lint_kinds: HashMap<LintKind, usize> = HashMap::new();
    let mut lint_rules: HashMap<String, usize> = HashMap::new();
    let mut lint_kind_rule_pairs: HashMap<(LintKind, String), usize> = HashMap::new();
    let mut spellos: HashMap<String, usize> = HashMap::new();
    let mut json: Option<JsonFileResult> = None;

    if let Some(single_input) = current.input.try_as_single_ref() {
        // Create a new merged dictionary for this input.
        let mut merged_dictionary = curated_plus_user_dict.clone();

        // If processing a file, try to load its per-file dictionary
        if let Some(file) = single_input.try_as_file_ref() {
            let dict_path = file_dict_path.join(file_dict_name(file.path()));
            if let Ok(file_dictionary) = load_dict(&dict_path) {
                merged_dictionary.add_dictionary(Arc::new(file_dictionary));
                eprintln!(
                    "{}: Note: Using per-file dictionary: {}",
                    current.format_path(),
                    dict_path.display()
                );
            }
        }

        match single_input.load(markdown_options, &merged_dictionary) {
            Err(err) => {
                eprintln!("{}", err);
                if matches!(report_mode, ReportStyle::Json) {
                    json = Some(JsonFileResult {
                        file: current.plain_path(),
                        lint_count: 0,
                        lints: vec![],
                        error: Some(err.to_string()),
                    });
                }
            }
            Ok((doc, source)) => {
                // Create the Lint Group from which we will lint this input, using the combined dictionary and the specified dialect
                let mut lint_group = LintGroup::new_curated(merged_dictionary.into(), *dialect);

                for pack in weirpacks {
                    let pack_group = pack.to_lint_group()?;
                    lint_group.merge_from(pack_group);
                }

                // Turn specified rules on or off
                configure_lint_group(&mut lint_group, only, ignore);

                // Run the linter, getting back a map of rule name -> lints
                let mut named_lints = lint_group.organized_lints(&doc);

                // Lint counts, for brief reporting
                let lint_count_before = named_lints.values().map(|v| v.len()).sum::<usize>();
                if !keep_overlapping_lints {
                    remove_overlaps_map(&mut named_lints);
                }
                let lint_count_after = named_lints.values().map(|v| v.len()).sum::<usize>();

                // Extract the lint kinds and rules etc. for reporting
                (lint_kinds, lint_rules) = count_lint_kinds_and_rules(&named_lints);
                lint_kind_rule_pairs = collect_lint_kind_rule_pairs(&named_lints);
                spellos = collect_spellos(&named_lints, doc.get_source());

                // Build JSON result if in JSON mode
                if matches!(report_mode, ReportStyle::Json) {
                    let file = current.plain_path();
                    let source_chars = doc.get_source();
                    let mut lints = Vec::new();

                    for (rule_name, rule_lints) in &named_lints {
                        for lint in rule_lints {
                            let (line, column) =
                                char_index_to_line_col(source_chars, lint.span.start);
                            let matched_text = lint.get_str(source_chars);
                            let suggestions: Vec<String> =
                                lint.suggestions.iter().map(|s| format!("{s}")).collect();
                            lints.push(JsonLint {
                                rule: rule_name.clone(),
                                kind: lint.lint_kind.to_string(),
                                span: JsonSpan {
                                    char_start: lint.span.start,
                                    char_end: lint.span.end,
                                },
                                line,
                                column,
                                message: lint.message.clone(),
                                priority: lint.priority,
                                suggestions,
                                matched_text,
                            });
                        }
                    }

                    json = Some(JsonFileResult {
                        file,
                        lint_count: lint_count_after,
                        lints,
                        error: None,
                    });
                }

                single_input_report(
                    &FullInputInfo {
                        input: InputInfo {
                            parent_input_id: current.parent_input_id,
                            input: current.input,
                            color: current.color,
                        },
                        doc,
                        source,
                    },
                    // Linting results of this input
                    &named_lints,
                    (lint_count_before, lint_count_after),
                    &lint_kinds,
                    &lint_rules,
                    // Reporting arguments
                    batch_mode,
                    (report_mode, lint_options.quiet),
                );
            }
        }
    }

    Ok(LintOneResult {
        lint_kinds,
        lint_rules,
        lint_kind_rule_pairs,
        spellos,
        json,
    })
}

fn configure_lint_group(
    lint_group: &mut LintGroup,
    only: &Option<Vec<String>>,
    ignore: &Option<Vec<String>>,
) {
    if let Some(rules) = only {
        lint_group.set_all_rules_to(Some(false));
        rules
            .iter()
            .for_each(|rule| lint_group.config.set_rule_enabled(rule, true));
    }

    if let Some(rules) = ignore {
        rules
            .iter()
            .for_each(|rule| lint_group.config.set_rule_enabled(rule, false));
    }

    // Have all rules been disabled somehow?
    if !lint_group
        .iter_keys()
        .any(|rule| lint_group.config.is_rule_enabled(rule))
    {
        eprintln!("Warning: No rules are enabled.");
    }
}

fn count_lint_kinds_and_rules(
    named_lints: &BTreeMap<String, Vec<Lint>>,
) -> (HashMap<LintKind, usize>, HashMap<String, usize>) {
    let mut kinds = HashMap::new();
    let mut rules = HashMap::new();

    for (rule_name, lints) in named_lints {
        lints
            .iter()
            .for_each(|lint| *kinds.entry(lint.lint_kind).or_insert(0) += 1);

        if !lints.is_empty() {
            *rules.entry(rule_name.to_string()).or_insert(0) += lints.len();
        }
    }

    (kinds, rules)
}

fn collect_lint_kind_rule_pairs(
    named_lints: &BTreeMap<String, Vec<Lint>>,
) -> HashMap<(LintKind, String), usize> {
    let mut pairs = HashMap::new();

    for (rule_name, lints) in named_lints {
        for lint in lints {
            pairs
                .entry((lint.lint_kind, rule_name.to_string()))
                .and_modify(|count| *count += 1)
                .or_insert(1);
        }
    }

    pairs
}

fn collect_spellos(
    named_lints: &BTreeMap<String, Vec<Lint>>,
    source: &[char],
) -> HashMap<String, usize> {
    named_lints
        .get("SpellCheck")
        .into_iter()
        .flatten()
        .map(|lint| lint.get_str(source))
        .fold(HashMap::new(), |mut acc, spello| {
            *acc.entry(spello).or_insert(0) += 1;
            acc
        })
}