scoop:scoop.ui:0.1.0
src/text/editor/semantic_document.scoop
Selected production source · src/text/editor/semantic_document.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.*6
import scoop.window.accessibility.SemanticTextDocument8
internal fun TextEditHost.semanticTextDocument(): SemanticTextDocument? {9
if (options.protectedText)10
return None11
when (semanticDocument) {12
case Some(_) -> {}13
case None -> {14
val document = SemanticTextDocument(current.text.byteLength)15
current.semanticDocuments.add(document)16
semanticDocument = Some(document)17
}18
}19
return semanticDocument20
}22
internal fun TextEditHost.retireSemanticDocument() {23
val document = semanticDocument ?: return24
semanticDocument = None25
for (index in 0L ..< current.semanticDocuments.size) {26
if (current.semanticDocuments[index] === document) {27
current.semanticDocuments.removeAt(index)28
break29
}30
}31
document.close()32
}34
internal fun editorDifferenceMap(before: EditorValue, after: EditorValue): EditorOffsetMap {35
val edits = ArrayList<EditorDelta>()36
appendEditorDelta(edits, before, after, EditorEditKind.Separate)37
return EditorReplacementMap(38
Array<TextEditReplacement>(edits.size) {39
index ->40
val edit = edits[index]41
TextEditReplacement(42
Utf8Range(edit.start, edit.start + edit.removed.byteLength),43
edit.inserted44
)45
}46
)47
}49
private class SemanticMappingFailure {50
var error: Throwable? = None51
}53
internal fun editorOperationMap(54
before: EditorValue,55
after: EditorValue,56
operation: TextInputOperation57
): EditorOffsetMap {58
if (before.input.surroundingText == after.input.surroundingText)59
return EditorIdentityMap()60
return when (operation) {61
case Commit(val text, val replacement) -> EditorReplacementMap(62
[TextEditReplacement(editorReplacementRange(before, replacement), text)]63
)64
case Preedit(val text, _, val replacement) -> if (text.byteLength > 0L)65
EditorReplacementMap(66
[TextEditReplacement(editorReplacementRange(before, replacement), text)]67
)68
else69
editorDifferenceMap(before, after)70
case DeleteSurrounding(val previous, val following) -> {71
val range = before.input.selection.range72
EditorReplacementMap(73
[74
TextEditReplacement(Utf8Range(range.start - previous, range.start), ""),75
TextEditReplacement(Utf8Range(range.end, range.end + following), "")76
]77
)78
}79
else -> editorDifferenceMap(before, after)80
}81
}83
/** The native document owns endpoints, while this transaction supplies the existing mapping. */84
internal fun mapEditorSemanticDocuments(85
state: TextEditorState,86
before: EditorValue,87
after: EditorValue,88
change: EditorHistoryChange,89
explicit: EditorOffsetMap?90
) {91
if (92
state.semanticDocuments.size ==93
0L ||94
before.input.surroundingText ==95
after.input.surroundingText96
)97
return98
when (change) {99
case Reset -> {100
for (document in state.semanticDocuments)101
document.reset(after.input.surroundingText.byteLength)102
return103
}104
else -> {}105
}106
val mapping = explicit ?: editorDifferenceMap(before, after)107
val failure = SemanticMappingFailure()108
@Unsafe109
{110
val callback =111
foreignCallback<(Ptr<Unit>, Long, Int) -> Long>(112
{113
offset: Long,114
affinity: Int -> try {115
mapping.forward(116
TextPosition(117
offset,118
if (affinity == 0)119
TextAffinity.Upstream120
else121
TextAffinity.Downstream122
)123
).byteOffset124
} catch (error: Throwable) {125
failure.error = Some(error);126
-1L127
}128
},129
0,130
ForeignCallbackMode.Reusable131
)132
try {133
for (document in state.semanticDocuments)134
document.mapOffsets(135
after.input.surroundingText.byteLength,136
callback.function,137
callback.context138
)139
} catch (error: Throwable) {140
throw failure.error ?: error141
}142
finally {143
releaseForeignCallback(callback)144
}145
}146
}