mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-22 21:23:02 +01:00
TestFlight build 56 (2ef08aa4): after moving the cursor into the dictated text, Undo dictation did nothing. The chip stayed up for its 12 s after any cursor move, but undoDictation only deleted when the dictation was still right before the cursor, so a tap after a move just hid it. origin/dev (96b4f365) had the same code. Now the keyboard keeps the text either side of the dictation from when it went in (DictationUndo) and finds it again in the host's context around the cursor. When it is shown whole and exactly once, and the text either side still matches, Undo moves the cursor to its end (UTF-16 offsets that stay inside what the host showed), deletes it and puts the cursor back in the text it was moved to. Text views that show only the cursor's sentence leave out the space before it; that space goes too when the sentence starts what they show. When it can't be found (a selection, another sentence, the same words twice, text changed around it) the chip hides as the host reports the change instead of sitting there doing nothing. The unmoved case keeps its old check, now also refused while text is selected (deleteBackward would have taken the selection first) and when what followed it moved. KeyboardTextTarget gains selectedText (nil by default) for this. Verified: iPhone 16 Pro simulator, iOS 27.0, a UITextView host, a real dictation insert through Hex's hosted-keyboard-tour fixture ("Hi Sam." + " See you at ten."), real touches: build 56 left the text unchanged after moving into the dictation or before it; this change restores "Hi Sam." with the cursor at 7 after moving into it, unmoved undo still works, and moving to another sentence (before it, or into later text) hides the chip with nothing deleted. HEXMobile tests on that simulator: 661 run, the new case passes; the 11 cleanup-tagger failures were the unfetched model and pass (78/78) after scripts/fetch-cleanup-model.sh.
68 lines
3.1 KiB
Swift
68 lines
3.1 KiB
Swift
import Foundation
|
|
|
|
/// The dictation Undo dictation can take back, with the field's text either side of it when it
|
|
/// went in, so it can still be found after the cursor moves: within it, before it or after it.
|
|
struct DictationUndo: Equatable {
|
|
let text: String
|
|
/// As much of the field before and after the dictation as the host showed when it went in.
|
|
let before: String
|
|
let after: String
|
|
|
|
/// How to take the dictation back from where the cursor is now.
|
|
struct Plan: Equatable {
|
|
/// UTF-16 units from the cursor to the dictation's end.
|
|
var toEnd: Int
|
|
/// Characters to delete from there.
|
|
var delete: Int
|
|
/// UTF-16 units from where the dictation was back to the cursor's place in the text.
|
|
var back: Int
|
|
}
|
|
|
|
/// Finds the dictation in what the host shows around the cursor (`before` and `after`).
|
|
/// Nil while text is selected, when the host doesn't show all of it (bar the space before
|
|
/// it), when it shows it more than once, or when the text either side no longer matches:
|
|
/// Undo must never delete anything else.
|
|
func plan(before current: String, after following: String, selected: String?) -> Plan? {
|
|
guard selected?.isEmpty ?? true, !text.isEmpty else { return nil }
|
|
let window = Array(current) + Array(following)
|
|
let cursor = current.count
|
|
let dictation = Array(text)
|
|
let n = dictation.count
|
|
var start = 0
|
|
var end = 0
|
|
if let found = Self.only(dictation, in: window) {
|
|
start = found
|
|
end = found + n
|
|
} else {
|
|
// Hosts that show only the cursor's sentence leave out the space before it: the
|
|
// sentence must start what they show.
|
|
let core = Array(text.drop(while: \.isWhitespace))
|
|
guard core.count < n, Self.only(core, in: window) == 0 else { return nil }
|
|
end = core.count
|
|
}
|
|
let shownBefore = String(window[..<start])
|
|
let shownAfter = String(window[end...])
|
|
// Either side may be cut short by the host's window, now or when it went in.
|
|
guard shownBefore.hasSuffix(before) || before.hasSuffix(shownBefore),
|
|
shownAfter.hasPrefix(after) || after.hasPrefix(shownAfter)
|
|
else { return nil }
|
|
let toEnd = end >= cursor
|
|
? String(window[cursor..<end]).utf16.count
|
|
: -String(window[end..<cursor]).utf16.count
|
|
let back = if cursor < start {
|
|
-String(window[cursor..<start]).utf16.count
|
|
} else if cursor > end {
|
|
String(window[end..<cursor]).utf16.count
|
|
} else {
|
|
0
|
|
}
|
|
return Plan(toEnd: toEnd, delete: n, back: back)
|
|
}
|
|
|
|
/// Where `part` starts in `window` when it's there exactly once.
|
|
private static func only(_ part: [Character], in window: [Character]) -> Int? {
|
|
guard !part.isEmpty, window.count >= part.count else { return nil }
|
|
let starts = (0...(window.count - part.count)).filter { window[$0..<($0 + part.count)].elementsEqual(part) }
|
|
return starts.count == 1 ? starts.first : nil
|
|
}
|
|
}
|