Skip to content

Commit

Permalink
Move to dictionary cache
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Jan 28, 2025
1 parent 19e7922 commit 2605fb7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
6 changes: 3 additions & 3 deletions codebook-lsp/src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl LanguageServer for Backend {
return;
};

let suggestions = self.codebook.dictionary.suggest(word);
let suggestions = self.codebook.get_suggestions(word);
suggestions.iter().for_each(|suggestion| {
actions.push(CodeActionOrCommand::CodeAction(self.make_suggestion(
suggestion,
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Backend {
}
}
fn make_diagnostic(&self, result: &SpellCheckResult, range: &TextRange) -> Diagnostic {
let message = format!("Possible spelling issue: '{}'.", result.word);
let message = format!("Possible spelling issue '{}'.", result.word);
Diagnostic {
range: Range {
start: Position {
Expand Down Expand Up @@ -300,7 +300,7 @@ impl Backend {
Some(lang) => Some(LanguageType::from_str(lang)),
_ => None,
};
let spell_results = self.codebook.dictionary.spell_check(
let spell_results = self.codebook.spell_check(
&doc.text,
lang_type,
Some(file_path.to_str().unwrap_or_default()),
Expand Down
47 changes: 42 additions & 5 deletions codebook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ mod log;
pub mod queries;
mod splitter;

use std::sync::Arc;
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};

use codebook_config::CodebookConfig;
use dictionary::CodeDictionary;
use downloader::DictionaryDownloader;

#[derive(Debug)]
pub struct Codebook {
downloader: DictionaryDownloader,
pub dictionary: CodeDictionary,
// downloader: DictionaryDownloader,
dictionary_cache: Arc<RwLock<HashMap<String, CodeDictionary>>>,
config: Arc<CodebookConfig>,
}

Expand All @@ -23,15 +26,49 @@ impl Codebook {
crate::log::init_logging();
let downloader = DictionaryDownloader::with_cache(&config.cache_dir);
let files = downloader.get("en").unwrap();
let mut dictionary_cache = HashMap::new();
let dictionary = CodeDictionary::new(
Arc::clone(&config),
&files.aff_local_path,
&files.dic_local_path,
)?;
dictionary_cache.insert("en".to_string(), dictionary);
Ok(Self {
downloader,
dictionary,
// downloader,
dictionary_cache: Arc::new(RwLock::new(dictionary_cache)),
config,
})
}

pub fn spell_check(
&self,
text: &str,
language: Option<queries::LanguageType>,
file_path: Option<&str>,
) -> Vec<dictionary::SpellCheckResult> {
self.dictionary_cache
.read()
.unwrap()
.get("en")
.unwrap()
.spell_check(text, language, file_path)
}

pub fn spell_check_file(&self, file_path: &str) -> Vec<dictionary::SpellCheckResult> {
self.dictionary_cache
.read()
.unwrap()
.get("en")
.unwrap()
.spell_check_file(file_path)
}

pub fn get_suggestions(&self, word: &str) -> Vec<String> {
self.dictionary_cache
.read()
.unwrap()
.get("en")
.unwrap()
.suggest(word)
}
}
9 changes: 2 additions & 7 deletions codebook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn main() {
}
"#;

let misspelled =
processor
.dictionary
.spell_check(sample_text, Some(LanguageType::Rust), None);
let misspelled = processor.spell_check(sample_text, Some(LanguageType::Rust), None);
println!("Misspelled words: {:?}", misspelled);
return;
}
Expand All @@ -32,9 +29,7 @@ fn main() {
eprintln!("Can't find file {path:?}");
return;
}
let results = processor
.dictionary
.spell_check_file(path.to_str().unwrap());
let results = processor.spell_check_file(path.to_str().unwrap());
println!("Misspelled words: {:?}", results);
println!("Done");
}

0 comments on commit 2605fb7

Please sign in to comment.