Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: filter out unknown `only` and `ignore` rules only once (#2231)
Andrew Dunbar8ff2fe4b7927
diff --git a/harper-cli/src/lint.rs b/harper-cli/src/lint.rsindex a2bdc1611..8e268b667 100644--- a/harper-cli/src/lint.rs+++ b/harper-cli/src/lint.rs@@ -1,15 +1,18 @@-use either::Either;-use harper_core::spell::{Dictionary, MergedDictionary, MutableDictionary};-use hashbrown::HashMap;use std::collections::BTreeMap;use std::path::{Component, Path, PathBuf};use std::sync::Arc;use std::{fs, process};use ariadne::{Color, Fmt, Label, Report, ReportKind, Source};-use harper_core::linting::{Lint, LintGroup, LintKind};-use harper_core::parsers::MarkdownOptions;-use harper_core::{Dialect, DictWordMetadata, Document, Token, TokenKind, remove_overlaps_map};+use either::Either;+use hashbrown::HashMap;++use harper_core::{+ linting::{Lint, LintGroup, LintGroupConfig, LintKind},+ parsers::MarkdownOptions,+ spell::{Dictionary, MergedDictionary, MutableDictionary},+ {Dialect, DictWordMetadata, Document, Token, TokenKind, remove_overlaps_map},+};use crate::input::Input;@@ -46,12 +49,31 @@ pub struct LintOptions<'a> {pub only: &'a Option<Vec<String>>,pub dialect: Dialect,}-enum ReportStyle {FullAriadneLintReport,BriefCountsOnlyLintReport,}+struct InputInfo<'a> {+ parent_input_id: &'a str,+ input: Input,+}++trait InputPath {+ fn format_path(&self) -> String;+}++impl InputPath for InputInfo<'_> {+ fn format_path(&self) -> String {+ let child = self.input.get_identifier();+ if self.parent_input_id.is_empty() {+ child.into_owned()+ } else {+ format!("\x1b[33m{}/\x1b[0m{}", self.parent_input_id, child)+ }+ }+}+pub fn lint(markdown_options: MarkdownOptions,curated_dictionary: Arc<dyn Dictionary>,@@ -75,6 +97,32 @@ pub fn lint(inputs};+ // Filter out any rules from ignore/only lists that don't exist in the current config+ // Uses a cached config to avoid expensive linter initialization+ let config = LintGroupConfig::new_curated();+ let mut ignore = ignore.clone();+ let mut only = only.clone();++ if let Some(ref mut only) = only {+ only.retain(|rule| {+ if !config.has_rule(rule) {+ eprintln!("Warning: Cannot enable unknown rule '{}'.", rule);+ return false;+ }+ true+ });+ }++ if let Some(ref mut ignore) = ignore {+ ignore.retain(|rule| {+ if !config.has_rule(rule) {+ eprintln!("Warning: Cannot disable unknown rule '{}'.", rule);+ return false;+ }+ true+ });+ }+// Create merged dictionary with base dictionarylet mut curated_plus_user_dict = MergedDictionary::new();curated_plus_user_dict.add_dictionary(Arc::new(curated_dictionary));@@ -111,15 +159,25 @@ pub fn lint(// All the files within this input if it's a Dir, or just this input otherwise.let inputs = if let Some(dir) = maybe_dir {- Either::Left(- dir.filter_map(Result::ok)- .filter(|entry| entry.file_type().map(|ft| !ft.is_dir()).unwrap_or(false))- .map(|entry| Input::File(entry.path())),- )+ let mut entries: Vec<_> = dir+ .filter_map(Result::ok)+ .filter(|entry| entry.file_type().map(|ft| !ft.is_dir()).unwrap_or(false))+ .collect();++ // Sort entries by file name+ entries.sort_by_key(|entry| entry.file_name());++ Either::Left(entries.into_iter().map(|entry| Input::File(entry.path())))} else {Either::Right(std::iter::once(user_input.clone()))};+ let parent_input_id = if batch_mode {+ user_input.get_identifier().to_string()+ } else {+ String::new()+ };+for current_input in inputs {let lint_results = lint_one_input(// Common properties of harper-cli@@ -129,15 +187,18 @@ pub fn lint(&report_mode,LintOptions {count,- ignore,- only,+ ignore: &ignore,+ only: &only,dialect,},&file_dict_path,// Are we linting multiple inputs inside a directory?batch_mode,// The current input to be linted- current_input,+ InputInfo {+ parent_input_id: &parent_input_id,+ input: current_input,+ },);// Update the global stats@@ -173,8 +234,8 @@ type LintRuleCount = HashMap<String, usize>;type LintKindRulePairCount = HashMap<(LintKind, String), usize>;type SpelloCount = HashMap<String, usize>;-struct InputInfo {- input: Input,+struct FullInputInfo<'a> {+ input: InputInfo<'a>,doc: Document,source: String,}@@ -190,7 +251,7 @@ fn lint_one_input(// Are we linting multiple inputs?batch_mode: bool,// For the current input- current_input: Input,+ current: InputInfo,) -> (LintKindCount,LintRuleCount,@@ -210,27 +271,30 @@ fn lint_one_input(let mut spellos: HashMap<String, usize> = HashMap::new();// Create a new merged dictionary for this input.- let mut dictionary = curated_plus_user_dict.clone();+ let mut merged_dictionary = curated_plus_user_dict.clone();// If processing a file, try to load its per-file dictionary- if let Input::File(ref file) = current_input {+ if let Input::File(ref file) = current.input {let dict_path = file_dict_path.join(file_dict_name(file));if let Ok(file_dictionary) = load_dict(&dict_path) {- dictionary.add_dictionary(Arc::new(file_dictionary));+ merged_dictionary.add_dictionary(Arc::new(file_dictionary));println!("{}: Note: Using per-file dictionary: {}",- ¤t_input.get_identifier(),+ current.format_path(),dict_path.display());}}- match current_input.load(batch_mode, markdown_options, &dictionary) {+ match current+ .input+ .load(batch_mode, markdown_options, &merged_dictionary)+ {Err(err) => eprintln!("{}", err),Ok((maybe_doc, source)) => {if let Some(doc) = maybe_doc {// 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(dictionary.into(), dialect);+ let mut lint_group = LintGroup::new_curated(merged_dictionary.into(), dialect);// Turn specified rules on or offconfigure_lint_group(&mut lint_group, only, ignore);@@ -249,8 +313,11 @@ fn lint_one_input(spellos = collect_spellos(&named_lints, doc.get_source());single_input_report(- &InputInfo {- input: current_input,+ &FullInputInfo {+ input: InputInfo {+ parent_input_id: current.parent_input_id,+ input: current.input,+ },doc,source,},@@ -277,22 +344,15 @@ fn configure_lint_group() {if let Some(rules) = only {lint_group.set_all_rules_to(Some(false));-- for rule in rules {- if !lint_group.contains_key(rule) {- eprintln!("Warning: Cannot enable unknown rule '{}'.", &rule);- }- lint_group.config.set_rule_enabled(rule, true);- }+ rules+ .iter()+ .for_each(|rule| lint_group.config.set_rule_enabled(rule, true));}if let Some(rules) = ignore {- for rule in rules {- if !lint_group.contains_key(rule) {- eprintln!("Warning: Cannot disable unknown rule '{}'.", &rule);- }- lint_group.config.set_rule_enabled(rule, false);- }+ rules+ .iter()+ .for_each(|rule| lint_group.config.set_rule_enabled(rule, false));}}@@ -349,7 +409,7 @@ fn collect_spellos(fn single_input_report(// Properties of the current input- input_info: &InputInfo,+ input_info: &FullInputInfo,// Linting results of this inputnamed_lints: &BTreeMap<String, Vec<Lint>>,lint_count: (usize, usize),@@ -359,11 +419,7 @@ fn single_input_report(batch_mode: bool, // If true, we are processing multiple files, which affects how we reportreport_mode: &ReportStyle,) {- let InputInfo {- input: current_input,- doc,- source,- } = input_info;+ let FullInputInfo { input, doc, source } = input_info;let (lint_count_before, lint_count_after) = lint_count;// The Ariadne report works poorly for files with very long lines, so suppress it unless only processing one fileconst MAX_LINE_LEN: usize = 150;@@ -378,14 +434,14 @@ fn single_input_report(report_mode = &ReportStyle::BriefCountsOnlyLintReport;println!("{}: Longest line: {longest} exceeds max line length: {MAX_LINE_LEN}",- ¤t_input.get_identifier()+ input.format_path());}// Report the number of lints no matter what report mode we are inprintln!("{}: {}",- current_input.get_identifier(),+ input.format_path(),match (lint_count_before, lint_count_after) {(0, _) => "No lints found".to_string(),(before, after) if before != after =>@@ -398,7 +454,7 @@ fn single_input_report(if matches!(report_mode, ReportStyle::FullAriadneLintReport) {let primary_color = Color::Magenta;- let input_identifier = current_input.get_identifier();+ let input_identifier = input.input.get_identifier();if lint_count_after != 0 {let mut report_builder = Report::build(ReportKind::Advice, &input_identifier, 0);diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 60913b331..a9d3e6d38 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -223,6 +223,11 @@ fn curated_config() -> LintGroupConfig {}impl LintGroupConfig {+ /// Check if a rule exists in the configuration.+ pub fn has_rule(&self, key: impl AsRef<str>) -> bool {+ self.inner.contains_key(key.as_ref())+ }+pub fn set_rule_enabled(&mut self, key: impl ToString, val: bool) {self.inner.insert(key.to_string(), Some(val));}