Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/projection.scoop

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

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
7public enum TextCopyPolicy {
8 Default, Allow, Deny
9}
11/** A display-only interval transformation. The editing document remains unchanged. */
12public class TextProjection internal constructor(
13 internal val transform: (TextEditSnapshot) -> Array<TextEditReplacement>,
14 internal val masked: Boolean
17public fun textProjection(
18 transform: (TextEditSnapshot) -> Array<TextEditReplacement> = { _ -> [] }
19): TextProjection = TextProjection(transform, false)
21public fun textMask(glyph: String = "•"): TextProjection {
22 requireUi(
23 !editorHasLineBreak(glyph) && editorGraphemeBytes(glyph).size == 2L,
24 "mask glyph must be one non-newline grapheme"
25 )
26 return TextProjection(
27 {
28 input ->
29 val bytes = input.text.toByteArray()
30 val boundaries = editorGraphemeBytes(input.text)
31 val replacements = ArrayList<TextEditReplacement>()
32 for (index in 0L ..< boundaries.size - 1L) {
33 val range = Utf8Range(boundaries[index], boundaries[index + 1L])
34 if (!editorHasLineBreak(editorSlice(bytes, range)))
35 replacements.add(TextEditReplacement(range, glyph))
36 }
37 replacements.toArray()
38 },
39 true
40 )
43/** This immutable mapping must be retained with the layout built from text. */
44public class TextDisplay internal constructor(
45 public val source: TextEditSnapshot,
46 public val text: String,
47 internal val mapping: EditorOffsetMap,
48 internal val edits: Array<TextEditReplacement>
49) {
50 private val sourceBytes = source.text.toByteArray()
51 private val displayBytes = text.toByteArray()
52 internal val boundaries = editorGraphemeBytes(source.text)
53 internal fun documentPosition(position: TextPosition): TextPosition =
54 editorBoundaryPosition(boundaries, position)
55 public fun toDisplay(position: TextPosition): TextPosition {
56 requireUi(editorScalar(sourceBytes, position.byteOffset), "invalid document text position")
57 return mapping.forward(position)
58 }
59 public fun toDocument(position: TextPosition): TextPosition {
60 requireUi(editorScalar(displayBytes, position.byteOffset), "invalid display text position")
61 return mapping.reverse(position)
62 }
63 public fun displayRange(range: Utf8Range): TextRange {
64 checkEditorRange(sourceBytes, range)
65 val start = mapping.forward(TextPosition(range.start, TextAffinity.Downstream)).byteOffset
66 val end = mapping.forward(TextPosition(range.end, TextAffinity.Upstream)).byteOffset
67 return TextRange(
68 start,
69 if (end >= start)
70 end
71 else
72 start
73 )
74 }
75 internal fun forward(selection: EditorSelection): EditorSelection =
76 EditorSelection(mapping.forward(selection.anchor), mapping.forward(selection.cursor))
77 internal fun reverse(selection: EditorSelection): EditorSelection =
78 EditorSelection(mapping.reverse(selection.anchor), mapping.reverse(selection.cursor))
81public fun projectText(
82 input: TextEditSnapshot,
83 projection: TextProjection = textProjection()
84): TextDisplay {
85 val bytes = input.text.toByteArray()
86 requireUi(
87 editorScalar(bytes, input.anchor.byteOffset) &&
88 editorScalar(bytes, input.cursor.byteOffset),
89 "invalid projection selection"
90 )
91 when (input.markedRange) {
92 case Some(val range) -> checkEditorRange(bytes, range)
93 case None -> {}
94 }
95 return projectEditorText(input, projection)
98internal fun projectEditorText(input: TextEditSnapshot, projection: TextProjection): TextDisplay =
99 evaluateUi(None) {
100 val result = mapEditorText(input.text, projection.transform(input))
101 TextDisplay(input, result.text, result.mapping, result.edits)
102 }