KeyHandler // Deprecating NSString in buildInputtingState().
This commit is contained in:
parent
f445462026
commit
164e8d7b32
|
@ -37,58 +37,60 @@ extension KeyHandler {
|
||||||
var composingBuffer = ""
|
var composingBuffer = ""
|
||||||
var composedStringCursorIndex = 0
|
var composedStringCursorIndex = 0
|
||||||
|
|
||||||
var readingCursorIndex: size_t = 0
|
var readingCursorIndex = 0
|
||||||
let builderCursorIndex: size_t = getBuilderCursorIndex()
|
let builderCursorIndex = getBuilderCursorIndex()
|
||||||
|
|
||||||
// We must do some Unicode codepoint counting to find the actual cursor location for the client
|
for theAnchor in _walkedNodes {
|
||||||
// i.e. we need to take UTF-16 into consideration, for which a surrogate pair takes 2 UniChars
|
guard let node = theAnchor.node else {
|
||||||
// locations. These processes are inherited from the ObjC++ version of this class and might be
|
continue
|
||||||
// unnecessary in Swift, but this deduction requires further experiments.
|
}
|
||||||
for walkedNode in _walkedNodes {
|
|
||||||
if let theNode = walkedNode.node {
|
|
||||||
let strNodeValue = theNode.currentKeyValue().value
|
|
||||||
composingBuffer += strNodeValue
|
|
||||||
|
|
||||||
let arrSplit: [NSString] = (strNodeValue as NSString).split()
|
let valueString = node.currentKeyValue().value
|
||||||
let codepointCount = arrSplit.count
|
composingBuffer += valueString
|
||||||
|
let codepointCount = valueString.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
|
if readingCursorIndex + spanningLength <= builderCursorIndex {
|
||||||
// cursor (reading) cursor; each built node has a "spanning length"
|
composedStringCursorIndex += valueString.count
|
||||||
// (e.g. two reading blocks has a spanning length of 2), and we
|
readingCursorIndex += spanningLength
|
||||||
// accumulate those lengths to calculate the displayed cursor
|
} else {
|
||||||
// index.
|
if codepointCount == spanningLength {
|
||||||
let spanningLength: Int = walkedNode.spanningLength
|
for _ in 0..<codepointCount {
|
||||||
if readingCursorIndex + spanningLength <= builderCursorIndex {
|
|
||||||
composedStringCursorIndex += (strNodeValue as NSString).length
|
|
||||||
readingCursorIndex += spanningLength
|
|
||||||
} else {
|
|
||||||
if codepointCount == spanningLength {
|
|
||||||
var i = 0
|
|
||||||
while i < codepointCount, readingCursorIndex < builderCursorIndex {
|
|
||||||
composedStringCursorIndex += arrSplit[i].length
|
|
||||||
readingCursorIndex += 1
|
|
||||||
i += 1
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if readingCursorIndex < builderCursorIndex {
|
if readingCursorIndex < builderCursorIndex {
|
||||||
composedStringCursorIndex += (strNodeValue as NSString).length
|
composedStringCursorIndex += 1
|
||||||
readingCursorIndex += spanningLength
|
readingCursorIndex += 1
|
||||||
if readingCursorIndex > builderCursorIndex {
|
}
|
||||||
readingCursorIndex = builderCursorIndex
|
}
|
||||||
}
|
} else {
|
||||||
|
if readingCursorIndex < builderCursorIndex {
|
||||||
|
composedStringCursorIndex += valueString.count
|
||||||
|
readingCursorIndex += spanningLength
|
||||||
|
if readingCursorIndex > builderCursorIndex {
|
||||||
|
readingCursorIndex = builderCursorIndex
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.getDisplayedComposition()
|
let reading = _composer.getDisplayedComposition()
|
||||||
let tail = String((composingBuffer as NSString).substring(from: composedStringCursorIndex))
|
let tail = rawEnd
|
||||||
let composedText = head + reading + tail
|
let composedText = head + reading + tail
|
||||||
let cursorIndex = composedStringCursorIndex + reading.count
|
let cursorIndex = composedStringCursorIndex + reading.count
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue