iOS: your text replacements beat chat shorthand, and no spelling guesses with Autocorrect off (#2148)

* iOS: your text replacements win over chat shorthand

Hex leaves chat shorthand alone (brb, omg, idk, btw, … in shorthandAllowlist) so
autocorrect never turns it into a dictionary word, but that guard ran before the
text replacement lookup, so a user's own replacement with such a shortcut
("brb" -> "be right back") never applied, with Autocorrect on or off. Apple's
keyboard applies every text replacement (black box, iOS 27.2 sim: "Brb" offers and
becomes "Be right back").

Now the user's UILexicon shortcut is checked before the shorthand guard, on space
and punctuation and in the strip; only lexicon entries get past it, never Hex's own
corrections, so shorthand without a replacement ("idk") still stays as typed.
Words you kept (a tapped literal, an undone fix, Learn how I type's rejections)
still block the replacement, as before.

Verified: new AutocorrectEngineTests case (brb with Autocorrect on, omg with it off,
idk untouched, one backspace restores the shortcut); full HEXMobileTests suite
(465 tests) passes on the branch; live in Messages on an iOS 27.2 sim with a "brb"
text replacement added in Settings: Hex offers and applies it with Autocorrect on
and off.

* iOS: with Autocorrect off, the strip offers completions but no fixes

With Settings > Keyboard > Autocorrect off, Hex's strip still offered every fix it
would have applied, just without applying it: spelling guesses ("One" for "omw"),
contractions ("don't" for "dont"), learned typo fixes and Spatial autocorrect's
readings.

Apple's keyboard with Auto-Correction off (black box, iOS 27.2 sim, Predictive Text
on) offers what you typed, your text replacements and completions, and no fix:
"teh" and "dont" get nothing (with Auto-Correction on: "The", "Don't"), "im" gets
"Imagine" but no "I'm", "th" gets "The", "This", "omw" only "On my way!". Its
completions tolerate a slip ("wierd": Weird, Weirdly), which UITextChecker's
completions can't; Hex keeps its prefix completions only.

So with Autocorrect off the strip now holds the typed word, a text replacement and
completions (UITextChecker's and the typing model's short-prefix completion); learned
fixes, suggest-only contractions, Spatial readings and spelling guesses need
Autocorrect on, in either language of bilingual autocorrect. With Autocorrect on
nothing changes.

The old testTurningAutocorrectSettingOffKeepsSuggestionsWithoutAutoReplacing
expected a "dont" suggestion with Autocorrect off; it is replaced by
testTurningAutocorrectSettingOffKeepsCompletionsWithoutFixes, which checks the new
behaviour (no "don't", no "the" for "teh", "the" for "th", fixes back once on).

Verified: AutocorrectEngineTests and the full HEXMobileTests suite (465 tests) pass;
live in Messages on an iOS 27.2 sim with Hex's Autocorrect off: "Teh" and "Dont"
show only themselves, "Th" offers The and That, "Im" Important and Image, "Omw" only
"On my way!".
This commit is contained in:
Ryan Vogel
2026-11-21 06:43:31 +00:00
committed by GitHub
parent 1b44779efb
commit e0e8b2258b
2 changed files with 56 additions and 16 deletions
@@ -1089,13 +1089,27 @@ final class AutocorrectEngineTests: XCTestCase {
XCTAssertEqual(target.text, "what the fuck you bitch ")
}
func testTurningAutocorrectSettingOffKeepsSuggestionsWithoutAutoReplacing() {
/// Apple's keyboard with Auto-Correction off (black box, iOS 27.2) offers what you typed,
/// your text replacements and completions ("th": The, This; "beau": Beautiful, Beauty;
/// "im": Imagine), but no fix: nothing for "teh" or "dont", no "I'm" for "im".
func testTurningAutocorrectSettingOffKeepsCompletionsWithoutFixes() {
engine.autocorrect.isEnabled = false
typeWord("dont")
XCTAssertFalse(engine.suggestions.isEmpty)
XCTAssertFalse(engine.suggestions[1].willAutoReplace)
XCTAssertEqual(engine.suggestions.first?.replacement, "dont")
XCTAssertFalse(engine.suggestions.contains { $0.replacement == "don't" }, "no contraction")
tap(.space)
XCTAssertEqual(target.text, "dont ")
typeWord("teh")
XCTAssertFalse(engine.suggestions.contains { $0.replacement == "the" }, "no spelling guess")
tap(.space)
typeWord("th")
XCTAssertEqual(engine.suggestions.dropFirst().first?.replacement, "the", "completions stay")
XCTAssertFalse(engine.suggestions.contains { $0.willAutoReplace })
engine.autocorrect.isEnabled = true
tap(.space)
typeWord("teh")
XCTAssertEqual(engine.suggestions.dropFirst().first?.replacement, "the", "on: the fix")
}
/// A TestFlight tester (build 56) kept Autocorrect on only for text replacements. Apple's
@@ -1126,6 +1140,25 @@ final class AutocorrectEngineTests: XCTestCase {
typeWord("addr ")
XCTAssertEqual(target.text, "addr ", "a field without autocorrection gets none, as with Apple's")
}
/// Chat shorthand Hex leaves alone ("brb", "idk") still takes your own text replacement,
/// as Apple's keyboard applies every text replacement, with Autocorrect on or off.
func testYourTextReplacementWinsOverShorthand() {
engine.autocorrect.loadLexiconEntries([("brb", "be right back"), ("omg", "oh my god")])
typeWord("brb")
XCTAssertEqual(engine.suggestions.dropFirst().first?.replacement, "be right back")
XCTAssertTrue(engine.suggestions.dropFirst().first?.willAutoReplace == true)
typeWord(" idk ")
XCTAssertEqual(target.text, "be right back idk ", "shorthand without a replacement stays")
engine.autocorrect.isEnabled = false
typeWord("omg")
XCTAssertEqual(engine.suggestions.dropFirst().first?.replacement, "oh my god")
tap(.space)
XCTAssertEqual(target.text, "be right back idk oh my god ")
tap(.delete)
XCTAssertEqual(target.text, "be right back idk omg", "one backspace puts back the shortcut")
}
}
@@ -577,10 +577,11 @@ final class AutocorrectEngine {
self.beforeInput = beforeInput
let lower = token.lowercased()
// Chat shorthand ("brb") is left alone, unless it is your own text replacement's shortcut.
let allowAuto = !suppressedInSession.contains(lower)
&& !learning.rejected.contains(lower)
&& !learnedWords.contains(lower)
&& !Self.shorthandAllowlist.contains(lower)
&& (!Self.shorthandAllowlist.contains(lower) || lexiconShortcuts[lower] != nil)
partner?.beforeInput = beforeInput
// Autocorrect off: your text replacements only.
@@ -590,8 +591,8 @@ final class AutocorrectEngine {
if let partner = workingPartner {
// Both languages' candidates, those of the language the words before are in
// first; spelling guesses only for a word neither knows.
let mine = candidates(for: token, autoFix: autoFix, guesses: !partner.knowsAsTyped(token), limit: 2, model: predictionModel)
let theirs = partner.candidates(for: token, autoFix: autoFix, guesses: !knowsAsTyped(token), limit: 2, model: partner.decoder?.model)
let mine = candidates(for: token, autoFix: autoFix, guesses: !partner.knowsAsTyped(token), limit: 2, model: predictionModel, corrects: isEnabled)
let theirs = partner.candidates(for: token, autoFix: autoFix, guesses: !knowsAsTyped(token), limit: 2, model: partner.decoder?.model, corrects: isEnabled)
let leansPartner = languageLean(partner, token: token) > 0
let (first, second) = leansPartner ? (theirs, mine) : (mine, theirs)
for index in 0..<max(first.count, second.count) {
@@ -601,7 +602,7 @@ final class AutocorrectEngine {
}
}
} else {
pool += candidates(for: token, autoFix: autoFix, guesses: true, limit: 2 - pool.count, model: predictionModel)
pool += candidates(for: token, autoFix: autoFix, guesses: true, limit: 2 - pool.count, model: predictionModel, corrects: isEnabled)
}
let literal = KeyboardSuggestion(displayText: "“\(token)”", replacement: token, kind: .keepLiteral)
@@ -624,7 +625,13 @@ final class AutocorrectEngine {
/// strip), at most `limit`: a learned fix or contraction space won't apply, Spatial
/// autocorrect's other readings of your taps, spelling guesses for a word the dictionary
/// doesn't know (only with `guesses`), then completions.
private func candidates(for token: String, autoFix: String?, guesses: Bool, limit: Int, model: TypingDecoderModel?) -> [String] {
///
/// `corrects` false (Autocorrect off): completions only, as Apple's keyboard with
/// Auto-Correction off offers "Imagine" for "im" but not "I'm", and nothing for "teh" or
/// "dont" (black box, iOS 27.2).
private func candidates(
for token: String, autoFix: String?, guesses: Bool, limit: Int, model: TypingDecoderModel?, corrects: Bool = true
) -> [String] {
let lower = token.lowercased()
var pool: [String] = autoFix.map { [$0] } ?? []
let start = pool.count
@@ -633,7 +640,7 @@ final class AutocorrectEngine {
pool.contains { $0.caseInsensitiveCompare(item) == .orderedSame }
}
if autoFix == nil {
if corrects, autoFix == nil {
if let learned = learning.typos[lower], mayOffer(learned, replacing: token) {
// A learned fix space won't apply (the typo is a real word, or you kept it) is still offered.
pool.append(Self.matchCase(of: token, onto: learned))
@@ -653,7 +660,7 @@ final class AutocorrectEngine {
}
// Spatial autocorrect's other readings of your taps.
if let decision = decoderDecision(for: token) {
if corrects, let decision = decoderDecision(for: token) {
for reading in decision.readings where pool.count < end && mayOffer(reading.text, replacing: token) {
let cased = Self.matchCase(of: token, onto: reading.text)
if !has(cased) {
@@ -663,7 +670,7 @@ final class AutocorrectEngine {
}
// Spelling guesses for a word the dictionary doesn't know.
if guesses && pool.count < end && token.count >= 2 {
if corrects && guesses && pool.count < end && token.count >= 2 {
let valid = Self.shorthandAllowlist.contains(lower) || learnedWords.contains(lower) || checker.isWordValid(token)
if !valid {
for guess in rankedGuesses(for: token) {
@@ -727,11 +734,11 @@ final class AutocorrectEngine {
let lower = token.lowercased()
guard !suppressedInSession.contains(lower),
!learning.rejected.contains(lower),
!learnedWords.contains(lower),
!Self.shorthandAllowlist.contains(lower) else { return nil }
guard isEnabled, hasDictionary else {
return lexiconShortcuts[lower].map { (token, $0) }
}
!learnedWords.contains(lower) else { return nil }
// Your own text replacement comes first, as on Apple's keyboard: before the chat
// shorthand Hex leaves alone ("brb"), and with Autocorrect off or no dictionary.
if let shortcut = lexiconShortcuts[lower] { return (token, shortcut) }
guard isEnabled, hasDictionary, !Self.shorthandAllowlist.contains(lower) else { return nil }
guard let fix = cautiousReplacement(for: token) else { return nil }
return (token, fix)
}