KeyHandler // Re-enable NSString in buildInputtingState().

This commit is contained in:
ShikiSuen 2022-05-21 17:53:47 +08:00
parent b84fe50964
commit cda914b5c4
1 changed files with 36 additions and 43 deletions

View File

@ -36,34 +36,38 @@ extension KeyHandler {
// to "refresh" the text input buffer with our "composing text" // to "refresh" the text input buffer with our "composing text"
var composingBuffer = "" var composingBuffer = ""
var composedStringCursorIndex = 0 var composedStringCursorIndex = 0
var readingCursorIndex = 0 var readingCursorIndex = 0
let builderCursorIndex = builderCursorIndex // We must do some Unicode codepoint counting to find the actual cursor location for the client
// i.e. we need to take UTF-16 into consideration, for which a surrogate pair takes 2 UniChars
for theAnchor in _walkedNodes { // locations. These processes are inherited from the ObjC++ version of this class and might be
guard let node = theAnchor.node else { // unnecessary in Swift, but this deduction requires further experiments.
continue for walkedNode in _walkedNodes {
} if let theNode = walkedNode.node {
let strNodeValue = theNode.currentKeyValue.value
let valueString = node.currentKeyValue.value composingBuffer += strNodeValue
composingBuffer += valueString let arrSplit: [NSString] = (strNodeValue as NSString).split()
let codepointCount = valueString.count let codepointCount = arrSplit.count
// This re-aligns the cursor index in the composed string
let spanningLength = theAnchor.spanningLength // (the actual cursor on the screen) with the builder's logical
// cursor (reading) cursor; each built node has a "spanning length"
// (e.g. two reading blocks has a spanning length of 2), and we
// accumulate those lengths to calculate the displayed cursor
// index.
let spanningLength: Int = walkedNode.spanningLength
if readingCursorIndex + spanningLength <= builderCursorIndex { if readingCursorIndex + spanningLength <= builderCursorIndex {
composedStringCursorIndex += valueString.count composedStringCursorIndex += (strNodeValue as NSString).length
readingCursorIndex += spanningLength readingCursorIndex += spanningLength
} else { } else {
if codepointCount == spanningLength { if codepointCount == spanningLength {
for _ in 0..<codepointCount { var i = 0
if readingCursorIndex < builderCursorIndex { while i < codepointCount, readingCursorIndex < builderCursorIndex {
composedStringCursorIndex += 1 composedStringCursorIndex += arrSplit[i].length
readingCursorIndex += 1 readingCursorIndex += 1
} i += 1
} }
} else { } else {
if readingCursorIndex < builderCursorIndex { if readingCursorIndex < builderCursorIndex {
composedStringCursorIndex += valueString.count composedStringCursorIndex += (strNodeValue as NSString).length
readingCursorIndex += spanningLength readingCursorIndex += spanningLength
if readingCursorIndex > builderCursorIndex { if readingCursorIndex > builderCursorIndex {
readingCursorIndex = builderCursorIndex readingCursorIndex = builderCursorIndex
@ -72,25 +76,14 @@ extension KeyHandler {
} }
} }
} }
}
// Now, we gather all the intel, separate the composing buffer to two parts (head and tail), // Now, we gather all the intel, separate the composing buffer to two parts (head and tail),
// and insert the reading text (the Mandarin syllable) in between them. // and insert the reading text (the Mandarin syllable) in between them.
// The reading text is what the user is typing. // The reading text is what the user is typing.
let head = String((composingBuffer as NSString).substring(to: composedStringCursorIndex))
var rawHead = ""
var rawEnd = ""
for (i, n) in composingBuffer.enumerated() {
if i < composedStringCursorIndex {
rawHead += String(n)
} else {
rawEnd += String(n)
}
}
let head = rawHead
let reading = _composer.getInlineCompositionForIMK(isHanyuPinyin: mgrPrefs.showHanyuPinyinInCompositionBuffer) let reading = _composer.getInlineCompositionForIMK(isHanyuPinyin: mgrPrefs.showHanyuPinyinInCompositionBuffer)
let tail = rawEnd let tail = String((composingBuffer as NSString).substring(from: composedStringCursorIndex))
let composedText = head + reading + tail let composedText = head + reading + tail
let cursorIndex = composedStringCursorIndex + reading.count let cursorIndex = composedStringCursorIndex + reading.count