Skip to content

Commit

Permalink
Add allow word list from config
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Jan 19, 2025
1 parent 528b6f5 commit 3e9a65f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
20 changes: 15 additions & 5 deletions codebook-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
}
}
Expand All @@ -62,6 +64,14 @@ impl CodebookConfig {
Self::find_and_load_config(&current_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
Expand Down Expand Up @@ -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")?;
Expand Down
2 changes: 1 addition & 1 deletion codebook-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 24 additions & 13 deletions codebook/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3e9a65f

Please sign in to comment.