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
- (void)fixNodeWithValue:(NSString *)value
{
NSInteger cursorIndex = [self _actualCandidateCursorIndex];
NSInteger cursorIndex = [self getActualCandidateCursorIndex];
std::string stringValue(value.UTF8String);
Gramambular::NodeAnchor selectedNode = _builder->grid().fixNodeSelectedCandidate(cursorIndex, stringValue);
if (!mgrPrefs.useSCPCTypingMode)
@ -564,7 +564,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
if (!overrideValue.empty())
{
NSInteger cursorIndex = [self _actualCandidateCursorIndex];
NSInteger cursorIndex = [self getActualCandidateCursorIndex];
std::vector<Gramambular::NodeAnchor> nodes = _builder->grid().nodesCrossingOrEndingAt(cursorIndex);
double highestScore = FindHighestScore(nodes, kEpsilon);
_builder->grid().overrideNodeScoreForSelectedCandidate(cursorIndex, overrideValue,
@ -601,7 +601,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
{
NSMutableArray<NSString *> *candidatesArray = [[NSMutableArray alloc] init];
NSInteger cursorIndex = [self _actualCandidateCursorIndex];
NSInteger cursorIndex = [self getActualCandidateCursorIndex];
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

View File

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

View File

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

View File

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

View File

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