From 3e9a65fa80b575b3fb52e9b66f1e6500c578b07e Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Sun, 19 Jan 2025 13:23:17 -0800 Subject: [PATCH] Add allow word list from config --- codebook-config/src/lib.rs | 20 +++++++++++++++----- codebook-lsp/src/lsp.rs | 2 +- codebook/src/dictionary.rs | 37 ++++++++++++++++++++++++------------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/codebook-config/src/lib.rs b/codebook-config/src/lib.rs index 202b8a2..3924fdc 100644 --- a/codebook-config/src/lib.rs +++ b/codebook-config/src/lib.rs @@ -8,6 +8,8 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::sync::RwLock; +static CACHE_DIR: &str = "codebook"; + #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] pub struct ConfigSettings { /// List of dictionaries to use for spell checking @@ -50,7 +52,7 @@ impl Default for CodebookConfig { Self { settings: Arc::new(RwLock::new(ConfigSettings::default())), config_path: None, - cache_dir: env::temp_dir().join("codebook"), + cache_dir: env::temp_dir().join(CACHE_DIR), } } } @@ -62,6 +64,14 @@ impl CodebookConfig { Self::find_and_load_config(¤t_dir) } + pub fn new_no_file() -> Self { + Self { + settings: Arc::new(RwLock::new(ConfigSettings::default())), + config_path: None, + cache_dir: env::temp_dir().join(CACHE_DIR), + } + } + pub fn reload(&self) -> Result<()> { let config_path = self .config_path @@ -109,10 +119,10 @@ impl CodebookConfig { /// Save the configuration to its file pub fn save(&self) -> Result<()> { - let config_path = self - .config_path - .as_ref() - .ok_or_else(|| anyhow!("No config file path available."))?; + let config_path = match self.config_path.as_ref() { + Some(c) => c, + None => return Ok(()), + }; let content = toml::to_string_pretty(&*self.settings.read().unwrap()) .context("Failed to serialize config")?; diff --git a/codebook-lsp/src/lsp.rs b/codebook-lsp/src/lsp.rs index a4a0ef8..eebc64d 100644 --- a/codebook-lsp/src/lsp.rs +++ b/codebook-lsp/src/lsp.rs @@ -89,7 +89,7 @@ impl Backend { } /// Helper method to publish diagnostics for spell-checking. async fn publish_spellcheck_diagnostics(&self, uri: &Url, text: &str) { - self.config.reload(); + let _ = self.config.reload(); // Convert the file URI to a local file path (if needed). let uri = uri.clone(); let file_path = uri.to_file_path().unwrap_or_default(); diff --git a/codebook/src/dictionary.rs b/codebook/src/dictionary.rs index b1ef1ea..a7ed7e6 100644 --- a/codebook/src/dictionary.rs +++ b/codebook/src/dictionary.rs @@ -15,6 +15,8 @@ use std::{ use streaming_iterator::StreamingIterator; use tree_sitter::{Parser, Query, QueryCursor}; +static COMMON_DICTIONARY: &str = include_str!("../../word_lists/combined.gen.txt"); + #[derive(Debug, Clone, PartialEq)] pub struct SpellCheckResult { pub word: String, @@ -75,12 +77,14 @@ impl CodeDictionary { } pub fn check(&self, word: &str) -> bool { - self.custom_dictionary - .read() - .unwrap() - .contains(word.to_lowercase().as_str()) - || self.dictionary.check(word) - // self.lookup_cache.read().unwrap().contains(word) || self.dictionary.check(word) + let lower_word = word.to_lowercase(); + if self.custom_dictionary.read().unwrap().contains(&lower_word) { + return true; + } + if self.config.is_allowed_word(&lower_word) { + return true; + } + self.dictionary.check(word) } pub fn add_to_dictionary(&self, strings: &str) { @@ -312,18 +316,25 @@ impl CodeDictionary { #[cfg(test)] mod dictionary_tests { use super::*; - static EXTRA_WORDS: &'static [&'static str] = &["http", "https", "www", "viewport", "UTF"]; fn get_dict() -> CodeDictionary { - let config = Arc::new(CodebookConfig::default()); - let dict = - CodeDictionary::new(config, "./tests/en_index.aff", "./tests/en_index.dic").unwrap(); - for word in EXTRA_WORDS { - dict.add_to_dictionary(word); - } + let mut config = CodebookConfig::new_no_file(); + config.add_word("badword").unwrap(); + let dict = CodeDictionary::new( + Arc::new(config), + "./tests/en_index.aff", + "./tests/en_index.dic", + ) + .unwrap(); dict } + #[test] + fn test_spell_checking_custom_word() { + let processor = get_dict(); + assert!(processor.check("badword")); + } + #[test] fn test_spell_checking() { let processor = get_dict();