SessionCtl // Patch fixIndexForIMKCandidates(), etc.

This commit is contained in:
ShikiSuen 2023-06-24 16:59:12 +08:00
parent f346ff198e
commit 234c2656b7
2 changed files with 19 additions and 13 deletions

View File

@ -71,8 +71,8 @@ public protocol InputHandlerDelegate {
func candidateSelectionConfirmedByInputHandler(at index: Int)
func setInlineDisplayWithCursor()
func updatePopupDisplayWithCursor()
func performUserPhraseOperation(addToFilter: Bool)
-> Bool
func performUserPhraseOperation(addToFilter: Bool) -> Bool
func deductCandidateIndex(from candidateString: String) -> Int
}
// MARK: - (Kernel).

View File

@ -74,13 +74,11 @@ public extension SessionCtl {
/// IMK
/// - Parameter currentSelection:
override func candidateSelectionChanged(_ currentSelection: NSAttributedString!) {
guard state.isCandidateContainer else { return }
guard let candidateString = currentSelection?.string, !candidateString.isEmpty else { return }
// Handle candidatePairHighlightChanged().
var indexDeducted = 0
fixIndexForIMKCandidates(&indexDeducted, source: candidateString)
if state.type == .ofCandidates {
candidatePairHighlightChanged(at: indexDeducted)
}
let indexDeducted = deductCandidateIndex(from: candidateString)
candidatePairHighlightChanged(at: indexDeducted)
let realCandidateString = state.candidates[indexDeducted].value
// Handle IMK Annotation... We just use this to tell Apple that this never works in IMKCandidates.
DispatchQueue.main.async { [self] in
@ -97,6 +95,7 @@ public extension SessionCtl {
/// - Remark: IMK API Confirm Selection
/// - Parameter candidateString:
override func candidateSelected(_ candidateString: NSAttributedString!) {
guard state.isCandidateContainer else { return }
let candidateString: String = candidateString?.string ?? ""
if state.type == .ofAssociates {
// Shift+
@ -106,13 +105,18 @@ public extension SessionCtl {
}
}
let indexDeducted = deductCandidateIndex(from: candidateString)
candidatePairSelectionConfirmed(at: indexDeducted)
}
func deductCandidateIndex(from candidateString: String) -> Int {
var indexDeducted = 0
// / JIS 使
func fixSymbolIndexForIMKCandidates() {
for (i, neta) in state.candidates.enumerated() {
if candidateString == neta.value {
indexDeducted = i
indexDeducted = min(i, state.candidates.count - 1)
break
}
}
@ -126,7 +130,7 @@ public extension SessionCtl {
case .ofSymbolTable:
fixSymbolIndexForIMKCandidates()
case .ofCandidates:
guard !state.candidates.isEmpty else { return }
guard !state.candidates.isEmpty else { break }
if state.candidates[0].keyArray.description.contains("_punctuation") {
fixSymbolIndexForIMKCandidates() //
} else {
@ -134,11 +138,11 @@ public extension SessionCtl {
}
default: break
}
candidatePairSelectionConfirmed(at: indexDeducted)
return indexDeducted
}
/// IMKCandidates
/// - Remark: `\u{1A`便 IMEState
/// - Remark: `\u{1A}`便 IMEState
/// - Parameters:
/// - prefix:
/// - indexToFix:
@ -146,8 +150,10 @@ public extension SessionCtl {
private func fixIndexForIMKCandidates(
_ indexDeducted: inout Int, prefix: String = "", source candidateString: String
) {
guard state.isCandidateContainer else { return }
guard let separator = inputHandler?.keySeparator else { return }
let candidates = state.candidates
let maxIndex = candidates.count - 1
for (i, neta) in candidates.enumerated() {
let theConverted = ChineseConverter.kanjiConversionIfRequired(neta.value)
let netaShown = (neta.value == theConverted)
@ -169,11 +175,11 @@ public extension SessionCtl {
: neta.keyArray.joined(separator: separator))
let netaShownWithPronunciation = "\(netaShown)\u{17}(\(reading))"
if candidateString == prefix + netaShownWithPronunciation {
indexDeducted = i
indexDeducted = min(i, maxIndex)
break
}
if candidateString == prefix + netaShown {
indexDeducted = i
indexDeducted = min(i, maxIndex)
break
}
}