diff --git a/crates/proof/src/affix.rs b/crates/proof/src/affix.rs index 1cbf3d1df..2c281c7ca 100644 --- a/crates/proof/src/affix.rs +++ b/crates/proof/src/affix.rs @@ -266,6 +266,11 @@ fn zero_empty(s: &str) -> String { if s == "0" { String::new() } else { s.to_string() } } +/// U+FE0F VARIATION SELECTOR-16 asks for the emoji form of the character before it. A dictionary +/// that uses an emoji as a flag may write it after the flag (RLA-ES Spanish: `PFX ☎️`, `banco/S☎️`), +/// but it is not a flag of its own: it is dropped wherever flags are read. +const EMOJI_SELECTOR: char = '\u{fe0f}'; + /// Read Hunspell `.aff` text (already decoded to UTF-8). fn parse_aff(aff: &str) -> Result { let mut r = Rules::default(); @@ -290,7 +295,7 @@ fn parse_aff(aff: &str) -> Result { } } directive @ ("FORBIDDENWORD" | "NOSUGGEST") => { - let mut it = at(1).chars(); + let mut it = at(1).chars().filter(|&c| c != EMOJI_SELECTOR); match (it.next(), it.next()) { (Some(c), None) if !c.is_whitespace() => { if directive == "FORBIDDENWORD" { @@ -320,7 +325,7 @@ fn parse_aff(aff: &str) -> Result { } "PFX" | "SFX" => { let suffix = dir == "SFX"; - let mut flag_chars = at(1).chars(); + let mut flag_chars = at(1).chars().filter(|&c| c != EMOJI_SELECTOR); let (Some(flag), None) = (flag_chars.next(), flag_chars.next()) else { return Err(format!("line {}: flag `{}` is not one character", n + 1, at(1))); }; @@ -377,7 +382,9 @@ impl AffixDict { let mut in_flags = false; while let Some(c) = chars.next() { if in_flags { - flags.push(c); + if c != EMOJI_SELECTOR { + flags.push(c); + } } else if c == '\\' && chars.peek() == Some(&'/') { word.push('/'); chars.next(); @@ -1079,6 +1086,27 @@ pub(crate) mod tests { assert!(AffixDict::from_hunspell("FLAG UTF-8\n", "0\n", &[]).is_ok(), "FLAG alone is harmless"); } + #[test] + fn emoji_flags_with_a_variation_selector_load() { + // RLA-ES (the Spanish dictionaries in LibreOffice and most Linux distributions) writes its + // telephone flag as ☎ followed by U+FE0F, in the rule and on the stems. + let aff = "SET UTF-8\nFLAG UTF-8\nPFX \u{260e}\u{fe0f} Y 1\nPFX \u{260e}\u{fe0f} 0 tele .\nSFX S Y 1\nSFX S 0 s [aeiou]\n"; + let d = AffixDict::from_hunspell(aff, "2\nbanco/S\u{260e}\u{fe0f}\nbasura/S\n", &[]).unwrap(); + assert!(d.contains("banco") && d.contains("bancos")); + assert!(d.contains("telebanco") && d.contains("telebancos"), "the ☎ prefix applies"); + assert!(d.contains("basuras") && !d.contains("telebasura"), "only to stems that carry it"); + assert!(!d.flag_chars.contains(&EMOJI_SELECTOR), "the selector is not a flag"); + } + + #[test] + fn emoji_forbiddenword_and_nosuggest_flags_load() { + // The same selector after the flag of the directives that name one flag. + let aff = "SET UTF-8\nFLAG UTF-8\nFORBIDDENWORD \u{260e}\u{fe0f}\nNOSUGGEST \u{1f6ab}\u{fe0f}\n"; + let d = AffixDict::from_hunspell(aff, "3\nbanco\nbanko/\u{260e}\u{fe0f}\nbancu/\u{1f6ab}\u{fe0f}\n", &[]).unwrap(); + assert!(d.contains("banco") && d.contains("bancu")); + assert!(!d.contains("banko"), "a stem with the FORBIDDENWORD flag is not a word"); + } + #[test] fn more_than_64_distinct_flags_load() { // VERO pt_BR uses 106 distinct flags (FLAG UTF-8). Build one with every basic Latin diff --git a/crates/proof/src/cs/mod.rs b/crates/proof/src/cs/mod.rs index 1d4b92f63..af743406d 100644 --- a/crates/proof/src/cs/mod.rs +++ b/crates/proof/src/cs/mod.rs @@ -13,8 +13,16 @@ use crate::affix::AffixDict; pub use suggest::suggest; -/// The Czech dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); +/// The Czech dictionary and whether it came from [`install_dictionary`] rather than a file on +/// disk (it then has no file of its own to report from [`installed_path`], even if a matching +/// file also happens to exist): one lock, so a reload and an install can't interleave their two +/// fields into an inconsistent combination. +static DICT: RwLock = RwLock::new(DictState { dict: None, in_memory: false }); + +struct DictState { + dict: Option>, + in_memory: bool, +} /// File names of a Czech Hunspell dictionary. const NAMES: &[&str] = &["cs_CZ", "cs-CZ", "cs"]; @@ -23,13 +31,13 @@ const NAMES: &[&str] = &["cs_CZ", "cs-CZ", "cs"]; /// [`install_dictionary`], or an empty one when none is available (Czech spelling is off /// then; see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(|e| e.into_inner()).as_ref() { + if let Some(d) = DICT.read().unwrap_or_else(|e| e.into_inner()).dict.as_ref() { return d.clone(); } let loaded = Arc::new(load_installed()); let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() + slot.dict.get_or_insert(loaded).clone() } fn load_installed() -> AffixDict { @@ -47,14 +55,29 @@ fn load_installed() -> AffixDict { /// UTF-8), for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(|e| e.into_inner()) = Some(Arc::new(d)); + let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); + slot.dict = Some(Arc::new(d)); + slot.in_memory = true; Ok(()) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(|e| e.into_inner()) = None; + let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); + slot.dict = None; + slot.in_memory = false; +} + +/// The `.dic` file of the installed Czech dictionary, searching the dictionaries folder and +/// the system locations now (File › Options › Proofing shows it, #3226); `None` when there is +/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported, even if a +/// matching file also happens to exist. +pub fn installed_path() -> Option { + if DICT.read().unwrap_or_else(|e| e.into_inner()).in_memory { + return None; + } + crate::hunspell::find(NAMES).map(|(_, dic)| dic) } /// Is a Czech dictionary available (spelling is checked)? diff --git a/crates/proof/src/hyphen.rs b/crates/proof/src/hyphen.rs index 9fdae033f..00297331e 100644 --- a/crates/proof/src/hyphen.rs +++ b/crates/proof/src/hyphen.rs @@ -15,6 +15,9 @@ //! //! **Bulgarian**: the Bulgarian syllable rules ([`crate::bg`]); no pattern file or word list. //! +//! **German**: the TeX German patterns for the reformed spelling ([`Patterns::de`]), also for +//! Swiss text, whose `ss` the patterns split like any double consonant (`Stras-se`). +//! //! Then the paragraph's limits ([`Limits`]) are applied. pub use crate::dict; @@ -36,6 +39,9 @@ static PT_PAT: &str = include_str!("../../../assets/hyphenation/pt.pat"); /// hyph-utf8's `hyph-it.tex`, unmodified (offered under MIT or LPPL; WordCraft uses MIT, whose /// notice is in the file's header). static IT_PAT: &str = include_str!("../../../assets/hyphenation/it.pat"); +/// hyph-utf8's `hyph-de-1996.tex` (reformed spelling), unmodified (MIT; the notice is in the +/// file's header). +static DE_PAT: &str = include_str!("../../../assets/hyphenation/de.pat"); impl Dictionary { /// The bundled US English dictionary (decoded on first use). @@ -77,6 +83,12 @@ impl Patterns { static P: OnceLock = OnceLock::new(); P.get_or_init(|| Patterns::parse(IT_PAT)) } + + /// The bundled German patterns (parsed on first use; the file has no exception words). + pub fn de() -> &'static Patterns { + static P: OnceLock = OnceLock::new(); + P.get_or_init(|| Patterns::parse(DE_PAT)) + } } /// Hyphenation limits from the paragraph's Hyphenation settings. @@ -130,11 +142,11 @@ fn core_breaks(core: &[char], lang: ProofLang) -> (Vec, Source) { if lang == ProofLang::Tr && core.iter().all(|&c| crate::tr::hyphen::is_letter(c)) { return (crate::tr::hyphen::points(core), Source::Rules); } - if lang == ProofLang::Pt { - return core_breaks_pt(core); - } - if lang == ProofLang::It { - return core_breaks_it(core); + match lang { + ProofLang::Pt => return core_breaks_patterns(Patterns::pt(), core), + ProofLang::It => return core_breaks_patterns(Patterns::it(), core), + ProofLang::De => return core_breaks_patterns(Patterns::de(), core), + _ => {} } if lang == ProofLang::Ru && core.iter().all(|&c| crate::ru::is_cyrillic(c)) { return (crate::ru::hyphen::points(core), Source::Heuristic); @@ -178,25 +190,9 @@ fn core_breaks_pl(core: &[char]) -> (Vec, Source) { (heuristic_points_pl(core), Source::Heuristic) } -/// [`core_breaks_pl`] for Portuguese: the bundled patterns, then the generic heuristic. -fn core_breaks_pt(core: &[char]) -> (Vec, Source) { - let pats = Patterns::pt(); - let s: String = core.iter().collect(); - if let Some(p) = pats.exception(&s) { - return (p.to_vec(), Source::Dictionary); - } - if !pats.is_empty() - && let Some(p) = pats.points(core) - { - return (p, Source::Patterns); - } - (heuristic_points(core), Source::Heuristic) -} - -/// [`core_breaks_pt`] for Italian: the bundled patterns (the file has no exception words), -/// then the generic heuristic. -fn core_breaks_it(core: &[char]) -> (Vec, Source) { - let pats = Patterns::it(); +/// [`core_breaks_pl`] for a language with bundled patterns (Portuguese, Italian, German): its +/// exception words, the patterns, then the generic heuristic. +fn core_breaks_patterns(pats: &Patterns, core: &[char]) -> (Vec, Source) { let s: String = core.iter().collect(); if let Some(p) = pats.exception(&s) { return (p.to_vec(), Source::Dictionary); @@ -862,6 +858,27 @@ mod tests { } } + #[test] + fn german_patterns_load() { + let p = Patterns::de(); + assert_eq!(p.len(), 36_709); + // The file has no exception words, and its `\message{…}` line adds none. + assert_eq!(p.exception_count(), 0); + } + + #[test] + fn german_words_follow_the_german_patterns() { + let de = |w: &str| hyphenate_word_in(w, &Limits::default(), ProofLang::De); + assert_eq!(de("Silbentrennung"), "Sil-ben-tren-nung"); + assert_eq!(de("unterliegende"), "un-ter-lie-gen-de"); + assert_eq!(de("öffentlich-rechtlicher"), "öf-fent-lich-recht-li-cher"); + assert_eq!(de("Begründung"), "Be-grün-dung"); + assert_eq!(de("Zucker"), "Zu-cker"); + // Swiss spelling: `ss` splits like any double consonant. + assert_eq!(de("Strasse"), "Stras-se"); + assert_eq!(word_breaks_in("Unterlagen", ProofLang::De).1, Source::Patterns); + } + #[test] fn bulgarian_words_follow_the_bulgarian_rules() { let bg = |w: &str| hyphenate_word_in(w, &Limits::default(), ProofLang::Bg); diff --git a/crates/proof/src/lang.rs b/crates/proof/src/lang.rs index f94ec95f4..d5c0ba13d 100644 --- a/crates/proof/src/lang.rs +++ b/crates/proof/src/lang.rs @@ -49,6 +49,10 @@ pub enum ProofLang { /// `id_ID` dictionary, dictionary-driven suggestions and generic checks with Indonesian /// messages. Hyphenation keeps the fallback English patterns. Id, + /// German (`de`, `de-DE`, `de-AT`, `de-CH`…): hyphenation by the German patterns (reformed + /// spelling). There is no German dictionary or grammar yet, so, as for [`ProofLang::Other`], + /// no spelling or grammar marks. + De, /// A language without proofing tools here (`fr-FR`, `ja-JP`, `x-none`…). Like Word without /// the language's proofing tools: no spelling or grammar marks, so its words are never /// flagged as English misspellings. Hyphenation keeps using the English patterns, as before @@ -98,6 +102,8 @@ impl ProofLang { ProofLang::Cs } else if primary.eq_ignore_ascii_case("id") || primary.eq_ignore_ascii_case("ind") || primary.eq_ignore_ascii_case("in") { ProofLang::Id + } else if ["de", "deu", "ger"].iter().any(|c| primary.eq_ignore_ascii_case(c)) { + ProofLang::De } else { ProofLang::Other } @@ -127,7 +133,7 @@ impl ProofLang { matches!(self, ProofLang::En | ProofLang::EnGb | ProofLang::EnCa) } - /// The primary language subtag (`en`, `pl`, `bg`, `tr`, `ru`, `cs`, `id`); empty for [`ProofLang::Other`]. + /// The primary language subtag (`en`, `pl`, `bg`, `tr`, `ru`, `cs`, `id`, `de`); empty for [`ProofLang::Other`]. pub fn code(self) -> &'static str { match self { ProofLang::En | ProofLang::EnGb | ProofLang::EnCa => "en", @@ -140,6 +146,7 @@ impl ProofLang { ProofLang::Ru => "ru", ProofLang::Cs => "cs", ProofLang::Id => "id", + ProofLang::De => "de", ProofLang::Other => "", } } @@ -275,7 +282,11 @@ mod tests { for t in ["tr", "tr-TR", "tr-CY", "TR_tr", "tur"] { assert_eq!(ProofLang::from_tag(Some(t)), ProofLang::Tr, "{t}"); } - for t in ["fr-FR", "de", "x-none", "ja-JP", "plx", "bgx", "trx", "csx", "idx", "inx", "e", "-"] { + for t in ["de", "de-DE", "de-CH", "DE_at", "deu", "ger"] { + assert_eq!(ProofLang::from_tag(Some(t)), ProofLang::De, "{t}"); + } + assert!(!ProofLang::De.is_proofed()); + for t in ["fr-FR", "dex", "x-none", "ja-JP", "plx", "bgx", "trx", "csx", "idx", "inx", "e", "-"] { assert_eq!(ProofLang::from_tag(Some(t)), ProofLang::Other, "{t}"); } assert_eq!(ProofLang::from_tag(None), ProofLang::En); diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 9347fba77..6e17a6921 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -34,10 +34,12 @@ //! - **Indonesian** ([`ProofLang::Id`], module [`id`]): the installed Hunspell `id_ID` //! dictionary (not bundled), dictionary-driven suggestions and generic checks with Indonesian //! messages. Hyphenation uses the existing English fallback. +//! - **German** ([`ProofLang::De`]): hyphenation by `hyph-utf8`'s German patterns (reformed +//! spelling, MIT); no spelling or grammar marks yet. //! - **Other languages** ([`ProofLang::Other`]): no spelling or grammar marks, like Word without //! the language's proofing tools. -//! - **Hyphenation** ([`hyphen`]): dictionary, Liang patterns (English, Polish, Portuguese), -//! Bulgarian syllable rules, Russian word-division rules, heuristic. +//! - **Hyphenation** ([`hyphen`]): dictionary, Liang patterns (English, Polish, Portuguese, +//! Italian, German), Bulgarian syllable rules, Russian word-division rules, heuristic. //! //! [`check_text`] checks a paragraph whose runs are in different languages. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -151,6 +153,8 @@ pub fn set_dictionaries_folder(dir: Option) { it::reload_dictionary(); hr::reload_dictionary(); tr::reload_dictionary(); + ru::reload_dictionary(); + cs::reload_dictionary(); id::reload_dictionary(); DICTIONARY_GENERATION.fetch_add(1, Ordering::Relaxed); } @@ -164,6 +168,8 @@ pub fn installed_dictionaries() -> Vec<(ProofLang, Option)> (ProofLang::It, it::installed_path()), (ProofLang::Hr, hr::installed_path()), (ProofLang::Tr, tr::installed_path()), + (ProofLang::Ru, ru::installed_path()), + (ProofLang::Cs, cs::installed_path()), (ProofLang::Id, id::installed_path()), ] } @@ -720,7 +726,7 @@ pub fn is_correct_in_with(word: &str, lang: ProofLang, opts: &Options) -> bool { ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct(word), ProofLang::Cs => accepted_by_options(word, opts) || cs::is_correct(word), ProofLang::Id => accepted_by_options(word, opts) || id::is_correct(word), - ProofLang::Bg | ProofLang::Other => true, + ProofLang::Bg | ProofLang::De | ProofLang::Other => true, } } @@ -736,7 +742,7 @@ pub fn suggest_in(word: &str, max: usize, lang: ProofLang) -> Vec { ProofLang::Ru => ru::suggest(word, max), ProofLang::Cs => cs::suggest(word, max), ProofLang::Id => id::suggest(word, max), - ProofLang::Bg | ProofLang::Other => Vec::new(), + ProofLang::Bg | ProofLang::De | ProofLang::Other => Vec::new(), } } @@ -762,7 +768,7 @@ fn grammar_in(text: &str, lang: ProofLang, capital_first: bool) -> Vec { // Czech has no sentence-capital rule, so a list item needs no exception. ProofLang::Cs => cs::grammar::check(text), ProofLang::Id => id::grammar::check(text), - ProofLang::Bg | ProofLang::Other => Vec::new(), + ProofLang::Bg | ProofLang::De | ProofLang::Other => Vec::new(), } } @@ -951,7 +957,7 @@ mod tests { assert_eq!(dictionary_needed(ProofLang::Pt, false), Some("pt_BR")); assert_eq!(dictionary_needed(ProofLang::Pl, true), None); assert_eq!(dictionary_needed(ProofLang::Pt, true), None); - for l in [ProofLang::En, ProofLang::Bg, ProofLang::Other] { + for l in [ProofLang::En, ProofLang::Bg, ProofLang::De, ProofLang::Other] { assert_eq!(dictionary_needed(l, false), None); } // Installed dictionaries and the built-in English list: no hint. @@ -977,11 +983,18 @@ mod tests { ("pt_BR", pt::test_dict::AFF, pt::test_dict::DIC), ("it_IT", it::test_dict::AFF, it::test_dict::DIC), ("hr_HR", hr::test_dict::AFF, hr::test_dict::DIC), + ("ru_RU", ru::test_dict::AFF, ru::test_dict::DIC), + ("cs_CZ", cs::test_dict::AFF, cs::test_dict::DIC), ("id_ID", id::test_dict::AFF, id::test_dict::DIC), ] { std::fs::write(dir.join(format!("{name}.aff")), aff).unwrap(); std::fs::write(dir.join(format!("{name}.dic")), dic).unwrap(); } + // #3252 review: force Russian and Czech to cache whatever they find before the folder is + // set (empty, with no folder and nothing installed on CI), so the folder change below has + // a stale cache to actually invalidate, not just an untouched `None`. + ru::dictionary(); + cs::dictionary(); let before = dictionary_generation(); set_dictionaries_folder(Some(dir.clone())); assert!(dictionary_generation() > before, "cached checks are dropped"); @@ -998,7 +1011,16 @@ mod tests { (ProofLang::Hr, Some(dir.join("hr_HR.dic"))), ] ); + // #3226: Russian and Czech were missing from installed_dictionaries() entirely (no + // installed_path() in either module), so neither ever showed up in File › Options › + // Proofing, installed or not. Their languages, not their exact path, is what belongs + // here: unlike the four above, installed_path() for Russian and Czech also reflects + // whether install_dictionary() gave one directly (#3252 review), which ru's and cs's own + // test modules do, concurrently, in tests of their own — asserting an exact path here + // would race them for no reason this test needs to care about. + assert_eq!(found[5..7].iter().map(|(lang, _)| *lang).collect::>(), [ProofLang::Ru, ProofLang::Cs], "{found:?}"); assert!(is_correct_in("chrząszcz", ProofLang::Pl) && !is_correct_in("chrzonszcz", ProofLang::Pl)); + assert!(is_correct_in("дом", ProofLang::Ru) && !is_correct_in("дум", ProofLang::Ru)); } /// #1741: a list item may be a fragment, so its first word needs no capital in any language @@ -1662,7 +1684,7 @@ mod tests { let _ = has_dictionary(Some(&s)); let _ = is_correct_for(&s, Some(&s)); let _ = hyphen::hyphen_points(&s, &hyphen::Limits::default()); - for lang in [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Hr, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::Other] { + for lang in [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Hr, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::De, ProofLang::Other] { let _ = check_spelling_in(&s, lang); let _ = check_grammar_in(&s, lang); let _ = is_correct_in(&s, lang); diff --git a/crates/proof/src/patterns.rs b/crates/proof/src/patterns.rs index 50dd74444..a91235e48 100644 --- a/crates/proof/src/patterns.rs +++ b/crates/proof/src/patterns.rs @@ -116,13 +116,16 @@ pub struct Patterns { enum Block { Patterns, Exceptions, + /// Another command's argument (`\message{…}`), ignored. + Skip, } impl Patterns { /// Parse patterns: whitespace-separated, `%` comments to the end of the line (`#` too at the /// start of a line). A whole TeX pattern file works as is: `\patterns{…}` holds patterns and - /// `\hyphenation{…}` exception words written with their hyphens (`ni-gdy`); outside any - /// block, a token with a hyphen is an exception and anything else a pattern. + /// `\hyphenation{…}` exception words written with their hyphens (`ni-gdy`), and other + /// commands' arguments (`\message{…}`) are skipped; outside any block, a token with a hyphen + /// is an exception and anything else a pattern. pub fn parse(text: &str) -> Patterns { let mut p = Patterns::default(); let mut block = Block::Patterns; @@ -136,12 +139,18 @@ impl Patterns { let mut tok = tok; if let Some(cmd) = tok.strip_prefix('\\') { let (name, rest) = cmd.split_once('{').unwrap_or((cmd, "")); - block = if name == "hyphenation" { Block::Exceptions } else { Block::Patterns }; + block = match name { + "hyphenation" => Block::Exceptions, + "patterns" => Block::Patterns, + // shortcut: the argument ends at the first `}`, so nested braces would leak; no bundled file has them. + _ if cmd.contains('{') => Block::Skip, + _ => Block::Patterns, + }; tok = rest; } let closes = tok.ends_with('}'); let tok = tok.trim_end_matches('}'); - if !tok.is_empty() { + if !tok.is_empty() && block != Block::Skip { if block == Block::Exceptions || tok.contains('-') { p.insert_exception(tok); } else { @@ -337,6 +346,12 @@ mod tests { assert!(p.to_tex().contains(&".ab4cdefghijklm5n".to_string())); } + #[test] + fn other_commands_arguments_are_skipped() { + let p = Patterns::parse("\\message{Some Patterns `x-y' 2024-02-28}\n\\patterns{%\na1b\n}\n\\endinput\n"); + assert_eq!((p.len(), p.exception_count()), (1, 0)); + } + #[test] fn apply_tolerates_short_value_buffers() { let p = Patterns::parse("a1b"); diff --git a/crates/proof/src/ru/mod.rs b/crates/proof/src/ru/mod.rs index 4515c695c..4b1ffe093 100644 --- a/crates/proof/src/ru/mod.rs +++ b/crates/proof/src/ru/mod.rs @@ -34,8 +34,16 @@ use crate::affix::AffixDict; pub use suggest::suggest; -/// The Russian dictionary: `None` until first use, then the installed one (or an empty one). -static DICT: RwLock>> = RwLock::new(None); +/// The Russian dictionary and whether it came from [`install_dictionary`] rather than a file on +/// disk (it then has no file of its own to report from [`installed_path`], even if a matching +/// file also happens to exist): one lock, so a reload and an install can't interleave their two +/// fields into an inconsistent combination. +static DICT: RwLock = RwLock::new(DictState { dict: None, in_memory: false }); + +struct DictState { + dict: Option>, + in_memory: bool, +} /// File names of a Russian Hunspell dictionary. const NAMES: &[&str] = &["ru_RU", "ru-RU", "ru"]; @@ -44,13 +52,13 @@ const NAMES: &[&str] = &["ru_RU", "ru-RU", "ru"]; /// [`install_dictionary`], or an empty one when none is available (Russian spelling is off then; /// see [`has_dictionary`]). pub fn dictionary() -> Arc { - if let Some(d) = DICT.read().unwrap_or_else(|e| e.into_inner()).as_ref() { + if let Some(d) = DICT.read().unwrap_or_else(|e| e.into_inner()).dict.as_ref() { return d.clone(); } let loaded = Arc::new(load_installed()); let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); // Another thread (or an install) may have filled it meanwhile: that one wins. - slot.get_or_insert(loaded).clone() + slot.dict.get_or_insert(loaded).clone() } fn load_installed() -> AffixDict { @@ -68,14 +76,29 @@ fn load_installed() -> AffixDict { /// for example one the user picked. On error the current dictionary stays. pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { let d = AffixDict::from_hunspell(aff, dic, &[])?; - *DICT.write().unwrap_or_else(|e| e.into_inner()) = Some(Arc::new(d)); + let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); + slot.dict = Some(Arc::new(d)); + slot.in_memory = true; Ok(()) } /// Forget the loaded dictionary, so the next check looks for an installed one again (after /// [`crate::hunspell::set_dictionary_dir`] or a dictionary was installed). pub fn reload_dictionary() { - *DICT.write().unwrap_or_else(|e| e.into_inner()) = None; + let mut slot = DICT.write().unwrap_or_else(|e| e.into_inner()); + slot.dict = None; + slot.in_memory = false; +} + +/// The `.dic` file of the installed Russian dictionary, searching the dictionaries folder and +/// the system locations now (File › Options › Proofing shows it, #3226); `None` when there is +/// none. A dictionary given with [`install_dictionary`] has no file and isn't reported, even if a +/// matching file also happens to exist. +pub fn installed_path() -> Option { + if DICT.read().unwrap_or_else(|e| e.into_inner()).in_memory { + return None; + } + crate::hunspell::find(NAMES).map(|(_, dic)| dic) } /// Is a Russian dictionary available (spelling is checked)?