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 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 15:30:03 +08:00
co-authored by Claude Opus 5.5
parent 6448bf4ca2
commit fa5b266ae4
5 changed files with 36 additions and 17 deletions
+8 -5
View File
@@ -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."""
+13 -3
View File
@@ -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()