From 74ccd6d0b720dbc10883c02dd0fcaf2788a86b9e Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 17 Feb 2023 19:26:49 +0800 Subject: [PATCH] LMMgr // Simplify writeUserPhrase(). --- .../Shared/Protocols/IMEStateProtocol.swift | 1 - Source/Modules/IMEStateData.swift | 8 -- Source/Modules/LMMgr.swift | 96 ++++++++++--------- Source/Modules/SessionCtl_Delegates.swift | 4 - 4 files changed, 50 insertions(+), 59 deletions(-) diff --git a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift index ad02d9d0..e9055ed0 100644 --- a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift +++ b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift @@ -57,7 +57,6 @@ public protocol IMEStateDataProtocol { var userPhraseKVPair: (String, String) { get } var userPhraseDumped: String { get } var userPhraseDumpedConverted: String { get } - var doesUserPhraseExist: Bool { get } var tooltipColorState: TooltipColorState { get set } mutating func updateTooltipForMarking() } diff --git a/Source/Modules/IMEStateData.swift b/Source/Modules/IMEStateData.swift index 41048211..ea20fc2e 100644 --- a/Source/Modules/IMEStateData.swift +++ b/Source/Modules/IMEStateData.swift @@ -169,14 +169,6 @@ public struct IMEStateData: IMEStateDataProtocol { // MARK: - IMEState 工具函式 public extension IMEStateData { - var doesUserPhraseExist: Bool { - let text = displayedText.map(\.description)[markedRange].joined() - let joined = markedReadings.joined(separator: InputHandler.keySeparator) - return LMMgr.checkIfUserPhraseExist( - userPhrase: text, mode: IMEApp.currentInputMode, key: joined - ) - } - var readingThreadForDisplay: String { var arrOutput = [String]() for neta in markedReadings { diff --git a/Source/Modules/LMMgr.swift b/Source/Modules/LMMgr.swift index 786b3223..ed7dfcf4 100644 --- a/Source/Modules/LMMgr.swift +++ b/Source/Modules/LMMgr.swift @@ -634,54 +634,58 @@ public class LMMgr { // MARK: - 寫入使用者檔案 public static func writeUserPhrase( - _ userPhrase: String?, inputMode mode: Shared.InputMode, areWeDuplicating: Bool, areWeDeleting: Bool + _ userPhrase: String, inputMode mode: Shared.InputMode, areWeDeleting: Bool ) -> Bool { - if var currentMarkedPhrase: String = userPhrase { - if !chkUserLMFilesExist(.imeModeCHS) - || !chkUserLMFilesExist(.imeModeCHT) - { - return false - } - - let theType: vChewingLM.ReplacableUserDataType = areWeDeleting ? .theFilter : .thePhrases - let theURL = userDictDataURL(mode: mode, type: theType) - - if areWeDuplicating, !areWeDeleting { - // Do not use ASCII characters to comment here. - // Otherwise, it will be scrambled by cnvHYPYtoBPMF - // module shipped in the vChewing Phrase Editor. - currentMarkedPhrase += " #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎" - } - - if let writeFile = FileHandle(forUpdatingAtPath: theURL.path), - let data = currentMarkedPhrase.data(using: .utf8), - let endl = "\n".data(using: .utf8) - { - writeFile.seekToEndOfFile() - writeFile.write(endl) - writeFile.write(data) - writeFile.write(endl) - writeFile.closeFile() - } else { - return false - } - - // We enforce the format consolidation here, since the pragma header - // will let the UserPhraseLM bypasses the consolidating process on load. - if !vChewingLM.LMConsolidator.consolidate(path: theURL.path, pragma: false) { - return false - } - - // The new FolderMonitor module does NOT monitor cases that files are modified - // by the current application itself, requiring additional manual loading process here. - if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } - if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { - CtlPrefWindow.shared?.updatePhraseEditor() - } - loadUserPhrasesData(type: .thePhrases) - return true + var userPhraseOutput: String = userPhrase + if !chkUserLMFilesExist(.imeModeCHS) + || !chkUserLMFilesExist(.imeModeCHT) + { + return false } - return false + + let theType: vChewingLM.ReplacableUserDataType = areWeDeleting ? .theFilter : .thePhrases + let theURL = userDictDataURL(mode: mode, type: theType) + + let arr = userPhraseOutput.split(separator: " ") + var areWeDuplicating = false + if arr.count >= 2 { + areWeDuplicating = Self.checkIfUserPhraseExist( + userPhrase: arr[0].description, mode: mode, key: arr[1].description, factoryDictionaryOnly: true + ) + } + + if areWeDuplicating, !areWeDeleting { + // Do not use ASCII characters to comment here. + userPhraseOutput += " #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎" + } + + if let writeFile = FileHandle(forUpdatingAtPath: theURL.path), + let data = userPhraseOutput.data(using: .utf8), + let endl = "\n".data(using: .utf8) + { + writeFile.seekToEndOfFile() + writeFile.write(endl) + writeFile.write(data) + writeFile.write(endl) + writeFile.closeFile() + } else { + return false + } + + // We enforce the format consolidation here, since the pragma header + // will let the UserPhraseLM bypasses the consolidating process on load. + if !vChewingLM.LMConsolidator.consolidate(path: theURL.path, pragma: false) { + return false + } + + // The new FolderMonitor module does NOT monitor cases that files are modified + // by the current application itself, requiring additional manual loading process here. + if #available(macOS 10.15, *) { FileObserveProject.shared.touch() } + if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { + CtlPrefWindow.shared?.updatePhraseEditor() + } + loadUserPhrasesData(type: .thePhrases) + return true } // MARK: - 藉由語彙編輯器開啟使用者檔案 diff --git a/Source/Modules/SessionCtl_Delegates.swift b/Source/Modules/SessionCtl_Delegates.swift index 59e60bfc..155e698b 100644 --- a/Source/Modules/SessionCtl_Delegates.swift +++ b/Source/Modules/SessionCtl_Delegates.swift @@ -35,12 +35,10 @@ extension SessionCtl: InputHandlerDelegate { guard let inputHandler = inputHandler, state.type == .ofMarking else { return false } if !LMMgr.writeUserPhrase( state.data.userPhraseDumped, inputMode: inputMode, - areWeDuplicating: state.data.doesUserPhraseExist, areWeDeleting: addToFilter ) || !LMMgr.writeUserPhrase( state.data.userPhraseDumpedConverted, inputMode: inputMode.reversed, - areWeDuplicating: false, areWeDeleting: addToFilter ) { @@ -194,12 +192,10 @@ extension SessionCtl: CtlCandidateDelegate { if !LMMgr.writeUserPhrase( userPhraseDumped, inputMode: inputMode, - areWeDuplicating: action != .toFilter, areWeDeleting: action == .toFilter ) || !LMMgr.writeUserPhrase( userPhraseDumpedConverted, inputMode: inputMode.reversed, - areWeDuplicating: action != .toFilter, areWeDeleting: action == .toFilter ) {