From 571ccbf8f978cf0c6aac61e8e59cc2190dc32df1 Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sat, 20 Jul 2024 19:19:59 +0900 Subject: [PATCH 1/2] feat(editor): Make simple conversion engine compatiable with Plain Zhuyin mode --- capi/data/mini.dat | Bin 91031 -> 91033 bytes capi/src/io.rs | 2 +- src/editor/mod.rs | 34 +++++++++++++++++++++++++++++-- src/editor/selection/phrase.rs | 6 ++++++ tests/genkeystroke.c | 36 ++++++++++++++++----------------- tests/test-bopomofo.c | 5 +++-- 6 files changed, 60 insertions(+), 23 deletions(-) diff --git a/capi/data/mini.dat b/capi/data/mini.dat index baeb678f49ebfc2926beaed0074ec414189e3017..d89a041c1322fa7e8d2716940cc6de0003cf31f7 100644 GIT binary patch delta 67 zcmbP!oOR}LRsn-%#^fnHEY2RT;Y^GS26+<&ycM|&^epuZbc>Sp40*(oGg8Yl^U`&b Qb21fR;*C{Xs~BaI0eICC_W%F@ delta 65 zcmbPvoOSwfRsn-%#^gynEY2RT;Y^GS200T2yyaO9^epsDc_foFQp+>*(sh$_G8GK; VEcFa@6LShO5*ur_)-cK@0{~eB62|}l diff --git a/capi/src/io.rs b/capi/src/io.rs index 3a3e242b..ecf1b705 100644 --- a/capi/src/io.rs +++ b/capi/src/io.rs @@ -475,7 +475,7 @@ pub unsafe extern "C" fn chewing_config_set_int( ctx.editor .set_conversion_engine(Box::new(FuzzyChewingEngine::new())); options.lookup_strategy = LookupStrategy::FuzzyPartialPrefix; - ConversionEngineKind::ChewingEngine + ConversionEngineKind::FuzzyChewingEngine } _ => return ERROR, } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 9d611220..2321f3e7 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -1129,6 +1129,10 @@ impl EnteringSyllable { None => self.spin_ignore(), } } + fn start_selecting_simple_engine(&self, editor: &mut SharedState) -> Transition { + editor.syl.clear(); + Transition::ToState(Box::new(Selecting::new_phrase_for_simple_engine(editor))) + } } impl State for EnteringSyllable { @@ -1184,9 +1188,18 @@ impl State for EnteringSyllable { .is_some() { shared.com.insert(Symbol::from(shared.syl.read())); + shared.syl.clear(); + if shared.options.conversion_engine + == ConversionEngineKind::SimpleEngine + { + self.start_selecting_simple_engine(shared) + } else { + self.start_entering() + } + } else { + shared.syl.clear(); + self.start_entering() } - shared.syl.clear(); - self.start_entering() } _ => self.spin_bell(), } @@ -1220,6 +1233,23 @@ impl Selecting { sel: Selector::Phrase(sel), } } + fn new_phrase_for_simple_engine(editor: &mut SharedState) -> Self { + editor.com.push_cursor(); + // editor.com.clamp_cursor(); + + let mut sel = PhraseSelector::new( + false, + editor.options.lookup_strategy, + editor.com.to_composition(), + ); + sel.init_single_word(editor.cursor()); + + Selecting { + page_no: 0, + action: SelectingAction::Replace, + sel: Selector::Phrase(sel), + } + } fn new_symbol(editor: &mut SharedState) -> Self { Selecting { page_no: 0, diff --git a/src/editor/selection/phrase.rs b/src/editor/selection/phrase.rs index 6242e27e..9632ed9a 100644 --- a/src/editor/selection/phrase.rs +++ b/src/editor/selection/phrase.rs @@ -65,6 +65,12 @@ impl PhraseSelector { } } + pub(crate) fn init_single_word(&mut self, cursor: usize) { + self.orig = cursor; + self.end = min(cursor, self.com.len()); + self.begin = self.end - 1; + } + pub(crate) fn begin(&self) -> usize { self.begin } diff --git a/tests/genkeystroke.c b/tests/genkeystroke.c index fbcb6d81..b9d06e7c 100644 --- a/tests/genkeystroke.c +++ b/tests/genkeystroke.c @@ -145,10 +145,16 @@ void show_interval_buffer(int x, int y, ChewingContext *ctx) void showBopomofo(ChewingContext *ctx) { if (chewing_get_ChiEngMode(ctx)) { - if (chewing_config_get_int(ctx, "chewing.fuzzy_search_mode")) { - addstr("[糊]"); - } else { - addstr("[中]"); + switch (chewing_config_get_int(ctx, "chewing.conversion_engine")) { + case 0: + addstr("[ㄅ]"); + break; + case 1: + addstr("[中]"); + break; + case 2: + addstr("[糊]"); + break; } } else { addstr("[英]"); @@ -283,6 +289,7 @@ int main(int argc, char *argv[]) int ch; int add_phrase_length; int kbtype; + int conversion_engine; if (argc < 2) { fprintf(stderr, "usage: genkeystroke filename\n"); @@ -352,11 +359,10 @@ int main(int argc, char *argv[]) mvaddstr(9, 20, "Ctrl + b : toggle Eng/Chi mode"); mvaddstr(10, 0, "F1, F2, F3, ..., F9 : Add user defined phrase"); mvaddstr(11, 0, "Ctrl + h : toggle Full/Half shape mode"); - mvaddstr(12, 0, "Ctrl + f : toggle Fuzzy Search mode"); - mvaddstr(13, 0, "Ctrl + s : toggle Simple mode"); - mvaddstr(14, 0, "Ctrl + n/p : Next / Previous keyboard layout"); + mvaddstr(12, 0, "Ctrl + s : cycle Simple/Chewing/Fuzzy mode"); + mvaddstr(13, 0, "Ctrl + n/p : Next / Previous keyboard layout"); show_commit_string(14, 0, ctx); - show_userphrase(7, 14, ctx); + show_userphrase(7, 15, ctx); show_edit_buffer(1, 0, ctx); ch = getch(); @@ -455,17 +461,11 @@ int main(int argc, char *argv[]) break; case KEY_CTRL_('D'): goto end; - case KEY_CTRL_('F'): - if (chewing_config_get_int(ctx, "chewing.fuzzy_search_mode") == TRUE) - chewing_config_set_int(ctx, "chewing.fuzzy_search_mode", FALSE); - else - chewing_config_set_int(ctx, "chewing.fuzzy_search_mode", TRUE); - break; case KEY_CTRL_('S'): - if (chewing_config_get_int(ctx, "chewing.conversion_engine") == 1) - chewing_config_set_int(ctx, "chewing.conversion_engine", 0); - else - chewing_config_set_int(ctx, "chewing.conversion_engine", 1); + conversion_engine = chewing_config_get_int(ctx, "chewing.conversion_engine"); + conversion_engine += 1; + conversion_engine %= 3; + chewing_config_set_int(ctx, "chewing.conversion_engine", conversion_engine); break; case KEY_CTRL_('H'): /* emulate Shift+Space */ chewing_handle_ShiftSpace(ctx); diff --git a/tests/test-bopomofo.c b/tests/test-bopomofo.c index daabf0ff..229cc5e2 100644 --- a/tests/test-bopomofo.c +++ b/tests/test-bopomofo.c @@ -1405,8 +1405,9 @@ void test_FuzzySearchMode_Hanyu() void test_SimpleEngine() { const TestData SIMPLE_INPUT[] = { - {"ru0320 5j4up ai6g4!", "簡單住因模市!" }, - {"ru0320 5j44up 2ai6g42!", "簡單注音模式!" }, + {"ru03120 15j41up 1ai61g41!", "簡單住因模市!" }, + {"ru03120 15j44up 2ai61g42!", "簡單注音模式!" }, + {"ru03120 15j44up 2ai61g42!20 1tjp61", "單純簡單注音模式!" }, }; size_t i; ChewingContext *ctx; From 61e8817e1c02fdfb1d530af7e44993dc478eaf5d Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sat, 20 Jul 2024 21:33:21 +0900 Subject: [PATCH 2/2] test: add more tests for simple engine --- src/conversion/simple.rs | 2 +- tests/test-bopomofo.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/conversion/simple.rs b/src/conversion/simple.rs index 375c06d1..62bf00cc 100644 --- a/src/conversion/simple.rs +++ b/src/conversion/simple.rs @@ -21,7 +21,7 @@ impl SimpleEngine { for (i, sym) in comp.symbols().iter().enumerate() { if comp - .selections + .selections() .iter() .any(|selection| selection.intersect_range(i, i + 1)) { diff --git a/tests/test-bopomofo.c b/tests/test-bopomofo.c index 229cc5e2..a3772e48 100644 --- a/tests/test-bopomofo.c +++ b/tests/test-bopomofo.c @@ -1406,6 +1406,7 @@ void test_SimpleEngine() { const TestData SIMPLE_INPUT[] = { {"ru03120 15j41up 1ai61g41!", "簡單住因模市!" }, + {"ru0320 5j4up ai6g4!", "簡單住因模市!" }, {"ru03120 15j44up 2ai61g42!", "簡單注音模式!" }, {"ru03120 15j44up 2ai61g42!20 1tjp61", "單純簡單注音模式!" }, };