Skip to content

Commit

Permalink
Merge pull request #624 from chewing/issue6200
Browse files Browse the repository at this point in the history
feat(editor): Make simple conversion engine compatiable with Plain Zhuyin mode
  • Loading branch information
kanru authored Jul 20, 2024
2 parents 73298c4 + 61e8817 commit 613f9ca
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/conversion/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
34 changes: 32 additions & 2 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,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 {
Expand Down Expand Up @@ -1175,9 +1179,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(),
}
Expand Down Expand Up @@ -1211,6 +1224,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,
Expand Down
6 changes: 6 additions & 0 deletions src/editor/selection/phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 18 additions & 18 deletions tests/genkeystroke.c
Original file line number Diff line number Diff line change
Expand Up @@ -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("[英]");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions tests/test-bopomofo.c
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,10 @@ void test_FuzzySearchMode_Hanyu()
void test_SimpleEngine()
{
const TestData SIMPLE_INPUT[] = {
{"ru0320 5j4up ai6g4!<E>", "簡單住因模市!" },
{"ru0320 5j4<D>4up <D>2ai6g4<D><D>2!<E>", "簡單注音模式!" },
{"ru03120 15j41up 1ai61g41!<E>", "簡單住因模市!" },
{"ru03<EE>20 <EE>5j4<EE>up <EE>ai6<EE>g4<EE>!<E>", "簡單住因模市!" },
{"ru03120 15j44up 2ai61g4<D>2!<E>", "簡單注音模式!" },
{"ru03120 15j44up 2ai61g4<D>2!<H>20 1tjp61<E>", "單純簡單注音模式!" },
};
size_t i;
ChewingContext *ctx;
Expand Down

0 comments on commit 613f9ca

Please sign in to comment.