KeyHandler // Refactor if-let to guard-let for state conditioning.

This commit is contained in:
ShikiSuen 2022-05-15 14:55:36 +08:00
parent 6d6e50ab8f
commit 6e6cce7f4f
1 changed files with 62 additions and 86 deletions

View File

@ -256,16 +256,10 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard let currentState = state as? InputState.Inputting else { return false }
return false
}
clear() clear()
stateCallback(InputState.Committing(poppedText: currentState.composingBuffer))
if let current = state as? InputState.Inputting {
stateCallback(InputState.Committing(poppedText: current.composingBuffer))
}
stateCallback(InputState.Empty()) stateCallback(InputState.Empty())
return true return true
} }
@ -277,9 +271,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
var composingBuffer = currentReadings().joined(separator: "-") var composingBuffer = currentReadings().joined(separator: "-")
if mgrPrefs.inlineDumpPinyinInLieuOfZhuyin { if mgrPrefs.inlineDumpPinyinInLieuOfZhuyin {
@ -305,9 +297,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
var composed = "" var composed = ""
@ -346,9 +336,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
if _composer.hasToneMarker(withNothingElse: true) { if _composer.hasToneMarker(withNothingElse: true) {
_composer.clear() _composer.clear()
@ -381,9 +369,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
if _composer.isEmpty { if _composer.isEmpty {
if getBuilderCursorIndex() != getBuilderLength() { if getBuilderCursorIndex() != getBuilderLength() {
@ -417,9 +403,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
if !_composer.isEmpty { if !_composer.isEmpty {
IME.prtDebugIntel("9B6F908D") IME.prtDebugIntel("9B6F908D")
errorCallback() errorCallback()
@ -435,9 +419,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
if !_composer.isEmpty { if !_composer.isEmpty {
IME.prtDebugIntel("ABC44080") IME.prtDebugIntel("ABC44080")
@ -465,9 +447,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { guard state is InputState.Inputting else { return false }
return false
}
if !_composer.isEmpty { if !_composer.isEmpty {
IME.prtDebugIntel("9B69908D") IME.prtDebugIntel("9B69908D")
@ -495,7 +475,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { return false } guard state is InputState.Inputting else { return false }
let escToClearInputBufferEnabled: Bool = mgrPrefs.escToCleanInputBuffer let escToClearInputBufferEnabled: Bool = mgrPrefs.escToCleanInputBuffer
@ -528,7 +508,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { return false } guard let currentState = state as? InputState.Inputting else { return false }
if !_composer.isEmpty { if !_composer.isEmpty {
IME.prtDebugIntel("B3BA5257") IME.prtDebugIntel("B3BA5257")
@ -537,7 +517,6 @@ extension KeyHandler {
return true return true
} }
if let currentState = state as? InputState.Inputting {
if input.isShiftHold { if input.isShiftHold {
// Shift + Right // Shift + Right
if currentState.cursorIndex < (currentState.composingBuffer as NSString).length { if currentState.cursorIndex < (currentState.composingBuffer as NSString).length {
@ -566,7 +545,6 @@ extension KeyHandler {
stateCallback(state) stateCallback(state)
} }
} }
}
return true return true
} }
@ -579,7 +557,7 @@ extension KeyHandler {
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
if !(state is InputState.Inputting) { return false } guard let currentState = state as? InputState.Inputting else { return false }
if !_composer.isEmpty { if !_composer.isEmpty {
IME.prtDebugIntel("6ED95318") IME.prtDebugIntel("6ED95318")
@ -588,7 +566,6 @@ extension KeyHandler {
return true return true
} }
if let currentState = state as? InputState.Inputting {
if input.isShiftHold { if input.isShiftHold {
// Shift + left // Shift + left
if currentState.cursorIndex > 0 { if currentState.cursorIndex > 0 {
@ -617,7 +594,6 @@ extension KeyHandler {
stateCallback(state) stateCallback(state)
} }
} }
}
return true return true
} }