Skip to content

Commit 51fb091

Browse files
committed
refactor: improve ergonomics of editor.set_editor_options
1 parent 7d26e72 commit 51fb091

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ What's New in libchewing (unreleased)
4040
- rust: simplified dictionary API.
4141
- rust: logging now depends on tracing and tracing-subscriber.
4242
- rust: the DictionaryMut trait is merged back to the Dictionary trait.
43+
- rust: the Editor::set_editor_options now accepts a closure for setting
44+
options in place.
4345

4446
* Dictionary
4547
- Default symbols.dat now includes commonly used emojis.

capi/src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ pub unsafe extern "C" fn chewing_config_set_int(
570570
_ => return ERROR,
571571
};
572572

573-
ctx.editor.set_editor_options(options);
573+
ctx.editor.set_editor_options(|opt| *opt = options);
574574

575575
OK
576576
}

src/editor/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,15 @@ impl Editor {
304304
pub fn editor_options(&self) -> EditorOptions {
305305
self.shared.options
306306
}
307-
pub fn set_editor_options(&mut self, options: EditorOptions) {
308-
if self.shared.options.language_mode != options.language_mode {
307+
pub fn set_editor_options<F>(&mut self, update_op: F)
308+
where
309+
F: FnOnce(&mut EditorOptions),
310+
{
311+
let old = self.shared.options;
312+
update_op(&mut self.shared.options);
313+
if self.shared.options.language_mode != old.language_mode {
309314
self.cancel_entering_syllable();
310315
}
311-
self.shared.options = options;
312316
}
313317
pub fn entering_syllable(&self) -> bool {
314318
!self.shared.syl.is_empty()
@@ -1598,7 +1602,7 @@ mod tests {
15981602
use crate::{
15991603
conversion::{ChewingEngine, Interval, Symbol},
16001604
dictionary::{Layered, TrieBuf},
1601-
editor::{EditorKeyBehavior, EditorOptions, SymbolSelector, abbrev::AbbrevTable},
1605+
editor::{EditorKeyBehavior, SymbolSelector, abbrev::AbbrevTable},
16021606
input::{
16031607
KeyboardEvent, keycode,
16041608
keymap::{QWERTY_MAP, map_ascii},
@@ -1692,10 +1696,7 @@ mod tests {
16921696
let sym_sel = SymbolSelector::default();
16931697
let mut editor = Editor::new(conversion_engine, dict, estimate, abbrev, sym_sel);
16941698

1695-
editor.set_editor_options(EditorOptions {
1696-
sort_candidates_by_frequency: false,
1697-
..Default::default()
1698-
});
1699+
editor.set_editor_options(|opt| opt.sort_candidates_by_frequency = false);
16991700

17001701
editor.process_keyevent(
17011702
KeyboardEvent::builder()
@@ -1740,10 +1741,7 @@ mod tests {
17401741
let sym_sel = SymbolSelector::default();
17411742
let mut editor = Editor::new(conversion_engine, dict, estimate, abbrev, sym_sel);
17421743

1743-
editor.set_editor_options(EditorOptions {
1744-
sort_candidates_by_frequency: true,
1745-
..Default::default()
1746-
});
1744+
editor.set_editor_options(|opt| opt.sort_candidates_by_frequency = true);
17471745

17481746
editor.process_keyevent(
17491747
KeyboardEvent::builder()

0 commit comments

Comments
 (0)