LMMgr // Simplify writeUserPhrase().
This commit is contained in:
parent
4e9faa70c5
commit
74ccd6d0b7
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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: - 藉由語彙編輯器開啟使用者檔案
|
||||
|
|
|
@ -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
|
||||
)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue