diff --git a/capi/data/mini.dat b/capi/data/mini.dat index 971d63ce..ac09a039 100644 Binary files a/capi/data/mini.dat and b/capi/data/mini.dat differ diff --git a/capi/src/io.rs b/capi/src/io.rs index b1c6be48..38d056a9 100644 --- a/capi/src/io.rs +++ b/capi/src/io.rs @@ -128,6 +128,7 @@ pub unsafe extern "C" fn chewing_new2( vec![Box::new(builtin.unwrap()) as Box] } }; + let extra_dicts = sys_loader.load_extra().unwrap_or_default(); let abbrev = sys_loader.load_abbrev(); let abbrev = match abbrev { Ok(abbr) => abbr, @@ -166,7 +167,8 @@ pub unsafe extern "C" fn chewing_new2( let estimate = LaxUserFreqEstimate::max_from(user_dictionary.as_ref()); - let dict = Layered::new(dictionaries, user_dictionary); + let system_dicts = Vec::from_iter(dictionaries.into_iter().chain(extra_dicts.into_iter())); + let dict = Layered::new(system_dicts, user_dictionary); let conversion_engine = Box::new(ChewingEngine::new()); let kb_compat = KeyboardLayoutCompat::Default; let keyboard = AnyKeyboardLayout::Qwerty(Qwerty); diff --git a/src/dictionary/loader.rs b/src/dictionary/loader.rs index 2de0a7cf..380920c8 100644 --- a/src/dictionary/loader.rs +++ b/src/dictionary/loader.rs @@ -88,7 +88,17 @@ impl SystemDictionaryLoader { let tsi_dict = Trie::open(tsi_dict_path).map_err(io_err)?; results.push(Box::new(tsi_dict)); + Ok(results) + } + /// Searches and loads the extra dictionaries. + pub fn load_extra(&self) -> Result>, LoadDictionaryError> { + let search_path = if let Some(sys_path) = &self.sys_path { + sys_path.to_owned() + } else { + sys_path_from_env_var() + }; let extra_files = find_extra_dat_by_path(&search_path); + let mut results: Vec> = vec![]; for path in extra_files { info!("Loading {}", path.display()); match Trie::open(&path) { diff --git a/src/editor/mod.rs b/src/editor/mod.rs index be967f1a..9d5b0ae7 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -191,13 +191,16 @@ pub(crate) struct SharedState { impl Editor { pub fn chewing() -> Result> { - let system_dict = SystemDictionaryLoader::new().load()?; + let sys_loader = SystemDictionaryLoader::new(); + let base_dict = sys_loader.load()?; + let extra_dict = sys_loader.load_extra()?; + let system_dict = Vec::from_iter(base_dict.into_iter().chain(extra_dict.into_iter())); let user_dict = UserDictionaryLoader::new().load()?; let estimate = LaxUserFreqEstimate::max_from(user_dict.as_ref()); let dict = Layered::new(system_dict, user_dict); let conversion_engine = Box::new(ChewingEngine::new()); - let abbrev = SystemDictionaryLoader::new().load_abbrev()?; - let sym_sel = SystemDictionaryLoader::new().load_symbol_selector()?; + let abbrev = sys_loader.load_abbrev()?; + let sym_sel = sys_loader.load_symbol_selector()?; let editor = Editor::new(conversion_engine, dict, estimate, abbrev, sym_sel); Ok(editor) } diff --git a/tools/src/info.rs b/tools/src/info.rs index c27c28f5..2a7b8cfb 100644 --- a/tools/src/info.rs +++ b/tools/src/info.rs @@ -4,20 +4,35 @@ use chewing::dictionary::{Dictionary, SystemDictionaryLoader, UserDictionaryLoad use crate::flags; pub(crate) fn run(args: flags::Info) -> Result<()> { - let mut dictionaries = vec![]; - if args.user { - dictionaries.push(UserDictionaryLoader::new().load()?); - } if args.system { - dictionaries.extend(SystemDictionaryLoader::new().load()?); + let dictionaries = SystemDictionaryLoader::new().load()?; + if args.json { + print_json_info(&dictionaries, "base"); + } else { + print_info(&dictionaries, "base"); + } + let extra = SystemDictionaryLoader::new().load_extra()?; + if args.json { + print_json_info(&extra, "extra"); + } else { + print_info(&extra, "extra"); + } } - if let Some(path) = args.path { - dictionaries.push(UserDictionaryLoader::new().userphrase_path(path).load()?); + if args.user { + let dict = UserDictionaryLoader::new().load()?; + if args.json { + print_json_info(&[dict], "user"); + } else { + print_info(&[dict], "user"); + } } - if args.json { - print_json_info(&dictionaries); - } else { - print_info(&dictionaries); + if let Some(path) = args.path { + let dict = UserDictionaryLoader::new().userphrase_path(path).load()?; + if args.json { + print_json_info(&[dict], "input"); + } else { + print_info(&[dict], "input"); + } } Ok(()) } @@ -39,7 +54,7 @@ fn escape_json(str: String) -> String { out } -fn print_json_info(dictionaries: &[Box]) { +fn print_json_info(dictionaries: &[Box], from: &str) { let mut iter = dictionaries.iter().peekable(); println!("["); while let Some(dict) = iter.next() { @@ -49,6 +64,7 @@ fn print_json_info(dictionaries: &[Box]) { .unwrap_or(String::new()); let info = dict.about(); println!(" {{"); + println!(r#" "from": "{from}","#); println!(r#" "path": "{}","#, escape_json(path)); println!(r#" "name": "{}","#, escape_json(info.name)); println!(r#" "version": "{}","#, escape_json(info.version)); @@ -60,7 +76,7 @@ fn print_json_info(dictionaries: &[Box]) { println!("]"); } -fn print_info(dictionaries: &[Box]) { +fn print_info(dictionaries: &[Box], from: &str) { for dict in dictionaries { let path = dict .path() @@ -68,6 +84,7 @@ fn print_info(dictionaries: &[Box]) { .unwrap_or(String::new()); let info = dict.about(); println!("---"); + println!("From : {from}"); println!("Path : {}", path); println!("Name : {}", info.name); println!("Version : {}", info.version);