@@ -3,7 +3,10 @@ mod queries;
33mod splitter;
44use lru:: LruCache ;
55
6- use crate :: queries:: { get_language_name_from_filename, get_language_setting, LanguageSetting } ;
6+ use crate :: queries:: {
7+ get_language_name_from_filename, get_language_setting, LanguageSetting , LanguageType ,
8+ COMMON_DICTIONARY ,
9+ } ;
710use std:: {
811 collections:: { HashMap , HashSet } ,
912 num:: NonZeroUsize ,
@@ -39,9 +42,9 @@ pub struct TextRange {
3942
4043#[ derive( Debug ) ]
4144pub struct CodeDictionary {
42- custom_dictionary : HashSet < String > ,
45+ custom_dictionary : Arc < RwLock < HashSet < String > > > ,
4346 dictionary : spellbook:: Dictionary ,
44- dictionary_lookup_cache : Arc < RwLock < LruCache < String , Vec < String > > > > ,
47+ suggestion_cache : Arc < RwLock < LruCache < String , Vec < String > > > > ,
4548}
4649
4750impl CodeDictionary {
@@ -50,29 +53,41 @@ impl CodeDictionary {
5053 let dic = std:: fs:: read_to_string ( dic_path) ?;
5154 let dict = spellbook:: Dictionary :: new ( & aff, & dic)
5255 . map_err ( |e| format ! ( "Dictionary parse error: {}" , e) ) ?;
53-
56+ let mut custom_dictionary: HashSet < String > = HashSet :: new ( ) ;
57+ for word in COMMON_DICTIONARY . lines ( ) {
58+ custom_dictionary. insert ( word. to_string ( ) ) ;
59+ }
5460 Ok ( CodeDictionary {
55- custom_dictionary : HashSet :: new ( ) ,
61+ custom_dictionary : Arc :: new ( RwLock :: new ( custom_dictionary ) ) ,
5662 dictionary : dict,
57- dictionary_lookup_cache : Arc :: new ( RwLock :: new ( LruCache :: new (
63+ suggestion_cache : Arc :: new ( RwLock :: new ( LruCache :: new (
5864 NonZeroUsize :: new ( 10000 ) . unwrap ( ) ,
5965 ) ) ) ,
6066 } )
6167 }
6268
6369 pub fn check ( & self , word : & str ) -> bool {
64- self . custom_dictionary . contains ( word) || self . dictionary . check ( word)
65- // self.dictionary_lookup_cache.read().unwrap().contains(word) || self.dictionary.check(word)
70+ self . custom_dictionary
71+ . read ( )
72+ . unwrap ( )
73+ . contains ( word. to_lowercase ( ) . as_str ( ) )
74+ || self . dictionary . check ( word)
75+ // self.lookup_cache.read().unwrap().contains(word) || self.dictionary.check(word)
6676 }
6777
68- pub fn add_to_dictionary ( & mut self , word : String ) {
69- self . custom_dictionary . insert ( word) ;
78+ pub fn add_to_dictionary ( & self , strings : & str ) {
79+ for line in strings. lines ( ) {
80+ self . custom_dictionary
81+ . write ( )
82+ . unwrap ( )
83+ . insert ( line. to_string ( ) ) ;
84+ }
7085 }
7186
7287 pub fn suggest ( & self , word : & str ) -> Vec < String > {
7388 println ! ( "Checking Cache: {:?}" , word) ;
7489 // First try to get from cache with write lock since get() needs to modify LRU order
75- if let Some ( suggestions) = self . dictionary_lookup_cache . write ( ) . unwrap ( ) . get_mut ( word) {
90+ if let Some ( suggestions) = self . suggestion_cache . write ( ) . unwrap ( ) . get_mut ( word) {
7691 println ! ( "Cache hit for {:?}" , word) ;
7792 return suggestions. clone ( ) ;
7893 }
@@ -81,7 +96,7 @@ impl CodeDictionary {
8196 let mut suggestions = Vec :: new ( ) ;
8297 self . dictionary . suggest ( word, & mut suggestions) ;
8398 if !suggestions. is_empty ( ) {
84- self . dictionary_lookup_cache
99+ self . suggestion_cache
85100 . write ( )
86101 . unwrap ( )
87102 . put ( word. to_string ( ) , suggestions. clone ( ) ) ;
@@ -90,27 +105,41 @@ impl CodeDictionary {
90105 }
91106
92107 pub fn spell_check ( & self , text : & str , language : & str ) -> Vec < SpellCheckResult > {
93- // print!("language: {:?}", language);
94- let lang = get_language_setting ( language) ;
95- match lang {
96- None => {
97- return self . spell_check_text ( text) ;
98- }
99- Some ( lang) => {
100- return self . spell_check_code ( text, lang) ;
101- }
102- }
108+ let lang_type = LanguageType :: from_str ( language) ;
109+ return self . spell_check_enum ( text, lang_type) ;
103110 }
104111
105112 pub fn spell_check_file ( & self , path : & str ) -> Vec < SpellCheckResult > {
106- let lang_name = get_language_name_from_filename ( path) ;
113+ let lang_type = get_language_name_from_filename ( path) ;
107114 let file_text = std:: fs:: read_to_string ( path) . unwrap ( ) ;
108- return self . spell_check ( & file_text, & lang_name ) ;
115+ return self . spell_check_enum ( & file_text, lang_type ) ;
109116 }
110117
111118 pub fn spell_check_file_memory ( & self , path : & str , contents : & str ) -> Vec < SpellCheckResult > {
112- let lang_name = get_language_name_from_filename ( path) ;
113- return self . spell_check ( & contents, & lang_name) ;
119+ let lang_type = get_language_name_from_filename ( path) ;
120+ return self . spell_check_enum ( & contents, lang_type) ;
121+ }
122+
123+ fn spell_check_enum (
124+ & self ,
125+ text : & str ,
126+ language_type : Option < LanguageType > ,
127+ ) -> Vec < SpellCheckResult > {
128+ let language = match language_type {
129+ None => None ,
130+ Some ( lang) => get_language_setting ( lang) ,
131+ } ;
132+ match language {
133+ None => {
134+ return self . spell_check_text ( text) ;
135+ }
136+ Some ( lang) => {
137+ // if let Some(dictionary) = lang.language_dictionary {
138+ // self.add_to_dictionary(dictionary);
139+ // }
140+ return self . spell_check_code ( text, lang) ;
141+ }
142+ }
114143 }
115144
116145 fn spell_check_text ( & self , text : & str ) -> Vec < SpellCheckResult > {
@@ -301,7 +330,7 @@ mod lib_tests {
301330 let mut cdict =
302331 CodeDictionary :: new ( "./tests/en_index.aff" , "./tests/en_index.dic" ) . unwrap ( ) ;
303332 for word in EXTRA_WORDS {
304- cdict. add_to_dictionary ( word. to_string ( ) ) ;
333+ cdict. add_to_dictionary ( word) ;
305334 }
306335 cdict
307336 }
@@ -311,7 +340,7 @@ mod lib_tests {
311340 let processor = get_processor ( ) ;
312341
313342 let text = "HelloWorld calc_wrld" ;
314- let misspelled = processor. spell_check ( text, "text" ) ;
343+ let misspelled = processor. spell_check_enum ( text, None ) ;
315344 println ! ( "{:?}" , misspelled) ;
316345 assert ! ( misspelled. iter( ) . any( |r| r. word == "wrld" ) ) ;
317346 }
0 commit comments