Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/semantic_document.scoop

Selected production source · src/text/editor/semantic_document.scoop

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
6import scoop.window.accessibility.SemanticTextDocument
8internal fun TextEditHost.semanticTextDocument(): SemanticTextDocument? {
9 if (options.protectedText)
10 return None
11 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 semanticDocument
22internal fun TextEditHost.retireSemanticDocument() {
23 val document = semanticDocument ?: return
24 semanticDocument = None
25 for (index in 0L ..< current.semanticDocuments.size) {
26 if (current.semanticDocuments[index] === document) {
27 current.semanticDocuments.removeAt(index)
28 break
29 }
30 }
31 document.close()
34internal 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.inserted
44 )
45 }
46 )
49private class SemanticMappingFailure {
50 var error: Throwable? = None
53internal fun editorOperationMap(
54 before: EditorValue,
55 after: EditorValue,
56 operation: TextInputOperation
57): 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 else
69 editorDifferenceMap(before, after)
70 case DeleteSurrounding(val previous, val following) -> {
71 val range = before.input.selection.range
72 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 }
83/** The native document owns endpoints, while this transaction supplies the existing mapping. */
84internal 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.surroundingText
96 )
97 return
98 when (change) {
99 case Reset -> {
100 for (document in state.semanticDocuments)
101 document.reset(after.input.surroundingText.byteLength)
102 return
103 }
104 else -> {}
105 }
106 val mapping = explicit ?: editorDifferenceMap(before, after)
107 val failure = SemanticMappingFailure()
108 @Unsafe
109 {
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.Upstream
120 else
121 TextAffinity.Downstream
122 )
123 ).byteOffset
124 } catch (error: Throwable) {
125 failure.error = Some(error);
126 -1L
127 }
128 },
129 0,
130 ForeignCallbackMode.Reusable
131 )
132 try {
133 for (document in state.semanticDocuments)
134 document.mapOffsets(
135 after.input.surroundingText.byteLength,
136 callback.function,
137 callback.context
138 )
139 } catch (error: Throwable) {
140 throw failure.error ?: error
141 }
142 finally {
143 releaseForeignCallback(callback)
144 }
145 }