Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: `just getforms` uses dictionary annotation if none provided
hippietrail99e07750f4aa
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rsindex 686983dad..602ce9707 100644--- a/harper-cli/src/main.rs+++ b/harper-cli/src/main.rs@@ -1,6 +1,7 @@#![doc = include_str!("../README.md")]use std::collections::BTreeMap;+use std::collections::HashSet;use std::path::{Path, PathBuf};use std::process;@@ -11,7 +12,9 @@ use harper_comments::CommentParser;use harper_core::linting::{LintGroup, Linter};use harper_core::parsers::{Markdown, MarkdownOptions};use harper_core::spell::hunspell::parse_default_attribute_list;+use harper_core::spell::hunspell::parse_default_word_list;use harper_core::spell::hunspell::word_list::parse_word_list;+use harper_core::spell::hunspell::word_list::MarkedWord;use harper_core::{remove_overlaps, CharString, CharStringExt, Dictionary, Document, FstDictionary, TokenKind,TokenStringExt, WordMetadata,@@ -199,32 +202,104 @@ fn main() -> anyhow::Result<()> {Ok(())}- Args::Forms { words } => {- let mut expanded: HashMap<CharString, WordMetadata> = HashMap::new();- let attributes = parse_default_attribute_list();- let total = words.len();-- for (index, word) in words.iter().enumerate() {- expanded.clear();-- let hunspell_word_list = format!("1\n{word}");- let words = parse_word_list(&hunspell_word_list.to_string()).unwrap();- attributes.expand_marked_words(words, &mut expanded);-- println!(- "{}{}{}",- if index > 0 { "\n" } else { "" },- if total != 1 {- format!("{}/{}: ", index + 1, total)+ Args::Forms { words: args_words } => {+ let default_attrs = parse_default_attribute_list();+ let args_total = args_words.len();++ let default_marked_words = parse_default_word_list().unwrap();++ for (arg_index, arg_word) in args_words.iter().enumerate() {+ let mut arg_bare_word = arg_word.to_string();+ let mut arg_annot = None;+ if let Some((word_part, annot_part)) = arg_word.split_once('/') {+ arg_bare_word = word_part.to_string();+ arg_annot = Some(annot_part);+ }++ let mut already_in_dict = false;+ let mut already_annotated = false;+ let old_annot = get_annotations(&default_marked_words, &arg_bare_word);++ if let Some(ref annot) = old_annot {+ already_in_dict = true;+ already_annotated = !annot.is_empty();+ }++ let mut status_summary = match (already_in_dict, already_annotated) {+ (true, true) => format!(+ "'{}' is already in the dictionary with annotation `{}`.",+ arg_bare_word,+ old_annot.clone().unwrap()+ ),+ (true, false) => format!(+ "'{}' is already in the dictionary but not annotated.",+ arg_bare_word+ ),+ _ => format!(+ "'{}' is not in the dictionary yet.",+ arg_bare_word+ ),+ };++ let mut different_annot = false;++ if already_annotated && arg_annot.is_some() {+ if old_annot+ .clone()+ .unwrap()+ .chars()+ .collect::<HashSet<char>>()+ != arg_annot.unwrap().chars().collect()+ {+ different_annot = true;+ status_summary.push_str(+ format!("\n Your annotations differ from the dictionary.\n").as_str(),+ );} else {- "".to_string()- },- word- );- expanded.keys().for_each(|form| {- let string_form: String = form.iter().collect();- println!(" - {}", string_form);- });+ status_summary.push_str(+ format!("\n Your annotations are the same as the dictionary.\n").as_str(),+ );+ }+ }++ println!("{}", status_summary);++ let print_word_info = |arg_index: usize, arg_word: &str| {+ let mut our_expanded_word: HashMap<CharString, WordMetadata> = HashMap::new();+ let our_marked_word = parse_word_list(&format!("1\n{arg_word}")).unwrap();+ default_attrs.expand_marked_words(our_marked_word, &mut our_expanded_word);++ println!(+ "{}{}{}",+ if arg_index > 0 { "\n" } else { "" },+ if args_total != 1 {+ format!("{}/{}: ", arg_index + 1, args_total)+ } else {+ "".to_string()+ },+ arg_word+ );++ our_expanded_word.keys().for_each(|form| {+ let string_form: String = form.iter().collect();+ println!(" - {}", string_form);+ });+ };++ let should_print_old_info = already_annotated;+ let should_print_new_info = arg_annot.is_some() && (!already_annotated || different_annot);++ if should_print_old_info {+ println!("Old, from the dictionary:");+ print_word_info(+ arg_index,+ &format!("{}/{}", arg_bare_word, old_annot.unwrap()),+ );+ }+ if should_print_new_info {+ println!("New, from you:");+ print_word_info(arg_index, arg_word);+ }}Ok(())@@ -306,3 +381,14 @@ fn load_file(file: &Path, markdown_options: MarkdownOptions) -> anyhow::Result<(Ok((Document::new_curated(&source, &parser), source))}++fn get_annotations(marked_words: &[MarkedWord], word: &str) -> Option<String> {+ for marked_word in marked_words.iter() {+ let mw = &marked_word.letters;+ let mw = mw.iter().collect::<String>();+ if mw == word {+ return Some(marked_word.attributes.iter().collect::<String>());+ }+ }+ None+}