hex/ios/HEXShared/FullKeyboard/GreekTyping.swift
Ryan Vogel dc332bf6ca iOS: delete after an autocorrection takes only its space, and the strip offers what you typed
X tester Brandon McConnell (and a TestFlight tester about text replacements, fixed in
uncorrected version." Apple's keyboard doesn't (black box, iOS 27.2 simulator, a
UITextView): after "becuase " became "Because ", delete took only the space and the text
view showed "Becuase" with an undo arrow; space then kept "Because ", a letter continued
the word, a second delete took a letter, a comma went like a space, tapping the bubble put
"Becuase" back without the space, and deleting back to the word later brought the bubble
back. Revisions ("well see " -> "We'll see ", bubble "Well see") and splits ("ofthe" ->
"Of the", bubble "Ofthe") work the same.

Removed: Hex's deliberate "one backspace undoes the correction" (FullKeyboardEngine.
deleteBackward's undo branch and AutocorrectEngine's pending undo, consumed by the next
delete). Replaced by:
- AutocorrectEngine.lastCorrection keeps the latest autocorrection with what was typed,
  what went in and an anchor (the text before the cursor right after it); it is offered
  while the text before the cursor reads that again (its last 64 characters for long
  text), and forgotten on a new field, a new language or a newer correction. The many
  clearPendingUndo calls after other input are gone: the anchor check covers them.
- KeyboardSuggestion.Kind.revert: the strip's left slot shows it in place of the word as
  it reads, pill-highlighted with Apple's undo arrow (a keyboard can't draw a bubble in
  the host's text). Tapping it puts back what was typed, suppresses the fix for the
  session, and counts as an undone autocorrection for Learn how I type and Adaptive touch
  (whose word taps the delete of the space now keeps for it). Typing logs: autocorrect_undo
  with src strip, context why revert.
- Merges, splits and #2144's revisions go through the same record.
- deleteBackward's allowAutocorrectUndo is now takesSwipeBack: it only gates a swipe's
  whole-word delete, which stays.

Verified on the Studio (iPhone 17, iOS 27.2 simulator): AutocorrectEngineTests,
FullKeyboardEngineTests and TypingLogTests pass (updated: the keep loop, the revision,
the edge-space join, punctuation and Adaptive touch tests now revert from the strip; new:
testDeleteAfterAnAutocorrectionTakesOnlyItsSpaceAsOnApplesKeyboard with the black-box
cases).
2026-11-21 09:12:46 -05:00

59 lines
2.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
/// The Greek layout's own typing rules, as the system Greek keyboard was observed to apply them
/// (iOS 27.2 simulator, real touches in Messages, black box). The tonos key ´ accents the vowel
/// right before it (α´ -> ά, Α´ -> Ά, and ϊ´ -> ΐ, ϋ´ -> ΰ); after anything else, an accented
/// vowel included, it types ´ (U+00B4) itself, and it never adds a diaeresis (ϊ ϋ ΐ ΰ are held on
/// ι and υ). σ types ς while it ends the word, and turns back into σ when another letter
/// follows ("ας", then α: "ασα").
nonisolated enum GreekTyping {
/// The tonos key: the character it types when there is no vowel before it to accent.
static let tonosKey = "\u{00B4}"
private static let accented: [Character: Character] = [
"α": "ά", "ε": "έ", "η": "ή", "ι": "ί", "ο": "ό", "υ": "ύ", "ω": "ώ",
"Α": "Ά", "Ε": "Έ", "Η": "Ή", "Ι": "Ί", "Ο": "Ό", "Υ": "Ύ", "Ω": "Ώ",
"ϊ": "ΐ", "ϋ": "ΰ",
]
/// The vowel `before` ends with, accented, when the tonos key is typed after it; nil when
/// the key types ´ itself.
static func accenting(_ text: String, after before: String?) -> String? {
guard text == tonosKey, let last = before?.last, let vowel = accented[last] else { return nil }
return String(vowel)
}
/// What a letter key types: ς for σ, which ends the word until another letter follows.
static func letter(_ text: String) -> String {
text == "σ" ? "ς" : text
}
/// Whether typing `text` after `before` turns a final ς back into σ: another letter
/// follows it.
static func reopensFinalSigma(typing text: String, after before: String?) -> Bool {
before?.last == "ς" && text.first?.isLetter == true
}
}
@MainActor
extension FullKeyboardEngine {
/// The Greek layout's tonos key and final sigma (`GreekTyping`): true when the tonos key
/// accented the vowel before it, which is all it does; else `text` is what to type (ς for
/// σ), with a final ς before it already back to σ.
func typeGreek(_ text: inout String) -> Bool {
let before = target.documentContextBeforeInput
if let vowel = GreekTyping.accenting(text, after: before) {
target.deleteBackward()
target.insertText(vowel)
didEdit()
logState("typed")
return true
}
if GreekTyping.reopensFinalSigma(typing: text, after: before) {
target.deleteBackward()
target.insertText("σ")
}
text = GreekTyping.letter(text)
return false
}
}