Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): printable non A-Z chars should output symbols in pinyin #593

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ if(WITH_RUST)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG be76480232216a64f65e3b1d9794d68cbac6c690 # v0.4.5
GIT_TAG 64289b1d79d6d19cd2e241db515381a086bb8407 # v0.5
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(Corrosion)
Expand Down
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
Loading