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: Revert works on a line edited, reverted and edited the same way again, and dropping a word names both sides of the change

wquguru3dc4aa11bb01
diff --git a/Sources/Cicada/Correction.swift b/Sources/Cicada/Correction.swift
index de03eaf..bf70c9b 100644
--- a/Sources/Cicada/Correction.swift
+++ b/Sources/Cicada/Correction.swift
@@ -229,13 +229,15 @@ enum Correction {
while let f = shown.range(of: x, range: r) { n += 1; r = f.upperBound..<shown.endIndex }
return n
}
- while !span().contains(where: { $0.isLetter || $0.isNumber }) || count(span()) != 1 {
+ func replacement() -> String { String(b[start..<(b.count - (a.count - end))]) }
+ // A dropped word ("in now." → "in.") grows too, so neither side of the fix is empty.
+ while !span().contains(where: { $0.isLetter || $0.isNumber }) || count(span()) != 1 || replacement().isEmpty {
if let t = tokens.last(where: { $0.upperBound <= start }) { start = t.lowerBound }
else if let t = tokens.first(where: { $0.lowerBound >= end }) { end = t.upperBound }
else { break }
}
let from = span()
- let to = String(b[start..<(b.count - (a.count - end))])
+ let to = replacement()
guard !from.isEmpty, from != to else { return nil }
var heard = from
if !src.contains(from), let earlier = fixes.first(where: { $0.applied && from.contains($0.to) }) {
diff --git a/Sources/Cicada/LiveFixes.swift b/Sources/Cicada/LiveFixes.swift
index b803f12..d9d4831 100644
--- a/Sources/Cicada/LiveFixes.swift
+++ b/Sources/Cicada/LiveFixes.swift
@@ -187,8 +187,10 @@ final class LiveFixes: ObservableObject {
/// 改回: this line goes back to what was heard, and the meeting stops making this change.
func revert(_ fix: LineFix, lineID: String, in id: String) {
+ // The one still on: an earlier, already undone copy of the same change may sit before it.
guard let l = store.session(id)?.lines.first(where: { $0.id == lineID }), var fixes = l.fixes,
- let k = fixes.firstIndex(where: { $0.from == fix.from && $0.to == fix.to }) else { return }
+ let k = fixes.firstIndex(where: { $0.from == fix.from && $0.to == fix.to && $0.applied })
+ ?? fixes.firstIndex(where: { $0.from == fix.from && $0.to == fix.to }) else { return }
remember(id, lines: [lineID])
fixes[k].off = true
fixes[k].ask = nil
@@ -226,7 +228,8 @@ final class LiveFixes: ObservableObject {
remember(id, lines: targets.map(\.id))
for t in targets {
// A guess still waiting on the same words is answered by the edit: not that.
- let fixes = (t.fixes ?? []).map { f -> LineFix in
+ // The same change made again replaces any earlier copy (say, one undone with 改回).
+ let fixes = (t.fixes ?? []).filter { !($0.from == e.fix.from && $0.to == e.fix.to) }.map { f -> LineFix in
var f = f
if f.ask == true, f.from.contains(e.heard) || e.heard.contains(f.from) { f.ask = nil; f.off = true }
return f
diff --git a/Tests/CicadaTests/CorrectionTests.swift b/Tests/CicadaTests/CorrectionTests.swift
index 78ef1ed..364683e 100644
--- a/Tests/CicadaTests/CorrectionTests.swift
+++ b/Tests/CicadaTests/CorrectionTests.swift
@@ -164,4 +164,12 @@ import Testing
#expect(Correction.label(LineFix(from: "Great", to: "OK Great", user: true)) == ("Great", "OK Great"))
#expect(Correction.label(LineFix(from: "解除质压", to: "解除质押")) == ("质压", "质押"))
}
+
+ @Test func droppedWordKeepsBothSidesOfTheFix() {
+ let e = Correction.userEdit(shown: "Great, let's lock that in now.", edited: "Great, let's lock that in.", src: "Great, let's lock that in now.", fixes: [])
+ #expect(e != nil && !e!.fix.from.isEmpty && !e!.fix.to.isEmpty)
+ #expect(Correction.apply([e!.fix], to: "Great, let's lock that in now.").text == "Great, let's lock that in.")
+ let l = Correction.label(e!.fix)
+ #expect(!l.from.isEmpty && !l.to.isEmpty)
+ }
}
diff --git a/Tests/CicadaTests/LiveFixesTests.swift b/Tests/CicadaTests/LiveFixesTests.swift
index 068d3f4..2bdc9f9 100644
--- a/Tests/CicadaTests/LiveFixesTests.swift
+++ b/Tests/CicadaTests/LiveFixesTests.swift
@@ -92,4 +92,17 @@ import Testing
fx.undo()
#expect(line(store, id, "l1").shown == "开布特那边也说了")
}
+
+ @Test func editRevertEditRevertGoesBack() {
+ let (fx, store, id) = setup(["Great, let's lock that in.", "Design signs off by Friday."])
+ for _ in 0..<2 {
+ fx.edit(lineID: "l0", in: id, to: "Great, let's lock that in now.", everywhere: true, remember: true)
+ #expect(line(store, id, "l0").shown == "Great, let's lock that in now.")
+ let f = line(store, id, "l0").fixes!.first { $0.applied }!
+ fx.revert(f, lineID: "l0", in: id)
+ #expect(line(store, id, "l0").shown == "Great, let's lock that in.")
+ }
+ #expect(line(store, id, "l0").fixes!.count == 1)
+ #expect(line(store, id, "l1").fixes == nil)
+ }
}