Scoop API

scoop:scoop.ui:0.1.0

platform/desktop/text/replacements.scoop

Selected production source · platform/desktop/text/replacements.scoop

1package scoop.ui.text
3import scoop.window.*
4import scoop.collections.sortedBy
6internal fun editorNativeReplacements(host: TextEditHost, raw: EditorPreparedInput): Array<
7 TextEditReplacement
8> {
9 if (
10 !host.options.automaticTextReplacement ||
11 !raw.completed ||
12 raw.pending.size !=
13 0L ||
14 host.options.protectedText ||
15 host.options.projection.masked ||
16 !host.copyAllowed
17 )
18 return []
19 val services = host.scope.application.textServices
20 if (!services.capabilities.automaticReplacement)
21 return []
22 val text = raw.current.input.surroundingText
23 val inserted = editorInsertedRanges(raw.committedSpans)
24 val result = ArrayList<TextEditReplacement>()
25 try {
26 for (changed in inserted) {
27 val addition = text.utf8Slice(changed)
28 if (
29 addition.charLength ==
30 0L ||
31 !textReplacementBoundary(addition.getCharAt(addition.charLength - 1L))
32 )
33 continue
34 val context = textCheckingContext(text, changed)
35 val findings =
36 services.checkText(
37 context.text,
38 TextCheckingOptions(spelling = false, grammar = false, replacements = true)
39 )
40 for (finding in findings) {
41 if (finding.kind != TextCheckKind.Replacement || finding.suggestions.size == 0L)
42 continue
43 val range =
44 Utf8Range(
45 context.start + finding.range.start,
46 context.start + finding.range.end
47 )
48 if (!(textCheckTouches(range, changed) || range.end == changed.start))
49 continue
50 var overlaps = false
51 for (existing in result)
52 if (existing.range.start <= range.end && existing.range.end >= range.start)
53 overlaps = true
54 if (!overlaps)
55 result.add(TextEditReplacement(range, finding.suggestions[0L]))
56 }
57 }
58 } catch (_: WindowException) {
59 return []
60 }
61 return result.sortedBy<TextEditReplacement, Long> { value -> value.range.start }.toArray()
64private fun textReplacementBoundary(character: Char): Boolean =
65 when (character.code) {
66 9, 10, 13, 32, 33, 34, 39, 44, 45, 46, 58, 59, 63,
67 0x3000, 0x3001, 0x3002, 0xff01, 0xff0c, 0xff0e, 0xff1a, 0xff1b, 0xff1f -> true
68 else -> false
69 }