From cda914b5c4cd4040e57a444a61994764ed4ae48a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 21 May 2022 17:53:47 +0800 Subject: [PATCH] KeyHandler // Re-enable NSString in buildInputtingState(). --- .../ControllerModules/KeyHandler_States.swift | 79 +++++++++---------- 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index c20ef4a5..0f96f902 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -36,37 +36,42 @@ extension KeyHandler { // to "refresh" the text input buffer with our "composing text" var composingBuffer = "" var composedStringCursorIndex = 0 - var readingCursorIndex = 0 - let builderCursorIndex = builderCursorIndex - - for theAnchor in _walkedNodes { - guard let node = theAnchor.node else { - continue - } - - let valueString = node.currentKeyValue.value - composingBuffer += valueString - let codepointCount = valueString.count - - let spanningLength = theAnchor.spanningLength - if readingCursorIndex + spanningLength <= builderCursorIndex { - composedStringCursorIndex += valueString.count - readingCursorIndex += spanningLength - } else { - if codepointCount == spanningLength { - for _ in 0.. builderCursorIndex { - readingCursorIndex = builderCursorIndex + if codepointCount == spanningLength { + var i = 0 + while i < codepointCount, readingCursorIndex < builderCursorIndex { + composedStringCursorIndex += arrSplit[i].length + readingCursorIndex += 1 + i += 1 + } + } else { + if readingCursorIndex < builderCursorIndex { + composedStringCursorIndex += (strNodeValue as NSString).length + readingCursorIndex += spanningLength + if readingCursorIndex > builderCursorIndex { + readingCursorIndex = builderCursorIndex + } } } } @@ -76,21 +81,9 @@ extension KeyHandler { // 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. // The reading text is what the user is typing. - - var rawHead = "" - var rawEnd = "" - - for (i, n) in composingBuffer.enumerated() { - if i < composedStringCursorIndex { - rawHead += String(n) - } else { - rawEnd += String(n) - } - } - - let head = rawHead + let head = String((composingBuffer as NSString).substring(to: composedStringCursorIndex)) 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 cursorIndex = composedStringCursorIndex + reading.count