Skip to content

Commit

Permalink
fix(editor): printable non A-Z chars should output symbols in pinyin
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Jul 6, 2024
1 parent b0b5167 commit e95e79e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/editor/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl KeyCode {
_ => None,
}
}
#[rustfmt::skip]
pub const fn is_atoz(&self) -> bool {
matches!(self, A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z)
}
}

use KeyCode::*;
Expand All @@ -248,6 +252,13 @@ pub struct KeyEvent {
pub modifiers: Modifiers,
}

impl KeyEvent {
pub(crate) fn is_printable(&self) -> bool {
// FIXME refactor
self.unicode != '�'
}
}

impl fmt::Display for KeyEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "key-{:?}-{:?}-{}-", self.index, self.code, self.unicode)?;
Expand Down
26 changes: 26 additions & 0 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,32 @@ impl State for Entering {
shared.com.insert(Symbol::from(symbol));
return self.spin_absorb();
}
if ev.is_printable() {
match shared.options.character_form {
CharacterForm::Halfwidth => {
if shared.com.is_empty() {
// FIXME we should ignore these keys if pre-edit is empty
shared.commit_buffer.clear();
shared.commit_buffer.push(ev.unicode);
return self.spin_commit();
} else {
shared.com.insert(Symbol::from(ev.unicode));
return self.spin_absorb();
}
}
CharacterForm::Fullwidth => {
let char_ = full_width_symbol_input(ev.unicode).unwrap();
if shared.com.is_empty() {
shared.commit_buffer.clear();
shared.commit_buffer.push(char_);
return self.spin_commit();
} else {
shared.com.insert(Symbol::from(char_));
return self.spin_absorb();
}
}
}
}
self.spin_bell()
}
LanguageMode::English => match shared.options.character_form {
Expand Down
3 changes: 3 additions & 0 deletions src/editor/zhuyin_layout/pinyin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ impl Pinyin {

impl SyllableEditor for Pinyin {
fn key_press(&mut self, key: KeyEvent) -> KeyBehavior {
if self.key_seq.is_empty() && !key.code.is_atoz() {
return KeyBehavior::KeyError;
}
if ![
KeyCode::Space,
KeyCode::N1,
Expand Down
23 changes: 23 additions & 0 deletions tests/test-bopomofo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,28 @@ void test_KB_HANYU()
chewing_delete(ctx);
}

void test_KB_HANYU_direct_symbol_output()
{
ChewingContext *ctx;

ctx = chewing_new();
start_testcase(ctx, fd);

chewing_set_KBType(ctx, KB_HANYU_PINYIN);

type_keystroke_by_string(ctx, "pin yin 123 mo2shi4");
ok_preedit_buffer(ctx, "拼音 123 模式");
chewing_clean_preedit_buf(ctx);

chewing_set_KBType(ctx, KB_HANYU_PINYIN);
chewing_set_ShapeMode(ctx, FULLSHAPE_MODE);

type_keystroke_by_string(ctx, "pin yin 123 mo2shi4");
ok_preedit_buffer(ctx, "拼音 123 模式");
chewing_clean_preedit_buf(ctx);

chewing_delete(ctx);
}

void test_KB_THL()
{
Expand Down Expand Up @@ -2162,6 +2184,7 @@ void test_KB()
test_KB_COLEMAK_DH_ORTH();

test_KB_HANYU();
test_KB_HANYU_direct_symbol_output();
test_KB_THL();
test_KB_MPS2();
}
Expand Down

0 comments on commit e95e79e

Please sign in to comment.