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 candidateSelectionConfirmedByInputHandler(at index: Int)
func setInlineDisplayWithCursor() func setInlineDisplayWithCursor()
func updatePopupDisplayWithCursor() func updatePopupDisplayWithCursor()
func performUserPhraseOperation(addToFilter: Bool) func performUserPhraseOperation(addToFilter: Bool) -> Bool
-> Bool func deductCandidateIndex(from candidateString: String) -> Int
} }
// MARK: - (Kernel). // MARK: - (Kernel).

View File

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