hex/ios/HEXShared/FullKeyboard/FullKeyboardEngine+Revert.swift
Ryan Vogel 14ed855dfe iOS: no double space when you fix a word mid-sentence
TestFlight: "Tapping on a word to select it, and then quickly typing in a
correction doesn't account for the trailing space and you end up with 2
spaces inserted." The engine never looked at the text after the cursor, so
a space after a word (typed, autocorrected, after a swipe) or a picked
suggestion's own space doubled the space already there ("black  fox"), or
went in before closing punctuation ("jumped .").

What changed (new FullKeyboardEngine+SmartSpace.swift):
- When the text after the cursor starts with a space (not a line break) or
  a closing mark (. , ! ? ; : ) ] } quotes, apostrophe, », …), the space
  after a word is held instead of typed. A picked suggestion and the space
  bar (corrected or not, after a typed or swiped word) both hold it.
- A held space goes in only if the next key starts another word ("black",
  space, "cat" reads "black cat fox"), like a swiped word's pending space;
  a mark goes against the word ("black, fox"); a second space is swallowed;
  moving the cursor drops it. After a space (a gap you made) the space bar
  types a space as before.
- Delete right after a held space takes it back and deletes nothing, and the
  strip offers the autocorrection's revert only then, so #2192's
  delete-after-autocorrect revert still works mid-sentence.
- French keeps its space before ? ! ; : and » (FrenchSpacing): those don't
  count as closing marks with a French layout. Smart quotes are unchanged
  (the text after the cursor is read through SmartPunctuation.plain).
- Dictation already handled this (TranscriptFormatter.insertionText); now
  covered by a test.

Not Apple's behaviour exactly (black box, iOS 27.2 sim, UITextField): Apple
types the second space after a typed word, corrected or not, and a picked
suggestion steps over the space after the cursor, so a word typed next joins
the following one ("black xfox"). Holding the space reads right both ways and
needs no cursor moves, whose context the host reports late.

FullKeyboardEngine.swift stays at the 1,500-line cap: suggestionSpaceBefore,
insert and spaceAfterSwipedWord lose private so the new file can use them.

Verified: reproduced on an iOS 27.2 simulator (KBHost text field, word
selected by double tap / Select): origin/dev typed "The quick black  fox
jumps" for select-type-space, select-typo-space (autocorrect) and
select-type-pick; this branch types "The quick black fox jumps" for all
three, "black cat fox" when a word follows, "black, fox" for a comma, and
delete then the strip's revert gives "blsck fox". xcodebuild test: 624 tests,
0 failures (1 skipped, Apple Speech), including 5 new FullKeyboardEngineTests
cases.
2026-11-21 12:34:26 -05:00

60 lines
3.2 KiB
Swift

import UIKit
/// Delete after an autocorrection, as on Apple's keyboard (black box, iOS 27.2): delete takes
/// only the space or mark after the corrected words, and Apple's text view then shows what you
/// typed in a bubble with an undo arrow. A keyboard can't draw in the text, so the strip's left
/// slot offers it instead, highlighted with the same arrow (`KeyboardSuggestion.Kind.revert`);
/// tapping it puts back what you typed, without the space, and space after that keeps it. The
/// offer comes back whenever the cursor is at the end of the latest autocorrection again
/// (deleting back to it), as Apple's bubble does, and goes once a newer one replaces it.
@MainActor
extension FullKeyboardEngine {
/// `suggestions` with the revert in the left slot, in place of the word as it reads (which
/// space keeps anyway), while the cursor is right after the latest autocorrection.
func offeringRevert(_ suggestions: [KeyboardSuggestion]) -> [KeyboardSuggestion] {
// A held space (`spacePending`) stands for the one delete takes first.
guard !spacePending, let correction = autocorrect.revertibleCorrection(before: textBefore) else {
return suggestions
}
let revert = KeyboardSuggestion(displayText: correction.original, replacement: correction.original, kind: .revert)
let others = suggestions.filter {
$0.kind != .keepLiteral && $0.replacement.caseInsensitiveCompare(correction.original) != .orderedSame
}
return [revert] + others.prefix(2)
}
}
extension AutocorrectEngine {
/// The latest autocorrection, which the strip offers to revert while the cursor is right
/// after it, as Apple's keyboard offers its bubble (black box, iOS 27.2): delete takes only
/// the space or mark after it, and the cursor may come back to it later.
struct AppliedCorrection: Equatable {
/// One word the revert puts back as typed (`from`), and what autocorrect made it.
struct Change: Equatable {
let from: String
let to: String
/// The language the fix was made in, for what the keys learn from reverting it.
var language: KeyboardLanguage? = nil
}
/// What you typed ("teh", "well see", "ofthe").
let original: String
/// What autocorrect put in its place, without the space or mark after it.
let corrected: String
/// The words in `original` that autocorrect changed; the revert keeps each as typed.
let changes: [Change]
/// The text before the cursor right after `corrected`.
let anchor: String
/// How much of the text before the cursor must read as `anchor`: hosts give a window of
/// it, which may start elsewhere once the text is long.
static let anchorTail = 64
/// Whether the cursor is right after `corrected`, with the same text before it.
func isAtEnd(of before: String?) -> Bool {
guard let before, before.hasSuffix(corrected) else { return false }
guard anchor.count >= Self.anchorTail, before.count >= Self.anchorTail else { return before == anchor }
return before.suffix(Self.anchorTail) == anchor.suffix(Self.anchorTail)
}
}
}