ctlIME // Refine candidate processings for IMKCandidates.

This commit is contained in:
ShikiSuen 2022-08-15 09:22:02 +08:00
parent e7a320eef6
commit 37e1364f93
1 changed files with 26 additions and 56 deletions

View File

@ -236,34 +236,24 @@ class ctlInputMethod: IMKInputController {
override func candidates(_ sender: Any!) -> [Any]! {
_ = sender //
var arrResult = [String]()
//
func handleCandidatesPrepared(_ candidates: [(String, String)]) {
for theCandidate in candidates {
let theConverted = IME.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)(\(theCandidate.1))"
if arrResult.contains(result) {
result = "\(result)(\(theCandidate.0))"
}
arrResult.append(result)
}
}
if let state = state as? InputState.AssociatedPhrases {
for theCandidate in state.candidates {
let theConverted = IME.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)(\(theCandidate.1))"
if arrResult.contains(result) {
result = "\(result)(\(theCandidate.0))"
}
arrResult.append(result)
}
handleCandidatesPrepared(state.candidates)
} else if let state = state as? InputState.SymbolTable {
for theCandidate in state.candidates {
let theConverted = IME.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)(\(theCandidate.1))"
if arrResult.contains(result) {
result = "\(result)(\(theCandidate.0))"
}
arrResult.append(result)
}
handleCandidatesPrepared(state.candidates)
} else if let state = state as? InputState.ChoosingCandidate {
for theCandidate in state.candidates {
let theConverted = IME.kanjiConversionIfRequired(theCandidate.1)
var result = (theCandidate.1 == theConverted) ? theCandidate.1 : "\(theConverted)(\(theCandidate.1))"
if arrResult.contains(result) {
result = "\(result)(\(theCandidate.0))"
}
arrResult.append(result)
}
handleCandidatesPrepared(state.candidates)
}
return arrResult
}
@ -291,37 +281,9 @@ class ctlInputMethod: IMKInputController {
}
var indexDeducted = 0
//
if let state = state as? InputState.AssociatedPhrases {
for (i, neta) in state.candidates.enumerated() {
let theConverted = IME.kanjiConversionIfRequired(neta.1)
let netaShown = (neta.1 == theConverted) ? neta.1 : "\(theConverted)(\(neta.1))"
let netaShownWithPronunciation = "\(theConverted)(\(neta.0))"
if candidateString.string == netaShownWithPronunciation {
indexDeducted = i
break
}
if candidateString.string == netaShown {
indexDeducted = i
break
}
}
} else if let state = state as? InputState.SymbolTable {
for (i, neta) in state.candidates.enumerated() {
let theConverted = IME.kanjiConversionIfRequired(neta.1)
let netaShown = (neta.1 == theConverted) ? neta.1 : "\(theConverted)(\(neta.1))"
let netaShownWithPronunciation = "\(theConverted)(\(neta.0))"
if candidateString.string == netaShownWithPronunciation {
indexDeducted = i
break
}
if candidateString.string == netaShown {
indexDeducted = i
break
}
}
} else if let state = state as? InputState.ChoosingCandidate {
for (i, neta) in state.candidates.enumerated() {
func handleCandidatesSelected(_ candidates: [(String, String)]) {
for (i, neta) in candidates.enumerated() {
let theConverted = IME.kanjiConversionIfRequired(neta.1)
let netaShown = (neta.1 == theConverted) ? neta.1 : "\(theConverted)(\(neta.1))"
let netaShownWithPronunciation = "\(theConverted)(\(neta.0))"
@ -335,6 +297,14 @@ class ctlInputMethod: IMKInputController {
}
}
}
if let state = state as? InputState.AssociatedPhrases {
handleCandidatesSelected(state.candidates)
} else if let state = state as? InputState.SymbolTable {
handleCandidatesSelected(state.candidates)
} else if let state = state as? InputState.ChoosingCandidate {
handleCandidatesSelected(state.candidates)
}
keyHandler(
keyHandler,
didSelectCandidateAt: indexDeducted,