-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNonLetterInputHandler.kt
More file actions
214 lines (196 loc) · 10.7 KB
/
Copy pathNonLetterInputHandler.kt
File metadata and controls
214 lines (196 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.urik.keyboard.service
import android.icu.lang.UCharacter
import android.icu.lang.UProperty
import com.urik.keyboard.KeyboardConstants.TextProcessingConstants
import com.urik.keyboard.settings.KeyboardSettings
import com.urik.keyboard.utils.CursorEditingUtils
import com.urik.keyboard.utils.ErrorLogger
import com.urik.keyboard.utils.UrlEmailDetector
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class NonLetterInputHandler(
private val inputState: InputStateManager,
private val outputBridge: OutputBridge,
private val suggestionPipeline: SuggestionPipeline,
private val autoCorrectionEngine: AutoCorrectionEngine,
private val textInputProcessor: TextInputProcessor,
private val swipeSpaceManager: SwipeSpaceManager,
private val languageManager: LanguageManager,
private val candidateBarController: CandidateBarController,
private val serviceScope: CoroutineScope,
private val onGetCurrentSettings: () -> KeyboardSettings,
private val onCoordinateStateClear: () -> Unit,
private val onCheckAutoCapitalization: (textBefore: String) -> Unit,
private val onDisableCapsLockAfterPunctuation: () -> Unit
) {
fun handle(char: String) {
if (inputState.requiresDirectCommit) {
outputBridge.sendCharacter(char)
return
}
if (inputState.isSuggestionsDisabled) {
outputBridge.sendCharacter(char)
return
}
serviceScope.launch {
try {
inputState.lastSpaceTime = 0
if (char.length == 1) {
val textBeforeCursor = outputBridge.safeGetTextBeforeCursor(1)
if (swipeSpaceManager.shouldRemoveSpaceForPunctuation(char.single(), textBeforeCursor)) {
outputBridge.deleteSurroundingText(1, 0)
}
}
if (inputState.displayBuffer.isNotEmpty()) {
val actualTextBefore = outputBridge.safeGetTextBeforeCursor(1)
val actualTextAfter = outputBridge.safeGetTextAfterCursor(1)
if (actualTextBefore.isEmpty() && actualTextAfter.isEmpty()) {
onCoordinateStateClear()
}
}
if (inputState.postCommitReplacementState != null) {
inputState.postCommitReplacementState = null
candidateBarController.clearSuggestions()
}
inputState.lastAutocorrection = null
if (inputState.spellConfirmationState == SpellConfirmationState.AWAITING_CONFIRMATION) {
outputBridge.beginBatchEdit()
try {
suggestionPipeline.confirmAndLearnWord(onCheckAutoCapitalization)
outputBridge.commitText(char, 1)
if (char.length == 1) {
val singleChar = char.single()
if (isSentenceEndingPunctuation(singleChar) && !inputState.requiresDirectCommit) {
onDisableCapsLockAfterPunctuation()
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
}
}
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
if (inputState.displayBuffer.isNotEmpty() &&
onGetCurrentSettings().spellCheckEnabled &&
inputState.displayBuffer.length >= TextProcessingConstants.MIN_SPELL_CHECK_LENGTH
) {
val textBefore = outputBridge.safeGetTextBeforeCursor(100)
val isUrlOrEmail =
UrlEmailDetector.isUrlOrEmailContext(
currentWord = inputState.displayBuffer,
textBeforeCursor = textBefore,
nextChar = char
)
if (!isUrlOrEmail) {
val isPunctuation =
char.length == 1 && CursorEditingUtils.isPunctuation(char.single())
if (isPunctuation) {
suggestionPipeline.cancelDebounceJob()
val isValid = textInputProcessor.validateWord(inputState.displayBuffer)
if (!isValid) {
outputBridge.beginBatchEdit()
try {
val pronounLang = languageManager.currentLanguage.value.split("-").first()
if (pronounLang == "en" && inputState.displayBuffer.isNotEmpty()) {
val corrected = EnglishPronounCorrection.capitalize(
inputState.displayBuffer.lowercase()
)
if (corrected != null && corrected != inputState.displayBuffer) {
inputState.onPronounCapitalized(corrected)
outputBridge.setComposingText(corrected, 1)
}
}
suggestionPipeline.learnWordAndInvalidateCache(
inputState.displayBuffer,
InputMethod.TYPED
)
outputBridge.finishComposingText()
outputBridge.commitText(char, 1)
val singleChar = char.single()
if (isSentenceEndingPunctuation(singleChar) &&
!inputState.requiresDirectCommit
) {
onDisableCapsLockAfterPunctuation()
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
}
onCoordinateStateClear()
suggestionPipeline.showBigramPredictions()
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
} else {
suggestionPipeline.cancelDebounceJob()
val decision = autoCorrectionEngine.decide(
buffer = inputState.displayBuffer,
spellCheckEnabled = onGetCurrentSettings().spellCheckEnabled,
autocorrectionEnabled = onGetCurrentSettings().autocorrectionEnabled,
pauseOnMisspelledWord = onGetCurrentSettings().pauseOnMisspelledWord,
lastAutocorrection = inputState.lastAutocorrection,
textBeforeCursor = textBefore,
nextChar = char
)
when (decision) {
is AutocorrectDecision.Pause -> {
inputState.spellConfirmationState = SpellConfirmationState.AWAITING_CONFIRMATION
inputState.pendingWordForLearning = inputState.displayBuffer
outputBridge.highlightCurrentWord()
val suggestions = textInputProcessor.getSuggestions(inputState.displayBuffer)
val displaySuggestions =
suggestionPipeline.storeAndCapitalizeSuggestions(
suggestions,
inputState.isCurrentWordAtSentenceStart
)
inputState.pendingSuggestions = displaySuggestions
if (displaySuggestions.isNotEmpty()) {
candidateBarController.updateSuggestions(displaySuggestions)
} else {
candidateBarController.clearSuggestions()
}
return@launch
}
else -> { /* fall through to commit char */ }
}
}
}
}
outputBridge.beginBatchEdit()
try {
val pronounLang = languageManager.currentLanguage.value.split("-").first()
if (pronounLang == "en" && inputState.displayBuffer.isNotEmpty()) {
val corrected = EnglishPronounCorrection.capitalize(inputState.displayBuffer.lowercase())
if (corrected != null && corrected != inputState.displayBuffer) {
inputState.onPronounCapitalized(corrected)
outputBridge.setComposingText(corrected, 1)
}
}
suggestionPipeline.coordinateWordCompletion()
suggestionPipeline.showBigramPredictions()
outputBridge.commitText(char, 1)
if (char.length == 1) {
val singleChar = char.single()
if (isSentenceEndingPunctuation(singleChar) && !inputState.requiresDirectCommit) {
onDisableCapsLockAfterPunctuation()
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
}
}
} finally {
outputBridge.endBatchEdit()
}
} catch (e: Exception) {
ErrorLogger.logException(
component = "UrikInputMethodService",
severity = ErrorLogger.Severity.HIGH,
exception = e,
context = mapOf("operation" to "handleNonLetterInput")
)
}
}
}
private fun isSentenceEndingPunctuation(char: Char): Boolean =
UCharacter.hasBinaryProperty(char.code, UProperty.S_TERM)
}