KeyHandler // Updating nomenclatures to fit Swift conventions.

This commit is contained in:
ShikiSuen 2022-04-19 15:20:11 +08:00
parent 4145c15920
commit fcb891affa
5 changed files with 64 additions and 61 deletions

View File

@ -167,7 +167,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
// NON-SWIFTIFIABLE // NON-SWIFTIFIABLE
- (void)fixNodeWithValue:(NSString *)value - (void)fixNodeWithValue:(NSString *)value
{ {
NSInteger cursorIndex = [self _actualCandidateCursorIndex]; NSInteger cursorIndex = [self getActualCandidateCursorIndex];
std::string stringValue(value.UTF8String); std::string stringValue(value.UTF8String);
Gramambular::NodeAnchor selectedNode = _builder->grid().fixNodeSelectedCandidate(cursorIndex, stringValue); Gramambular::NodeAnchor selectedNode = _builder->grid().fixNodeSelectedCandidate(cursorIndex, stringValue);
if (!mgrPrefs.useSCPCTypingMode) if (!mgrPrefs.useSCPCTypingMode)
@ -564,7 +564,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
if (!overrideValue.empty()) if (!overrideValue.empty())
{ {
NSInteger cursorIndex = [self _actualCandidateCursorIndex]; NSInteger cursorIndex = [self getActualCandidateCursorIndex];
std::vector<Gramambular::NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex); std::vector<Gramambular::NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
double highestScore = FindHighestScore(nodes, kEpsilon); double highestScore = FindHighestScore(nodes, kEpsilon);
_builder->grid().overrideNodeScoreForSelectedCandidate(cursorIndex, overrideValue, _builder->grid().overrideNodeScoreForSelectedCandidate(cursorIndex, overrideValue,
@ -601,7 +601,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
{ {
NSMutableArray<NSString *> *candidatesArray = [[NSMutableArray alloc] init]; NSMutableArray<NSString *> *candidatesArray = [[NSMutableArray alloc] init];
NSInteger cursorIndex = [self _actualCandidateCursorIndex]; NSInteger cursorIndex = [self getActualCandidateCursorIndex];
std::vector<Gramambular::NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex); std::vector<Gramambular::NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
// sort the nodes, so that longer nodes (representing longer phrases) are placed at the top of the candidate list // sort the nodes, so that longer nodes (representing longer phrases) are placed at the top of the candidate list

View File

@ -29,8 +29,8 @@ import Cocoa
// MARK: - § Handle Candidate State. // MARK: - § Handle Candidate State.
@objc extension KeyHandler { @objc extension KeyHandler {
func _handleCandidateState( func handleCandidate(
_ state: InputState, state: InputState,
input: InputHandler, input: InputHandler,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void

View File

@ -105,16 +105,16 @@ import Cocoa
// MARK: Handle Candidates. // MARK: Handle Candidates.
if state is InputState.ChoosingCandidate { if state is InputState.ChoosingCandidate {
return _handleCandidateState( return handleCandidate(
state, input: input, stateCallback: stateCallback, errorCallback: errorCallback state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
) )
} }
// MARK: Handle Associated Phrases. // MARK: Handle Associated Phrases.
if state is InputState.AssociatedPhrases { if state is InputState.AssociatedPhrases {
let result = _handleCandidateState( let result = handleCandidate(
state, input: input, stateCallback: stateCallback, errorCallback: errorCallback state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
) )
if result { if result {
return true return true
@ -126,7 +126,7 @@ import Cocoa
// MARK: Handle Marking. // MARK: Handle Marking.
if let marking = state as? InputState.Marking { if let marking = state as? InputState.Marking {
if _handleMarkingState( if handleMarkingState(
marking, input: input, stateCallback: stateCallback, marking, input: input, stateCallback: stateCallback,
errorCallback: errorCallback errorCallback: errorCallback
) { ) {
@ -188,8 +188,8 @@ import Cocoa
stateCallback(inputting) stateCallback(inputting)
if mgrPrefs.useSCPCTypingMode { if mgrPrefs.useSCPCTypingMode {
let choosingCandidates: InputState.ChoosingCandidate = _buildCandidateState( let choosingCandidates: InputState.ChoosingCandidate = buildCandidate(
inputting, state: inputting,
useVerticalMode: input.useVerticalMode useVerticalMode: input.useVerticalMode
) )
if choosingCandidates.candidates.count == 1 { if choosingCandidates.candidates.count == 1 {
@ -251,8 +251,8 @@ import Cocoa
return true return true
} }
} }
let choosingCandidates = _buildCandidateState( let choosingCandidates = buildCandidate(
currentState, state: currentState,
useVerticalMode: input.useVerticalMode useVerticalMode: input.useVerticalMode
) )
stateCallback(choosingCandidates) stateCallback(choosingCandidates)
@ -264,13 +264,13 @@ import Cocoa
// MARK: Esc // MARK: Esc
if input.isESC { return _handleEscWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) } if input.isESC { return handleEsc(state: state, stateCallback: stateCallback, errorCallback: errorCallback) }
// MARK: Cursor backward // MARK: Cursor backward
if input.isCursorBackward || input.emacsKey == vChewingEmacsKey.backward { if input.isCursorBackward || input.emacsKey == vChewingEmacsKey.backward {
return _handleBackwardWithState( return handleBackward(
state, state: state,
input: input, input: input,
stateCallback: stateCallback, stateCallback: stateCallback,
errorCallback: errorCallback errorCallback: errorCallback
@ -280,59 +280,59 @@ import Cocoa
// MARK: Cursor forward // MARK: Cursor forward
if input.isCursorForward || input.emacsKey == vChewingEmacsKey.forward { if input.isCursorForward || input.emacsKey == vChewingEmacsKey.forward {
return _handleForwardWithState( return handleForward(
state, input: input, stateCallback: stateCallback, errorCallback: errorCallback state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
) )
} }
// MARK: Home // MARK: Home
if input.isHome || input.emacsKey == vChewingEmacsKey.home { if input.isHome || input.emacsKey == vChewingEmacsKey.home {
return _handleHomeWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleHome(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: End // MARK: End
if input.isEnd || input.emacsKey == vChewingEmacsKey.end { if input.isEnd || input.emacsKey == vChewingEmacsKey.end {
return _handleEndWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Ctrl+PgLf or Shift+PgLf // MARK: Ctrl+PgLf or Shift+PgLf
if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isLeft) { if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isLeft) {
return _handleHomeWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleHome(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Ctrl+PgRt or Shift+PgRt // MARK: Ctrl+PgRt or Shift+PgRt
if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isRight) { if (input.isControlHold || input.isShiftHold) && (input.isOptionHold && input.isRight) {
return _handleEndWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: AbsorbedArrowKey // MARK: AbsorbedArrowKey
if input.isAbsorbedArrowKey || input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse { if input.isAbsorbedArrowKey || input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse {
return _handleAbsorbedArrowKeyWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleAbsorbedArrowKey(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Backspace // MARK: Backspace
if input.isBackSpace { if input.isBackSpace {
return _handleBackspaceWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleBackspace(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Delete // MARK: Delete
if input.isDelete || input.emacsKey == vChewingEmacsKey.delete { if input.isDelete || input.emacsKey == vChewingEmacsKey.delete {
return _handleDeleteWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) return handleDelete(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Enter // MARK: Enter
if input.isEnter { if input.isEnter {
return (input.isCommandHold && input.isControlHold) return (input.isCommandHold && input.isControlHold)
? _handleCtrlCommandEnterWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) ? handleCtrlCommandEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
: _handleEnterWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) : handleEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: - // MARK: -
@ -348,8 +348,10 @@ import Cocoa
let inputting = buildInputtingState() let inputting = buildInputtingState()
inputting.poppedText = poppedText inputting.poppedText = poppedText
stateCallback(inputting) stateCallback(inputting)
let choosingCandidate = let choosingCandidate = buildCandidate(
_buildCandidateState(inputting, useVerticalMode: input.useVerticalMode) state: inputting,
useVerticalMode: input.useVerticalMode
)
stateCallback(choosingCandidate) stateCallback(choosingCandidate)
} else { // If there is still unfinished bpmf reading, ignore the punctuation } else { // If there is still unfinished bpmf reading, ignore the punctuation
IME.prtDebugIntel("17446655") IME.prtDebugIntel("17446655")
@ -361,7 +363,7 @@ import Cocoa
// commit buffer ESC // commit buffer ESC
// Enter 使 commit buffer // Enter 使 commit buffer
// bool _ = // bool _ =
_ = _handleEnterWithState(state, stateCallback: stateCallback, errorCallback: errorCallback) _ = handleEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
let root: SymbolNode! = SymbolNode.root let root: SymbolNode! = SymbolNode.root
let symbolState = let symbolState =
InputState.SymbolTable(node: root, useVerticalMode: input.useVerticalMode) InputState.SymbolTable(node: root, useVerticalMode: input.useVerticalMode)
@ -391,7 +393,7 @@ import Cocoa
punctuationNamePrefix, parser, String(format: "%c", CChar(charCode)), punctuationNamePrefix, parser, String(format: "%c", CChar(charCode)),
] ]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "") let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
if _handlePunctuation( if handlePunctuation(
customPunctuation, customPunctuation,
state: state, state: state,
usingVerticalMode: input.useVerticalMode, usingVerticalMode: input.useVerticalMode,
@ -405,7 +407,7 @@ import Cocoa
let arrPunctuations: [String] = [punctuationNamePrefix, String(format: "%c", CChar(charCode))] let arrPunctuations: [String] = [punctuationNamePrefix, String(format: "%c", CChar(charCode))]
let punctuation: String = arrPunctuations.joined(separator: "") let punctuation: String = arrPunctuations.joined(separator: "")
if _handlePunctuation( if handlePunctuation(
punctuation, punctuation,
state: state, state: state,
usingVerticalMode: input.useVerticalMode, usingVerticalMode: input.useVerticalMode,
@ -418,7 +420,7 @@ import Cocoa
// 使 2.2 // 使 2.2
if input.isUpperCaseASCIILetterKey { if input.isUpperCaseASCIILetterKey {
let letter: String! = String(format: "%@%c", "_letter_", CChar(charCode)) let letter: String! = String(format: "%@%c", "_letter_", CChar(charCode))
if _handlePunctuation( if handlePunctuation(
letter, letter,
state: state, state: state,
usingVerticalMode: input.useVerticalMode, usingVerticalMode: input.useVerticalMode,

View File

@ -33,7 +33,7 @@ import Cocoa
mgrPrefs.mandarinParserName + "_" mgrPrefs.mandarinParserName + "_"
} }
func _actualCandidateCursorIndex() -> Int { func getActualCandidateCursorIndex() -> Int {
var cursorIndex = getBuilderCursorIndex() var cursorIndex = getBuilderCursorIndex()
// MS Phonetics IME style, phrase is *after* the cursor. // MS Phonetics IME style, phrase is *after* the cursor.
// (i.e. the cursor is always *before* the phrase.) // (i.e. the cursor is always *before* the phrase.)

View File

@ -65,8 +65,8 @@ import Cocoa
// MARK: - // MARK: -
func _buildCandidateState( func buildCandidate(
_ currentState: InputState.NotEmpty, state currentState: InputState.NotEmpty,
useVerticalMode: Bool useVerticalMode: Bool
) -> InputState.ChoosingCandidate { ) -> InputState.ChoosingCandidate {
let candidatesArray = getCandidatesArray() let candidatesArray = getCandidatesArray()
@ -100,7 +100,7 @@ import Cocoa
// MARK: - // MARK: -
func _handleMarkingState( func handleMarkingState(
_ state: InputState.Marking, _ state: InputState.Marking,
input: InputHandler, input: InputHandler,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
@ -186,7 +186,7 @@ import Cocoa
// MARK: - // MARK: -
func _handlePunctuation( func handlePunctuation(
_ customPunctuation: String, _ customPunctuation: String,
state: InputState, state: InputState,
usingVerticalMode useVerticalMode: Bool, usingVerticalMode useVerticalMode: Bool,
@ -205,8 +205,9 @@ import Cocoa
stateCallback(inputting) stateCallback(inputting)
if mgrPrefs.useSCPCTypingMode, isPhoneticReadingBufferEmpty() { if mgrPrefs.useSCPCTypingMode, isPhoneticReadingBufferEmpty() {
let candidateState = _buildCandidateState( let candidateState = buildCandidate(
inputting, useVerticalMode: useVerticalMode state: inputting,
useVerticalMode: useVerticalMode
) )
if candidateState.candidates.count == 1 { if candidateState.candidates.count == 1 {
clear() clear()
@ -235,8 +236,8 @@ import Cocoa
// MARK: - Enter // MARK: - Enter
@discardableResult func _handleEnterWithState( @discardableResult func handleEnter(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -260,8 +261,8 @@ import Cocoa
// MARK: - CMD+Enter // MARK: - CMD+Enter
func _handleCtrlCommandEnterWithState( func handleCtrlCommandEnter(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -286,8 +287,8 @@ import Cocoa
// MARK: - Backspace (macOS Delete) // MARK: - Backspace (macOS Delete)
func _handleBackspaceWithState( func handleBackspace(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -321,8 +322,8 @@ import Cocoa
// MARK: - PC Delete (macOS Fn+BackSpace) // MARK: - PC Delete (macOS Fn+BackSpace)
func _handleDeleteWithState( func handleDelete(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -358,8 +359,8 @@ import Cocoa
// MARK: - 90 // MARK: - 90
func _handleAbsorbedArrowKeyWithState( func handleAbsorbedArrowKey(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -376,8 +377,8 @@ import Cocoa
// MARK: - Home // MARK: - Home
func _handleHomeWithState( func handleHome(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -407,8 +408,8 @@ import Cocoa
// MARK: - End // MARK: - End
func _handleEndWithState( func handleEnd(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -438,8 +439,8 @@ import Cocoa
// MARK: - Esc // MARK: - Esc
func _handleEscWithState( func handleEsc(
_ state: InputState, state: InputState,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void errorCallback _: @escaping () -> Void
) -> Bool { ) -> Bool {
@ -473,8 +474,8 @@ import Cocoa
// MARK: - // MARK: -
func _handleForwardWithState( func handleForward(
_ state: InputState, state: InputState,
input: InputHandler, input: InputHandler,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
@ -525,8 +526,8 @@ import Cocoa
// MARK: - // MARK: -
func _handleBackwardWithState( func handleBackward(
_ state: InputState, state: InputState,
input: InputHandler, input: InputHandler,
stateCallback: @escaping (InputState) -> Void, stateCallback: @escaping (InputState) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void