scoop:scoop.ui:0.1.0
platform/desktop/text/replacements.scoop
Selected production source · platform/desktop/text/replacements.scoop
1
package scoop.ui.text3
import scoop.window.*4
import scoop.collections.sortedBy6
internal fun editorNativeReplacements(host: TextEditHost, raw: EditorPreparedInput): Array<7
TextEditReplacement8
> {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.copyAllowed17
)18
return []19
val services = host.scope.application.textServices20
if (!services.capabilities.automaticReplacement)21
return []22
val text = raw.current.input.surroundingText23
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
continue34
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
continue43
val range =44
Utf8Range(45
context.start + finding.range.start,46
context.start + finding.range.end47
)48
if (!(textCheckTouches(range, changed) || range.end == changed.start))49
continue50
var overlaps = false51
for (existing in result)52
if (existing.range.start <= range.end && existing.range.end >= range.start)53
overlaps = true54
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()62
}64
private 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 -> true68
else -> false69
}