From fa5b266ae4579f41db5d6a72c6cce970ae40ba63 Mon Sep 17 00:00:00 2001 From: John Kao Date: Fri, 13 Nov 2026 15:30:03 +0800 Subject: [PATCH] Stop the cursor after the first Chinese character Moving left no longer reaches the start of the buffer (or any position before the first Zhuyin syllable), where there is nothing to pick candidates for. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- Sources/AutoZhuyinCore/CompositionEngine.swift | 14 +++++++++----- .../AutoZhuyinCoreTests.swift | 8 +++++--- windows/autozhuyin/core/engine.py | 13 ++++++++----- windows/tests/test_core.py | 16 +++++++++++++--- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cf63c52..9682cae 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 | 空白 | 注音:一聲;音節已完成時送出中文。英文:送出單字加空白 | | Enter | 送出目前組字 | | **Tab** | 判斷錯了?按 Tab 把整段翻轉成另一種語言,輸入法會**記住**這次修正 | -| ← → | 在組字區移動游標(可移到最前面)。游標在中間時:直接打注音會**插入**新字,Backspace 刪掉游標前的字,↓ 選游標前的字 | +| ← → | 在組字區移動游標(最左停在第一個中文字後面)。游標在中間時:直接打注音會**插入**新字,Backspace 刪掉游標前的字,↓ 選游標前的字 | | ↓ | 開啟游標前那個字或詞的候選字(游標預設在最後),數字鍵 1–9 選字,Esc 關閉 | | Esc | 取消組字 | | **單按 Shift** | 沒在打字時:切換**英文模式**,按鍵直接送出、保持小寫、沒有底線,一直維持到再單按一次 Shift 才回到中英自動判斷。打到一半時:同 Tab,整段翻轉 | diff --git a/Sources/AutoZhuyinCore/CompositionEngine.swift b/Sources/AutoZhuyinCore/CompositionEngine.swift index 3b30179..a148fe6 100644 --- a/Sources/AutoZhuyinCore/CompositionEngine.swift +++ b/Sources/AutoZhuyinCore/CompositionEngine.swift @@ -196,15 +196,19 @@ public final class CompositionEngine { ZhuyinParser.parse(Array(keys[seg.range])).syllables.map { seg.range.lowerBound + $0.keyRange.upperBound } } - /// Key indices the cursor can stop at: the start, after every Zhuyin syllable, and at - /// every segment boundary. + /// Key indices the cursor can stop at: after every Zhuyin syllable and at every segment + /// boundary, but never before the first Chinese character (there would be nothing to + /// pick candidates for). private var cursorPositions: [Int] { - var out: Set = [0, keys.count] + var out: Set = [keys.count] + var ends: [Int] = [] for seg in segments { out.insert(seg.range.upperBound) - if seg.mode == .zhuyin { out.formUnion(syllableEnds(seg)) } + if seg.mode == .zhuyin { ends += syllableEnds(seg) } } - return out.sorted() + out.formUnion(ends) + let first = ends.min() ?? keys.count + return out.filter { $0 >= first }.sorted() } /// A place to open candidates: after the `local`-th syllable of Zhuyin segment `segIndex`. diff --git a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift index 3cdca21..ab4e886 100644 --- a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift +++ b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift @@ -384,7 +384,7 @@ final class CursorTests: XCTestCase { _ = type("←", into: e) XCTAssertEqual(e.cursorOffset, 3) _ = type("←←←←", into: e) - XCTAssertEqual(e.cursorOffset, 0) // stops at the start + XCTAssertEqual(e.cursorOffset, 1) // stops after the first character _ = type("→→→→", into: e) XCTAssertEqual(e.cursorOffset, 4) } @@ -419,8 +419,10 @@ final class CursorTests: XCTestCase { XCTAssertEqual(type("\n", into: e), "今天氣") } - func testInsertAtStart() { - XCTAssertEqual(type("su3cl3←←ji3\n", into: engine()), "我你好") + func testCursorStopsAfterFirstCharacter() { + let e = engine() + _ = type("su3cl3←←←", into: e) + XCTAssertEqual(e.cursorOffset, 1) } func testBackspaceDeletesCharacterBeforeCursor() { diff --git a/windows/autozhuyin/core/engine.py b/windows/autozhuyin/core/engine.py index b16de96..da77237 100644 --- a/windows/autozhuyin/core/engine.py +++ b/windows/autozhuyin/core/engine.py @@ -175,14 +175,17 @@ class CompositionEngine: return [seg[0] + ps.end for ps in bopomofo.parse(self.keys[seg[0]:seg[1]]).syllables] def _cursor_positions(self): - """Key indices the cursor can stop at: the start, after every Zhuyin syllable, - and at every segment boundary.""" - out = {0, len(self.keys)} + """Key indices the cursor can stop at: after every Zhuyin syllable and at every + segment boundary, but never before the first Chinese character (there would be + nothing to pick candidates for).""" + out, ends = {len(self.keys)}, [] for seg in self.segments: out.add(seg[1]) if seg[2] == ZHUYIN: - out.update(self._syllable_ends(seg)) - return sorted(out) + ends += self._syllable_ends(seg) + out.update(ends) + first = min(ends) if ends else len(self.keys) + return sorted(p for p in out if p >= first) def _stop_at(self, pos): """(segment index, syllables before pos) for the Zhuyin segment pos is in.""" diff --git a/windows/tests/test_core.py b/windows/tests/test_core.py index 3687def..5a3f7f4 100644 --- a/windows/tests/test_core.py +++ b/windows/tests/test_core.py @@ -247,7 +247,7 @@ class CursorTests(unittest.TestCase): type_keys("←", e) self.assertEqual(e.cursor_offset, 3) type_keys("←←←←", e) - self.assertEqual(e.cursor_offset, 0) + self.assertEqual(e.cursor_offset, 1) # stops after the first character type_keys("→→→→", e) self.assertEqual(e.cursor_offset, 4) @@ -278,8 +278,18 @@ class InsertInMiddleTests(unittest.TestCase): self.assertEqual(e.cursor_offset, 2) self.assertEqual(type_keys("\n", e), "今天氣") - def test_insert_at_start(self): - self.assertEqual(type_keys("su3cl3←←ji3\n", make_engine()), "我你好") + def test_cursor_stops_after_first_character(self): + e = make_engine() + type_keys("su3cl3←←←", e) + self.assertEqual(e.cursor_offset, 1) + type_keys("ji3\n", e) + self.assertEqual(e.marked_text, "") + + def test_no_stop_before_first_chinese(self): + e = make_engine() + type_keys("hello ji3←←", e) # "hello " held, then 我: nothing to pick before 我 + type_keys("↓", e) + self.assertTrue(e.showing_candidates) def test_backspace_deletes_character_before_cursor(self): e = make_engine()