From 1927d1ca7400db371fd24aded6e09fdf39c15eb2 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 27 Aug 2022 17:20:24 +0800 Subject: [PATCH] KeyHandler // Fix an overkill error in consolidateCursorContext(). --- .../ControllerModules/KeyHandler_Core.swift | 41 +++++++++---------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 2ee93f28..8ac1887d 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -142,38 +142,37 @@ public class KeyHandler { /// /// 威注音輸入法截至 v1.9.3 SP2 版為止都受到上游的這個 Bug 的影響,且在 v1.9.4 版利用該函式修正了這個缺陷。 /// 該修正必須搭配至少天權星組字引擎 v2.0.2 版方可生效。算法可能比較囉唆,但至少在常用情形下不會再發生該問題。 - /// - Parameters: - /// - theCandidate: 要拿來覆寫的詞音配對。 - /// - cursorSpecified: 給定作業目標游標位置(不知道怎麼用的話就不要用),不設定的話則改用 actualCandidateCursor。 - func consolidateCursorContext(with theCandidate: Megrez.Compositor.Candidate = .init(), cursorSpecified: Int? = nil) { - var cursorSpecified = cursorSpecified ?? actualCandidateCursor - if cursorSpecified != actualCandidateCursor { - cursorSpecified = max(0, min(cursorSpecified, compositor.width - 1)) // 糾正傳入的數值。 - } - let grid = compositor - var frontBoundaryEX = compositor.width - 1 - var rearBoundaryEX = 0 - if grid.overrideCandidate(theCandidate, at: cursorSpecified) { - guard let node = compositor.walkedNodes.findNode(at: cursorSpecified, target: &frontBoundaryEX) else { - return - } - rearBoundaryEX = max(0, frontBoundaryEX - node.keyArray.count) + /// - Parameter theCandidate: 要拿來覆寫的詞音配對。 + func consolidateCursorContext(with theCandidate: Megrez.Compositor.Candidate) { + var grid = compositor + var frontBoundaryEX = actualCandidateCursor + 1 + var rearBoundaryEX = actualCandidateCursor + var debugIntelToPrint = "" + if grid.overrideCandidate(theCandidate, at: actualCandidateCursor) { + grid.walk() + let range = grid.walkedNodes.contextRange(ofGivenCursor: actualCandidateCursor) + rearBoundaryEX = range.lowerBound + frontBoundaryEX = range.upperBound + debugIntelToPrint.append("EX: \(rearBoundaryEX)..<\(frontBoundaryEX), ") } - var frontBoundary = 0 - guard let node = compositor.walkedNodes.findNode(at: cursorSpecified, target: &frontBoundary) else { return } + let range = compositor.walkedNodes.contextRange(ofGivenCursor: actualCandidateCursor) + var rearBoundary = min(range.lowerBound, rearBoundaryEX) + var frontBoundary = max(range.upperBound, frontBoundaryEX) - var rearBoundary = min(max(0, frontBoundary - node.keyArray.count), rearBoundaryEX) // 防呆 - frontBoundary = max(min(frontBoundary, compositor.width), frontBoundaryEX) // 防呆。 + debugIntelToPrint.append("INI: \(rearBoundary)..<\(frontBoundary), ") 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) + frontBoundary = min(max(compositor.cursor, frontBoundary), compositor.width) compositor.cursor = cursorBackup // 計算結束,游標歸位。 + debugIntelToPrint.append("FIN: \(rearBoundary)..<\(frontBoundary)") + IME.prtDebugIntel(debugIntelToPrint) + // 接下來獲取這個範圍內的媽的都不知道該怎麼講了。 var nodeIndices = [Int]() // 僅作統計用。