KeyHandler // Use guard-let instead in some functions.
This commit is contained in:
parent
f768d6c603
commit
f4894e5828
|
@ -249,13 +249,12 @@ class KeyHandler {
|
||||||
|
|
||||||
// 將節錨內的候選字詞資料拓印到輸出陣列內。
|
// 將節錨內的候選字詞資料拓印到輸出陣列內。
|
||||||
for currentNodeAnchor in arrAnchors {
|
for currentNodeAnchor in arrAnchors {
|
||||||
if let currentNode = currentNodeAnchor.node {
|
guard let currentNode = currentNodeAnchor.node else { continue }
|
||||||
for currentCandidate in currentNode.candidates {
|
for currentCandidate in currentNode.candidates {
|
||||||
// 選字窗的內容的康熙轉換 / JIS 轉換不能放在這裡處理,會影響選字有效性。
|
// 選字窗的內容的康熙轉換 / JIS 轉換不能放在這裡處理,會影響選字有效性。
|
||||||
// 選字的原理是拿著具體的候選字詞的字串去當前的節錨下找出對應的候選字詞(X元圖)。
|
// 選字的原理是拿著具體的候選字詞的字串去當前的節錨下找出對應的候選字詞(X元圖)。
|
||||||
// 一旦在這裡轉換了,節錨內的某些元圖就無法被選中。
|
// 一旦在這裡轉換了,節錨內的某些元圖就無法被選中。
|
||||||
arrCandidates.append(currentCandidate.value)
|
arrCandidates.append(currentCandidate.value)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 決定是否根據半衰記憶模組的建議來調整候選字詞的順序。
|
// 決定是否根據半衰記憶模組的建議來調整候選字詞的順序。
|
||||||
|
|
|
@ -45,47 +45,46 @@ extension KeyHandler {
|
||||||
/// 所以在這裡必須做糾偏處理。因為在用 Swift,所以可以用「.utf16」取代「NSString.length()」。
|
/// 所以在這裡必須做糾偏處理。因為在用 Swift,所以可以用「.utf16」取代「NSString.length()」。
|
||||||
/// 這樣就可以免除不必要的類型轉換。
|
/// 這樣就可以免除不必要的類型轉換。
|
||||||
for theAnchor in walkedAnchors {
|
for theAnchor in walkedAnchors {
|
||||||
if let theNode = theAnchor.node {
|
guard let theNode = theAnchor.node else { continue }
|
||||||
let strNodeValue = theNode.currentKeyValue.value
|
let strNodeValue = theNode.currentKeyValue.value
|
||||||
composingBuffer += strNodeValue
|
composingBuffer += strNodeValue
|
||||||
let arrSplit: [String] = Array(strNodeValue).map { String($0) }
|
let arrSplit: [String] = Array(strNodeValue).map { String($0) }
|
||||||
let codepointCount = arrSplit.count
|
let codepointCount = arrSplit.count
|
||||||
/// 藉下述步驟重新將「可見游標位置」對齊至「組字器內的游標所在的讀音位置」。
|
/// 藉下述步驟重新將「可見游標位置」對齊至「組字器內的游標所在的讀音位置」。
|
||||||
/// 每個節錨(NodeAnchor)都有自身的幅位長度(spanningLength),可以用來
|
/// 每個節錨(NodeAnchor)都有自身的幅位長度(spanningLength),可以用來
|
||||||
/// 累加、以此為依據,來校正「可見游標位置」。
|
/// 累加、以此為依據,來校正「可見游標位置」。
|
||||||
let spanningLength: Int = theAnchor.spanningLength
|
let spanningLength: Int = theAnchor.spanningLength
|
||||||
if readingCursorIndex + spanningLength <= compositorCursorIndex {
|
if readingCursorIndex + spanningLength <= compositorCursorIndex {
|
||||||
composedStringCursorIndex += strNodeValue.utf16.count
|
composedStringCursorIndex += strNodeValue.utf16.count
|
||||||
readingCursorIndex += spanningLength
|
readingCursorIndex += spanningLength
|
||||||
|
} else {
|
||||||
|
if codepointCount == spanningLength {
|
||||||
|
var i = 0
|
||||||
|
while i < codepointCount, readingCursorIndex < compositorCursorIndex {
|
||||||
|
composedStringCursorIndex += arrSplit[i].utf16.count
|
||||||
|
readingCursorIndex += 1
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if codepointCount == spanningLength {
|
if readingCursorIndex < compositorCursorIndex {
|
||||||
var i = 0
|
composedStringCursorIndex += strNodeValue.utf16.count
|
||||||
while i < codepointCount, readingCursorIndex < compositorCursorIndex {
|
readingCursorIndex += spanningLength
|
||||||
composedStringCursorIndex += arrSplit[i].utf16.count
|
readingCursorIndex = min(readingCursorIndex, compositorCursorIndex)
|
||||||
readingCursorIndex += 1
|
/// 接下來再處理這麼一種情況:
|
||||||
i += 1
|
/// 某些錨點內的當前候選字詞長度與讀音長度不相等。
|
||||||
}
|
/// 但此時游標還是按照每個讀音單位來移動的,
|
||||||
} else {
|
/// 所以需要上下文工具提示來顯示游標的相對位置。
|
||||||
if readingCursorIndex < compositorCursorIndex {
|
/// 這裡先計算一下要用在工具提示當中的顯示參數的內容。
|
||||||
composedStringCursorIndex += strNodeValue.utf16.count
|
switch compositorCursorIndex {
|
||||||
readingCursorIndex += spanningLength
|
case compositor.readings.count...:
|
||||||
readingCursorIndex = min(readingCursorIndex, compositorCursorIndex)
|
tooltipParameterRef[0] = compositor.readings[compositor.readings.count - 1]
|
||||||
/// 接下來再處理這麼一種情況:
|
case 0:
|
||||||
/// 某些錨點內的當前候選字詞長度與讀音長度不相等。
|
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
|
||||||
/// 但此時游標還是按照每個讀音單位來移動的,
|
default:
|
||||||
/// 所以需要上下文工具提示來顯示游標的相對位置。
|
do {
|
||||||
/// 這裡先計算一下要用在工具提示當中的顯示參數的內容。
|
tooltipParameterRef[0] = compositor.readings[compositorCursorIndex - 1]
|
||||||
switch compositorCursorIndex {
|
|
||||||
case compositor.readings.count...:
|
|
||||||
tooltipParameterRef[0] = compositor.readings[compositor.readings.count - 1]
|
|
||||||
case 0:
|
|
||||||
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
|
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
|
||||||
default:
|
}
|
||||||
do {
|
|
||||||
tooltipParameterRef[0] = compositor.readings[compositorCursorIndex - 1]
|
|
||||||
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue