scoop:scoop.window:0.1.0
src/text/apply.scoop
Selected production source · src/text/apply.scoop
1
package scoop.window3
internal fun applyTextBatch(original: TextInputState, batch: TextInputBatch): TextInputState {4
original.validate()5
when (batch.baseRevision) {6
case Some(val value) -> if (value != original.revision)7
throw IllegalStateException(Some("stale text input revision"))8
case None -> {}9
}10
if (batch.operations.size == 0L)11
return original12
requireWindow(original.revision < 9223372036854775807L, "text revision overflow")13
var state = original14
for (operation in batch.operations) {15
state =16
when (operation) {17
case ClearPreedit -> TextInputState(18
state.surroundingText,19
state.selection,20
None,21
state.caretRect,22
state.revision23
)24
case Commit(val text, val replacement) -> replaceText(25
state,26
text,27
replacement,28
None,29
false30
)31
case Preedit(val text, val selection, val replacement) -> replaceText(32
state,33
text,34
replacement,35
selection,36
true37
)38
case DeleteSurrounding(val before, val after) -> deleteSurrounding(39
state,40
before,41
after42
)43
case SetSelection(val selection) -> selectText(state, selection)44
}45
}46
return TextInputState(47
state.surroundingText,48
state.selection,49
state.markedRange,50
state.caretRect,51
original.revision + 1L52
)53
}55
private fun selectText(state: TextInputState, selection: Utf8Selection): TextInputState {56
val range = selection.range57
checkTextRange(state.surroundingText.toByteArray(), range)58
when (state.markedRange) {59
case Some(val marked) -> requireWindow(60
range.start >= marked.start && range.end <= marked.end,61
"selection leaves active preedit"62
)63
case None -> {}64
}65
return TextInputState(66
state.surroundingText,67
selection,68
state.markedRange,69
state.caretRect,70
state.revision71
)72
}74
private fun replaceText(75
state: TextInputState,76
text: String,77
replacement: TextReplacement,78
selection: Utf8Selection?,79
preedit: Boolean80
): TextInputState {81
val range =82
when (replacement) {83
case CurrentSelection -> state.markedRange ?: state.selection.range84
case Range(val value) -> value85
}86
checkTextRange(state.surroundingText.toByteArray(), range)87
val relative = selection ?: Utf8Selection(text.byteLength, text.byteLength)88
checkTextRange(text.toByteArray(), relative.range)89
val nextText =90
textSlice(state.surroundingText, Utf8Range(0L, range.start)) +91
text +92
textSlice(state.surroundingText, Utf8Range(range.end, state.surroundingText.byteLength))93
val end = range.start + text.byteLength94
val nextSelection =95
if (preedit)96
Utf8Selection(range.start + relative.anchor, range.start + relative.cursor)97
else98
Utf8Selection(end, end)99
val marked =100
if (preedit && text.byteLength > 0L)101
Some(Utf8Range(range.start, end))102
else103
None104
return TextInputState(nextText, nextSelection, marked, state.caretRect, state.revision)105
}107
private fun deleteSurrounding(state: TextInputState, before: Long, after: Long): TextInputState {108
val range = state.selection.range109
requireWindow(110
before >=111
0L &&112
after >=113
0L &&114
before <=115
range.start &&116
after <=117
state.surroundingText.byteLength -118
range.end,119
"invalid surrounding deletion"120
)121
val bytes = state.surroundingText.toByteArray()122
checkTextRange(bytes, Utf8Range(range.start - before, range.start))123
checkTextRange(bytes, Utf8Range(range.end, range.end + after))124
val text =125
textSlice(state.surroundingText, Utf8Range(0L, range.start - before)) +126
textSlice(state.surroundingText, range) +127
textSlice(state.surroundingText, Utf8Range(range.end + after, bytes.size))128
return TextInputState(129
text,130
Utf8Selection(state.selection.anchor - before, state.selection.cursor - before),131
None,132
state.caretRect,133
state.revision134
)135
}