hex/ios/HEXShared/FullKeyboard/View/FullKeyboardView+Vocabulary.swift
Kit Langton 9e352ea9d4
iOS: group shared sources and split the full keyboard tests (#2448)
- Move the full keyboard's engine, view and autocorrect families into
  HEXShared/FullKeyboard/Engine, View and Autocorrect.
- Move dictation formatting, spelling and romanizer sources into
  HEXShared/Formatting, Spelling and Romanizers.
- Split the 4,181-line FullKeyboardEngineTests file into per-capability
  extension files, and KeyboardClipboardTests into its own file.

Pure moves: no behavior changes and the same 160 tests.
2026-11-22 18:18:35 -08:00

45 lines
2 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 UIKit
/// Add to vocabulary from the strip: hold the word you typed (the strip's left slot) and the
/// strip offers it; the chip adds it to Hex's vocabulary list (`CustomVocabularySettings`,
/// the one Settings › Keyboard › Vocabulary and Settings › Dictation edit), which the keys
/// then never autocorrect away, complete and swipe.
@MainActor
extension FullKeyboardView {
func offerVocabulary(_ suggestion: KeyboardSuggestion) {
var word = suggestion.replacement
// A capital only the sentence's start gave it ("Kubectl" opening a message) isn't its own.
if let first = word.first, first.isUppercase, !word.dropFirst().contains(where: \.isUppercase),
AutocorrectEngine.startsSentence(before: engine.target.documentContextBeforeInput ?? "", token: word) {
word = first.lowercased() + word.dropFirst()
}
guard suggestion.kind == .keepLiteral, engine.autocorrect.vocabularyTerm(for: word) == nil,
VocabularySpelling.normalizedKey(word).count >= 2 else { return }
typingLog?.ui("vocabulary_offer")
selectionFeedback()
vocabularyOffer = word
refreshStrip()
}
var vocabularyNotice: KeyboardStripNotice? {
guard let word = vocabularyOffer else { return nil }
return KeyboardStripNotice(
title: "“\(word)”",
detail: String(localized: "Never autocorrected"),
button: String(localized: "Add to vocabulary")
)
}
/// Saves `word` in the App Group (kept for this session too where the keyboard can't
/// write there, without Full Access) and uses it at once.
func addToVocabulary(_ word: String) {
typingLog?.ui("vocabulary_add")
let settings = CustomVocabularySettings()
let terms = CustomVocabularySettings.sanitize(settings.terms + engine.autocorrect.vocabulary + [word])
settings.terms = terms
engine.autocorrect.setVocabulary(terms)
vocabularyOffer = nil
engine.refreshSuggestions()
refreshStrip()
}
}