From a85e3ca50df6e13347b5ac6182047f28b802c30b Mon Sep 17 00:00:00 2001
From: Kan-Ru Chen <kanru@kanru.info>
Date: Sun, 14 Jul 2024 08:44:14 +0900
Subject: [PATCH] feat(editor)!: Merge DOWN and SPACE key behavior in
 pagination

---
 src/editor/mod.rs     | 30 ++++--------------------------
 tests/test-bopomofo.c | 10 +++++-----
 2 files changed, 9 insertions(+), 31 deletions(-)

diff --git a/src/editor/mod.rs b/src/editor/mod.rs
index df79162b..ee7b1d19 100644
--- a/src/editor/mod.rs
+++ b/src/editor/mod.rs
@@ -1224,20 +1224,9 @@ impl Selecting {
         }
     }
     fn total_page(&self, editor: &SharedState, dict: &Layered) -> usize {
-        // MSRV: stable after rust 1.73
-        fn div_ceil(lhs: usize, rhs: usize) -> usize {
-            let d = lhs / rhs;
-            let r = lhs % rhs;
-            if r > 0 && rhs > 0 {
-                d + 1
-            } else {
-                d
-            }
-        }
-        div_ceil(
-            self.candidates(editor, dict).len(),
-            editor.options.candidates_per_page,
-        )
+        self.candidates(editor, dict)
+            .len()
+            .div_ceil(editor.options.candidates_per_page)
     }
     fn select(&mut self, editor: &mut SharedState, n: usize) -> Transition {
         let offset = self.page_no * editor.options.candidates_per_page + n;
@@ -1315,7 +1304,7 @@ impl State for Selecting {
                 shared.cancel_selecting();
                 self.start_entering()
             }
-            Space => {
+            Down | Space => {
                 if self.page_no + 1 < self.total_page(shared, &shared.dict) {
                     self.page_no += 1;
                 } else {
@@ -1330,17 +1319,6 @@ impl State for Selecting {
                 }
                 self.spin_absorb()
             }
-            Down => {
-                match &mut self.sel {
-                    Selector::Phrase(sel) => {
-                        sel.next(&shared.dict);
-                        self.page_no = 0;
-                    }
-                    Selector::Symbol(_sel) => (),
-                    Selector::SpecialSymmbol(_sel) => (),
-                }
-                self.spin_absorb()
-            }
             J => {
                 if shared.com.is_empty() {
                     return self.spin_ignore();
diff --git a/tests/test-bopomofo.c b/tests/test-bopomofo.c
index d7d538c9..68e4f087 100644
--- a/tests/test-bopomofo.c
+++ b/tests/test-bopomofo.c
@@ -57,10 +57,10 @@ void test_select_candidate_no_rearward()
     type_keystroke_by_string(ctx, "<D>");       /* ㄧˊㄕㄤˋㄌㄞˊ */
     ok_candidate(ctx, CAND_1, ARRAY_SIZE(CAND_1));
 
-    type_keystroke_by_string(ctx, "<D>");       /* ㄕㄤˋㄌㄞˊ */
+    type_keystroke_by_string(ctx, "<D>");       /* 移上 */
     ok_candidate(ctx, CAND_2, ARRAY_SIZE(CAND_2));
 
-    type_keystroke_by_string(ctx, "<D><D>2<E>");        /* select 移上來 */
+    type_keystroke_by_string(ctx, "<D><L><D>2<E>");        /* select 移上來 */
     ok_commit_buffer(ctx, CAND_1[1]);
 
     chewing_delete(ctx);
@@ -103,7 +103,7 @@ void test_select_candidate_rearward()
     type_keystroke_by_string(ctx, "<D>");       /* ㄕㄤˋㄌㄞˊ */
     ok_candidate(ctx, CAND_2, ARRAY_SIZE(CAND_2));
 
-    type_keystroke_by_string(ctx, "<D><D>2<E>");        /* select 移上來 */
+    type_keystroke_by_string(ctx, "<D><L><D>2<E>");        /* select 移上來 */
     ok_commit_buffer(ctx, CAND_1[1]);
 
     chewing_delete(ctx);
@@ -468,7 +468,7 @@ void test_select_candidate_second_page_rewind()
     chewing_set_candPerPage(ctx, 9);
     chewing_set_spaceAsSelection(ctx, 1);
     chewing_set_phraseChoiceRearward(ctx, 1);
-    type_keystroke_by_string(ctx, "zp zp <D><D><R><D>"); /* ㄈㄣ ㄈㄣ */
+    type_keystroke_by_string(ctx, "zp zp <D><D><R><D><D>"); /* ㄈㄣ ㄈㄣ */
     ok_candidate(ctx, CAND, ARRAY_SIZE(CAND));
 
     chewing_delete(ctx);
@@ -1698,7 +1698,7 @@ void test_KB_HSU_example()
     ok_preedit_buffer(ctx, "一隻隻可愛的小花貓");
     chewing_clean_preedit_buf(ctx);
 
-    type_keystroke_by_string(ctx, "sm sxajdwj<D><D>1xfsxajdgscewfhidxfdwj<D><D>1cd<D><D>1rnd");
+    type_keystroke_by_string(ctx, "sm sxajdwj<D><D>1xfsxajdgscewfhidxfdwj<D><D>1cd<D>1rnd");
     ok_preedit_buffer(ctx, "三歲到五歲的小孩五到十人");
     chewing_clean_preedit_buf(ctx);