KeyHandler // Consolidate the cursor context prior to fixing nodes.

- see: https://github.com/vChewing/vChewing-macOS/issues/100
This commit is contained in:
ShikiSuen 2022-08-16 15:05:30 +08:00
parent e20f95c4b5
commit 65eea6758f
3 changed files with 86 additions and 6 deletions

View File

@ -127,15 +127,95 @@ public class KeyHandler {
return arrResult return arrResult
} }
///
///
/// 使
/// **macOS **
/// OV Bug
///
///
/// - Remark:
///
///
///
/// v1.9.3 SP2 Bug v1.9.4
/// v2.0.2
/// - Parameter theCandidate:
func consolidateCursorContext(with theCandidate: Megrez.Compositor.Candidate) {
let grid = compositor
var frontBoundaryEX = compositor.width - 1
var rearBoundaryEX = 0
if grid.overrideCandidate(theCandidate, at: actualCandidateCursor) {
guard let node = compositor.walkedNodes.findNode(at: actualCandidateCursor, target: &frontBoundaryEX) else {
return
}
rearBoundaryEX = max(0, frontBoundaryEX - node.keyArray.count)
}
var frontBoundary = 0
guard let node = compositor.walkedNodes.findNode(at: actualCandidateCursor, target: &frontBoundary) else { return }
var rearBoundary = min(max(0, frontBoundary - node.keyArray.count), rearBoundaryEX) //
frontBoundary = max(min(frontBoundary, compositor.width), frontBoundaryEX) //
let cursorBackup = compositor.cursor
while compositor.cursor > rearBoundary { compositor.jumpCursorBySpan(to: .rear) }
rearBoundary = min(compositor.cursor, rearBoundary)
compositor.cursor = cursorBackup //
while compositor.cursor < frontBoundary { compositor.jumpCursorBySpan(to: .front) }
frontBoundary = max(compositor.cursor, frontBoundary)
compositor.cursor = cursorBackup //
//
var nodeIndices = [Int]() //
var position = rearBoundary //
while position < frontBoundary {
guard let regionIndex = compositor.cursorRegionMap[position] else {
position += 1
continue
}
if !nodeIndices.contains(regionIndex) {
nodeIndices.append(regionIndex) //
guard compositor.walkedNodes.count > regionIndex else { break } //
let currentNode = compositor.walkedNodes[regionIndex]
guard currentNode.keyArray.count == currentNode.value.count else {
compositor.overrideCandidate(currentNode.currentPair, at: position)
position += currentNode.keyArray.count
continue
}
let values = currentNode.currentPair.value.map { String($0) }
for (subPosition, key) in currentNode.keyArray.enumerated() {
guard values.count > subPosition else { break } //
let thePair = Megrez.Compositor.Candidate(
key: key, value: values[subPosition]
)
compositor.overrideCandidate(thePair, at: position)
position += 1
}
continue
}
position += 1
}
}
/// ///
/// ///
/// - Parameters: /// - Parameters:
/// - value: /// - value:
/// - respectCursorPushing: true /// - respectCursorPushing: true
func fixNode(candidate: (String, String), respectCursorPushing: Bool = true) { /// - consolidate:
let actualCursor = actualCandidateCursor func fixNode(candidate: (String, String), respectCursorPushing: Bool = true, preConsolidate: Bool = false) {
let theCandidate: Megrez.Compositor.Candidate = .init(key: candidate.0, value: candidate.1) let theCandidate: Megrez.Compositor.Candidate = .init(key: candidate.0, value: candidate.1)
if !compositor.overrideCandidate(theCandidate, at: actualCursor, overrideType: .withHighScore) { return }
///
if preConsolidate {
consolidateCursorContext(with: theCandidate)
}
//
if !compositor.overrideCandidate(theCandidate, at: actualCandidateCursor) { return }
let previousWalk = compositor.walkedNodes let previousWalk = compositor.walkedNodes
// //
walk() walk()

View File

@ -854,7 +854,7 @@ extension KeyHandler {
currentIndex = 0 currentIndex = 0
} }
fixNode(candidate: candidates[currentIndex], respectCursorPushing: false) fixNode(candidate: candidates[currentIndex], respectCursorPushing: false, preConsolidate: false)
stateCallback(buildInputtingState) stateCallback(buildInputtingState)
return true return true

View File

@ -118,7 +118,7 @@ extension ctlInputMethod: ctlCandidateDelegate {
if let state = state as? InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
let selectedValue = state.candidates[index] let selectedValue = state.candidates[index]
keyHandler.fixNode(candidate: selectedValue, respectCursorPushing: true) keyHandler.fixNode(candidate: selectedValue, respectCursorPushing: true, preConsolidate: true)
let inputting = keyHandler.buildInputtingState let inputting = keyHandler.buildInputtingState