Skip to content

Commit e95e79e

Browse files
committed
fix(editor): printable non A-Z chars should output symbols in pinyin
1 parent b0b5167 commit e95e79e

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

src/editor/keyboard/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ impl KeyCode {
230230
_ => None,
231231
}
232232
}
233+
#[rustfmt::skip]
234+
pub const fn is_atoz(&self) -> bool {
235+
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)
236+
}
233237
}
234238

235239
use KeyCode::*;
@@ -248,6 +252,13 @@ pub struct KeyEvent {
248252
pub modifiers: Modifiers,
249253
}
250254

255+
impl KeyEvent {
256+
pub(crate) fn is_printable(&self) -> bool {
257+
// FIXME refactor
258+
self.unicode != '�'
259+
}
260+
}
261+
251262
impl fmt::Display for KeyEvent {
252263
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253264
write!(f, "key-{:?}-{:?}-{}-", self.index, self.code, self.unicode)?;

src/editor/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,32 @@ impl State for Entering {
10271027
shared.com.insert(Symbol::from(symbol));
10281028
return self.spin_absorb();
10291029
}
1030+
if ev.is_printable() {
1031+
match shared.options.character_form {
1032+
CharacterForm::Halfwidth => {
1033+
if shared.com.is_empty() {
1034+
// FIXME we should ignore these keys if pre-edit is empty
1035+
shared.commit_buffer.clear();
1036+
shared.commit_buffer.push(ev.unicode);
1037+
return self.spin_commit();
1038+
} else {
1039+
shared.com.insert(Symbol::from(ev.unicode));
1040+
return self.spin_absorb();
1041+
}
1042+
}
1043+
CharacterForm::Fullwidth => {
1044+
let char_ = full_width_symbol_input(ev.unicode).unwrap();
1045+
if shared.com.is_empty() {
1046+
shared.commit_buffer.clear();
1047+
shared.commit_buffer.push(char_);
1048+
return self.spin_commit();
1049+
} else {
1050+
shared.com.insert(Symbol::from(char_));
1051+
return self.spin_absorb();
1052+
}
1053+
}
1054+
}
1055+
}
10301056
self.spin_bell()
10311057
}
10321058
LanguageMode::English => match shared.options.character_form {

src/editor/zhuyin_layout/pinyin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ impl Pinyin {
7171

7272
impl SyllableEditor for Pinyin {
7373
fn key_press(&mut self, key: KeyEvent) -> KeyBehavior {
74+
if self.key_seq.is_empty() && !key.code.is_atoz() {
75+
return KeyBehavior::KeyError;
76+
}
7477
if ![
7578
KeyCode::Space,
7679
KeyCode::N1,

tests/test-bopomofo.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,28 @@ void test_KB_HANYU()
20182018
chewing_delete(ctx);
20192019
}
20202020

2021+
void test_KB_HANYU_direct_symbol_output()
2022+
{
2023+
ChewingContext *ctx;
2024+
2025+
ctx = chewing_new();
2026+
start_testcase(ctx, fd);
2027+
2028+
chewing_set_KBType(ctx, KB_HANYU_PINYIN);
2029+
2030+
type_keystroke_by_string(ctx, "pin yin 123 mo2shi4");
2031+
ok_preedit_buffer(ctx, "拼音 123 模式");
2032+
chewing_clean_preedit_buf(ctx);
2033+
2034+
chewing_set_KBType(ctx, KB_HANYU_PINYIN);
2035+
chewing_set_ShapeMode(ctx, FULLSHAPE_MODE);
2036+
2037+
type_keystroke_by_string(ctx, "pin yin 123 mo2shi4");
2038+
ok_preedit_buffer(ctx, "拼音 123 模式");
2039+
chewing_clean_preedit_buf(ctx);
2040+
2041+
chewing_delete(ctx);
2042+
}
20212043

20222044
void test_KB_THL()
20232045
{
@@ -2162,6 +2184,7 @@ void test_KB()
21622184
test_KB_COLEMAK_DH_ORTH();
21632185

21642186
test_KB_HANYU();
2187+
test_KB_HANYU_direct_symbol_output();
21652188
test_KB_THL();
21662189
test_KB_MPS2();
21672190
}

0 commit comments

Comments
 (0)