KeyHandler // Reduce useless symbols.

This commit is contained in:
ShikiSuen 2022-07-22 16:03:46 +08:00
parent 7dceb5e533
commit 042f96d73d
4 changed files with 43 additions and 84 deletions

View File

@ -103,7 +103,7 @@ class KeyHandler {
///
/// 使 Node Crossing
var actualCandidateCursor: Int {
mgrPrefs.useRearCursorMode ? min(compositorCursorIndex, compositorLength - 1) : max(compositorCursorIndex, 1)
mgrPrefs.useRearCursorMode ? min(compositor.cursor, compositor.length - 1) : max(compositor.cursor, 1)
}
///
@ -164,7 +164,7 @@ class KeyHandler {
/// - respectCursorPushing: true
func fixNode(candidate: (String, String), respectCursorPushing: Bool = true) {
let theCandidate: Megrez.KeyValuePaired = .init(key: candidate.0, value: candidate.1)
let adjustedCursor = max(0, min(actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositorLength))
let adjustedCursor = max(0, min(actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositor.length))
//
let selectedNode: Megrez.NodeAnchor = compositor.fixNodeWithCandidate(theCandidate, at: adjustedCursor)
//
@ -254,7 +254,7 @@ class KeyHandler {
///
func fetchSuggestedCandidates() -> [Megrez.Unigram] {
currentUOM.suggest(
walkedAnchors: walkedAnchors, cursorIndex: compositorCursorIndex,
walkedAnchors: walkedAnchors, cursorIndex: compositor.cursor,
timestamp: NSDate().timeIntervalSince1970
)
}
@ -273,7 +273,7 @@ class KeyHandler {
IME.prtDebugIntel(
"UOM: Suggestion retrieved, overriding the node score of the selected candidate.")
compositor.overrideNodeScoreForSelectedCandidate(
location: min(actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositorLength),
location: min(actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositor.length),
value: overrideValue,
overridingScore: findHighestScore(nodeAnchors: rawAnchorsOfNodes, epsilon: kEpsilon)
)
@ -337,9 +337,6 @@ class KeyHandler {
// MARK: - Extracted methods and functions (Megrez).
///
var isCompositorEmpty: Bool { compositor.isEmpty }
///
var rawAnchorsOfNodes: [Megrez.NodeAnchor] {
/// 使 nodesCrossing macOS
@ -362,44 +359,6 @@ class KeyHandler {
compositor = Megrez.Compositor(lm: currentLM, separator: "-")
}
///
var currentReadings: [String] { compositor.readings }
///
func ifLangModelHasUnigrams(forKey reading: String) -> Bool {
currentLM.hasUnigramsFor(key: reading)
}
///
func insertToCompositorAtCursor(reading: String) {
compositor.insertReading(reading)
}
///
var compositorCursorIndex: Int {
get { compositor.cursor }
set { compositor.cursor = newValue }
}
///
var compositorLength: Int {
compositor.length
}
///
///
/// Rear
func deleteCompositorReadingAtTheRearOfCursor() {
compositor.dropReading(direction: .rear)
}
///
///
/// Front
func deleteCompositorReadingToTheFrontOfCursor() {
compositor.dropReading(direction: .front)
}
///
/// - Parameter input:
/// - Returns:

View File

@ -61,12 +61,12 @@ extension KeyHandler {
if cancelCandidateKey {
if (state is InputState.AssociatedPhrases)
|| mgrPrefs.useSCPCTypingMode
|| isCompositorEmpty
|| compositor.isEmpty
{
//
//
// 使 BackSpace
// isCompositorEmpty
// compositor.isEmpty
clear()
stateCallback(InputState.EmptyIgnoringPreviousState())
} else {
@ -326,12 +326,12 @@ extension KeyHandler {
let punctuation: String = arrPunctuations.joined(separator: "")
var shouldAutoSelectCandidate: Bool =
composer.inputValidityCheck(key: charCode) || ifLangModelHasUnigrams(forKey: customPunctuation)
|| ifLangModelHasUnigrams(forKey: punctuation)
composer.inputValidityCheck(key: charCode) || currentLM.hasUnigramsFor(key: customPunctuation)
|| currentLM.hasUnigramsFor(key: punctuation)
if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey {
let letter: String! = String(format: "%@%c", "_letter_", CChar(charCode))
if ifLangModelHasUnigrams(forKey: letter) { shouldAutoSelectCandidate = true }
if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true }
}
if shouldAutoSelectCandidate {

View File

@ -188,7 +188,7 @@ extension KeyHandler {
//
//
if !ifLangModelHasUnigrams(forKey: reading) {
if !currentLM.hasUnigramsFor(key: reading) {
IME.prtDebugIntel("B49C0979語彙庫內無「\(reading)」的匹配記錄。")
errorCallback()
composer.clear()
@ -198,7 +198,7 @@ extension KeyHandler {
}
//
insertToCompositorAtCursor(reading: reading)
compositor.insertReading(reading)
//
let textToCommit = commitOverflownCompositionAndWalk
@ -270,7 +270,7 @@ extension KeyHandler {
if input.isSpace {
/// Space
if !mgrPrefs.chooseCandidateUsingSpace {
if compositorCursorIndex >= compositorLength {
if compositor.cursor >= compositor.length {
let composingBuffer = currentState.composingBuffer
if !composingBuffer.isEmpty {
stateCallback(InputState.Committing(textToCommit: composingBuffer))
@ -278,8 +278,8 @@ extension KeyHandler {
clear()
stateCallback(InputState.Committing(textToCommit: " "))
stateCallback(InputState.Empty())
} else if ifLangModelHasUnigrams(forKey: " ") {
insertToCompositorAtCursor(reading: " ")
} else if currentLM.hasUnigramsFor(key: " ") {
compositor.insertReading(" ")
let textToCommit = commitOverflownCompositionAndWalk
let inputting = buildInputtingState
inputting.textToCommit = textToCommit
@ -396,9 +396,9 @@ extension KeyHandler {
if input.isSymbolMenuPhysicalKey && !input.isShiftHold {
if input.isOptionHold {
if ifLangModelHasUnigrams(forKey: "_punctuation_list") {
if currentLM.hasUnigramsFor(key: "_punctuation_list") {
if composer.isEmpty {
insertToCompositorAtCursor(reading: "_punctuation_list")
compositor.insertReading("_punctuation_list")
let textToCommit: String! = commitOverflownCompositionAndWalk
let inputting = buildInputtingState
inputting.textToCommit = textToCommit

View File

@ -54,35 +54,35 @@ extension KeyHandler {
/// NodeAnchorspanningLength
///
let spanningLength: Int = theAnchor.spanLength
if readingCursorIndex + spanningLength <= compositorCursorIndex {
if readingCursorIndex + spanningLength <= compositor.cursor {
composedStringCursorIndex += strNodeValue.utf16.count
readingCursorIndex += spanningLength
} else {
if codepointCount == spanningLength {
var i = 0
while i < codepointCount, readingCursorIndex < compositorCursorIndex {
while i < codepointCount, readingCursorIndex < compositor.cursor {
composedStringCursorIndex += arrSplit[i].utf16.count
readingCursorIndex += 1
i += 1
}
} else {
if readingCursorIndex < compositorCursorIndex {
if readingCursorIndex < compositor.cursor {
composedStringCursorIndex += strNodeValue.utf16.count
readingCursorIndex += spanningLength
readingCursorIndex = min(readingCursorIndex, compositorCursorIndex)
readingCursorIndex = min(readingCursorIndex, compositor.cursor)
///
///
///
///
///
switch compositorCursorIndex {
switch compositor.cursor {
case compositor.readings.count...:
tooltipParameterRef[0] = compositor.readings[compositorCursorIndex - 1]
tooltipParameterRef[0] = compositor.readings[compositor.cursor - 1]
case 0:
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
tooltipParameterRef[1] = compositor.readings[compositor.cursor]
default:
tooltipParameterRef[0] = compositor.readings[compositorCursorIndex - 1]
tooltipParameterRef[1] = compositor.readings[compositorCursorIndex]
tooltipParameterRef[0] = compositor.readings[compositor.cursor - 1]
tooltipParameterRef[1] = compositor.readings[compositor.cursor]
}
///
for (i, _) in tooltipParameterRef.enumerated() {
@ -301,7 +301,7 @@ extension KeyHandler {
stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void
) -> Bool {
if !ifLangModelHasUnigrams(forKey: customPunctuation) {
if !currentLM.hasUnigramsFor(key: customPunctuation) {
return false
}
@ -313,7 +313,7 @@ extension KeyHandler {
return true
}
insertToCompositorAtCursor(reading: customPunctuation)
compositor.insertReading(customPunctuation)
let textToCommit = commitOverflownCompositionAndWalk
let inputting = buildInputtingState
inputting.textToCommit = textToCommit
@ -372,7 +372,7 @@ extension KeyHandler {
) -> Bool {
guard state is InputState.Inputting else { return false }
var composingBuffer = currentReadings.joined(separator: "-")
var composingBuffer = compositor.readings.joined(separator: "-")
if mgrPrefs.inlineDumpPinyinInLieuOfZhuyin {
composingBuffer = Tekkon.restoreToneOneInZhuyinKey(target: composingBuffer) //
composingBuffer = Tekkon.cnvPhonaToHanyuPinyin(target: composingBuffer) //
@ -445,8 +445,8 @@ extension KeyHandler {
if composer.hasToneMarker(withNothingElse: true) {
composer.clear()
} else if composer.isEmpty {
if compositorCursorIndex > 0 {
deleteCompositorReadingAtTheRearOfCursor()
if compositor.cursor > 0 {
compositor.dropReading(direction: .rear)
walk()
} else {
IME.prtDebugIntel("9D69908D")
@ -485,14 +485,14 @@ extension KeyHandler {
return true
}
guard compositorCursorIndex != compositorLength else {
guard compositor.cursor != compositor.length else {
IME.prtDebugIntel("9B69938D")
errorCallback()
stateCallback(state)
return true
}
deleteCompositorReadingToTheFrontOfCursor()
compositor.dropReading(direction: .front)
walk()
let inputting = buildInputtingState
// count > 0!isEmpty滿
@ -544,8 +544,8 @@ extension KeyHandler {
return true
}
if compositorCursorIndex != 0 {
compositorCursorIndex = 0
if compositor.cursor != 0 {
compositor.cursor = 0
stateCallback(buildInputtingState)
} else {
IME.prtDebugIntel("66D97F90")
@ -578,8 +578,8 @@ extension KeyHandler {
return true
}
if compositorCursorIndex != compositorLength {
compositorCursorIndex = compositorLength
if compositor.cursor != compositor.length {
compositor.cursor = compositor.length
stateCallback(buildInputtingState)
} else {
IME.prtDebugIntel("9B69908E")
@ -650,7 +650,7 @@ extension KeyHandler {
composingBuffer: currentState.composingBuffer,
cursorIndex: currentState.cursorIndex,
markerIndex: nextPosition,
readings: currentReadings
readings: compositor.readings
)
marking.tooltipForInputting = currentState.tooltip
stateCallback(marking)
@ -672,8 +672,8 @@ extension KeyHandler {
}
stateCallback(buildInputtingState)
} else {
if compositorCursorIndex < compositorLength {
compositorCursorIndex += 1
if compositor.cursor < compositor.length {
compositor.cursor += 1
stateCallback(buildInputtingState)
} else {
IME.prtDebugIntel("A96AAD58")
@ -718,7 +718,7 @@ extension KeyHandler {
composingBuffer: currentState.composingBuffer,
cursorIndex: currentState.cursorIndex,
markerIndex: previousPosition,
readings: currentReadings
readings: compositor.readings
)
marking.tooltipForInputting = currentState.tooltip
stateCallback(marking)
@ -740,8 +740,8 @@ extension KeyHandler {
}
stateCallback(buildInputtingState)
} else {
if compositorCursorIndex > 0 {
compositorCursorIndex -= 1
if compositor.cursor > 0 {
compositor.cursor -= 1
stateCallback(buildInputtingState)
} else {
IME.prtDebugIntel("7045E6F3")
@ -795,7 +795,7 @@ extension KeyHandler {
var length = 0
var currentAnchor = Megrez.NodeAnchor()
let cursorIndex = min(
actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositorLength
actualCandidateCursor + (mgrPrefs.useRearCursorMode ? 1 : 0), compositor.length
)
for anchor in walkedAnchors {
length += anchor.spanLength