Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(linux): Revert works on a line edited, reverted and edited the same way again, and dropping a word names both sides of the change

wquguru6e5c5cb192b2
diff --git a/linux/cicada/core/correction.py b/linux/cicada/core/correction.py
index 2eeb7a3..b8121a3 100644
--- a/linux/cicada/core/correction.py
+++ b/linux/cicada/core/correction.py
@@ -273,9 +273,10 @@ def user_edit(shown: str, edited: str, src: str, fixes: list):
t = next((t for t in tokens if t[0] <= end - 1 < t[1]), None)
if t is not None:
end = t[1]
+ # A dropped word ("in now." → "in.") grows too, so neither side of the fix is empty.
while True:
span = a[start:end]
- if any(c.isalnum() for c in span) and span and a.count(span) == 1:
+ if any(c.isalnum() for c in span) and span and a.count(span) == 1 and b[start:len(b) - (len(a) - end)]:
break
before = [t for t in tokens if t[1] <= start]
after = [t for t in tokens if t[0] >= end]
diff --git a/linux/cicada/core/live_fixes.py b/linux/cicada/core/live_fixes.py
index ab4e698..8680e6c 100644
--- a/linux/cicada/core/live_fixes.py
+++ b/linux/cicada/core/live_fixes.py
@@ -206,7 +206,9 @@ class LiveFixes:
l = self.store.line(sid, line_id)
if l is None or not l.fixes:
return
- k = next((i for i, f in enumerate(l.fixes) if f.from_ == fix.from_ and f.to == fix.to), None)
+ # The one still on: an earlier, already undone copy of the same change may sit before it.
+ same = [i for i, f in enumerate(l.fixes) if f.from_ == fix.from_ and f.to == fix.to]
+ k = next((i for i in same if l.fixes[i].applied), same[0] if same else None)
if k is None:
return
self._remember(sid, [line_id])
@@ -253,6 +255,9 @@ class LiveFixes:
for t in targets:
fixes = []
for f in t.fixes or []:
+ # The same change made again replaces any earlier copy (say, one undone with 改回).
+ if f.from_ == fix.from_ and f.to == fix.to:
+ continue
f = f.copy()
# A guess still waiting on the same words is answered by the edit: not that.
if f.ask is True and (heard in f.from_ or f.from_ in heard):
diff --git a/linux/tests/test_fixes.py b/linux/tests/test_fixes.py
index dc3c547..8dd4549 100644
--- a/linux/tests/test_fixes.py
+++ b/linux/tests/test_fixes.py
@@ -187,6 +187,13 @@ class Correction(unittest.TestCase):
self.assertEqual(correction.user_edit("我木娜找了一个", "我在 Monad 找了一个", "我木娜找了一个", [])[1], "木娜")
self.assertIsNone(correction.user_edit("a", " a ", "a", []))
+ def test_dropped_word_keeps_both_sides_of_the_fix(self):
+ line = "Great, let's lock that in now."
+ fix = correction.user_edit(line, "Great, let's lock that in.", line, [])[0]
+ self.assertTrue(fix.from_ and fix.to)
+ self.assertEqual(apply([fix], line)[0], "Great, let's lock that in.")
+ self.assertTrue(all(correction.label(fix)))
+
def test_label_names_an_added_word_whole(self):
self.assertEqual(correction.label(LineFix("in", "in now", user=True)), ("in", "in now"))
self.assertEqual(correction.label(LineFix("Great", "OK Great", user=True)), ("Great", "OK Great"))
@@ -361,6 +368,17 @@ class Live(Base):
fx.revert(store.line(sid, "l0").fixes[-1], "l0", sid)
self.assertEqual(fx.toast_text(), "已改回 in,这场不再改")
+ def test_edit_revert_edit_revert_goes_back(self):
+ fx, store, sid = self.setup(["Great, let's lock that in.", "Design signs off by Friday."])
+ for _ in range(2):
+ fx.edit("l0", sid, "Great, let's lock that in now.", everywhere=True, remember=True)
+ self.assertEqual(store.line(sid, "l0").shown, "Great, let's lock that in now.")
+ f = next(f for f in store.line(sid, "l0").fixes if f.applied)
+ fx.revert(f, "l0", sid)
+ self.assertEqual(store.line(sid, "l0").shown, "Great, let's lock that in.")
+ self.assertEqual(len(store.line(sid, "l0").fixes), 1)
+ self.assertIsNone(store.line(sid, "l1").fixes)
+
def test_one_off_edit_does_not_teach_the_list(self):
fx, store, sid = self.setup(["我木娜找了一个", "木娜那边也有"])
fx.edit("l0", sid, "我Monad找了一个", everywhere=False, remember=True)