diff --git a/Source/Base.lproj/Localizable.strings b/Source/Base.lproj/Localizable.strings index 4995702d..d9bc1ea9 100644 --- a/Source/Base.lproj/Localizable.strings +++ b/Source/Base.lproj/Localizable.strings @@ -82,3 +82,5 @@ "Half-width punctuation off" = "Half-width punctuation off"; "Associated Phrases" = "Associated Phrases"; + +"There are special phrases in your text. We don't support adding new phrases in this case." = "There are special phrases in your text. We don't support adding new phrases in this case."; diff --git a/Source/InputState.swift b/Source/InputState.swift index fdbe1ebb..869559a1 100644 --- a/Source/InputState.swift +++ b/Source/InputState.swift @@ -106,16 +106,14 @@ class InputState: NSObject { class NotEmpty: InputState { @objc private(set) var composingBuffer: String @objc private(set) var cursorIndex: UInt - @objc private(set) var phrases: [InputPhrase] - @objc init(composingBuffer: String, cursorIndex: UInt, phrases: [InputPhrase]) { + @objc init(composingBuffer: String, cursorIndex: UInt) { self.composingBuffer = composingBuffer self.cursorIndex = cursorIndex - self.phrases = phrases } override var description: String { - "" + "" } } @@ -124,8 +122,8 @@ class InputState: NSObject { class Inputting: NotEmpty { @objc var poppedText: String = "" - @objc override init(composingBuffer: String, cursorIndex: UInt, phrases: [InputPhrase]) { - super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) + @objc override init(composingBuffer: String, cursorIndex: UInt) { + super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) } @objc var attributedString: NSAttributedString { @@ -137,7 +135,7 @@ class InputState: NSObject { } override var description: String { - ", poppedText:\(poppedText)>" + ", poppedText:\(poppedText)>" } } @@ -152,6 +150,10 @@ class InputState: NSObject { @objc private(set) var markedRange: NSRange @objc var tooltip: String { + if composingBuffer.count != readings.count { + return NSLocalizedString("There are special phrases in your text. We don't support adding new phrases in this case.", comment: "") + } + if Preferences.phraseReplacementEnabled { return NSLocalizedString("Phrase replacement mode is on. Not suggested to add phrase in the mode.", comment: "") } @@ -171,12 +173,15 @@ class InputState: NSObject { return String(format: NSLocalizedString("You are now selecting \"%@\". Press enter to add a new phrase.", comment: ""), text) } - @objc init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt, phrases: [InputPhrase]) { + @objc private(set) var readings: [String] + + @objc init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt, readings: [String]) { self.markerIndex = markerIndex let begin = min(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex) markedRange = NSMakeRange(Int(begin), Int(end - begin)) - super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) + self.readings = readings + super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) } @objc var attributedString: NSAttributedString { @@ -200,34 +205,25 @@ class InputState: NSObject { } override var description: String { - "" + "" } @objc func convertToInputting() -> Inputting { - let state = Inputting(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) + let state = Inputting(composingBuffer: composingBuffer, cursorIndex: cursorIndex) return state } @objc var validToWrite: Bool { - markedRange.length >= kMinMarkRangeLength && markedRange.length <= kMaxMarkRangeLength + if composingBuffer.count != readings.count { + return false + } + return markedRange.length >= kMinMarkRangeLength && markedRange.length <= kMaxMarkRangeLength } @objc var userPhrase: String { let text = (composingBuffer as NSString).substring(with: markedRange) let end = markedRange.location + markedRange.length - var selectedPhrases = [InputPhrase]() - var length = 0 - for component in self.phrases { - if length >= end { - break - } - if length >= markedRange.location { - selectedPhrases.append(component) - } - length += (component.text as NSString).length - } - - let readings = selectedPhrases.map { $0.reading } + let readings = readings[markedRange.location.." + "" } } diff --git a/Source/KeyHandler.mm b/Source/KeyHandler.mm index 35a7838b..cc1631b0 100644 --- a/Source/KeyHandler.mm +++ b/Source/KeyHandler.mm @@ -579,7 +579,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot // Shift + left if (currentState.cursorIndex > 0) { NSInteger previousPosition = [StringUtils previousUtf16PositionForIndex:currentState.cursorIndex in:currentState.composingBuffer]; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:previousPosition phrases:currentState.phrases]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:previousPosition readings:[self _currentReadings]]; stateCallback(marking); } else { errorCallback(); @@ -616,7 +616,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot // Shift + Right if (currentState.cursorIndex < currentState.composingBuffer.length) { NSInteger nextPosition = [StringUtils nextUtf16PositionForIndex:currentState.cursorIndex in:currentState.composingBuffer]; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:nextPosition phrases:currentState.phrases]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex markerIndex:nextPosition readings:[self _currentReadings]]; stateCallback(marking); } else { errorCallback(); @@ -843,7 +843,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot NSUInteger index = state.markerIndex; if (index > 0) { index = [StringUtils previousUtf16PositionForIndex:index in:state.composingBuffer]; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index phrases:state.phrases]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index readings:state.readings]; stateCallback(marking); } else { errorCallback(); @@ -858,7 +858,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot NSUInteger index = state.markerIndex; if (index < state.composingBuffer.length) { index = [StringUtils nextUtf16PositionForIndex:index in:state.composingBuffer]; - InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index phrases:state.phrases]; + InputStateMarking *marking = [[InputStateMarking alloc] initWithComposingBuffer:state.composingBuffer cursorIndex:state.cursorIndex markerIndex:index readings:state.readings]; stateCallback(marking); } else { errorCallback(); @@ -1177,7 +1177,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot NSString *composedText = [head stringByAppendingString:[reading stringByAppendingString:tail]]; NSInteger cursorIndex = composedStringCursorIndex + [reading length]; - InputStateInputting *newState = [[InputStateInputting alloc] initWithComposingBuffer:composedText cursorIndex:cursorIndex phrases:phrases]; + InputStateInputting *newState = [[InputStateInputting alloc] initWithComposingBuffer:composedText cursorIndex:cursorIndex]; return newState; } @@ -1247,7 +1247,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot } } - InputStateChoosingCandidate *state = [[InputStateChoosingCandidate alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex candidates:candidatesArray phrases:currentState.phrases useVerticalMode:useVerticalMode]; + InputStateChoosingCandidate *state = [[InputStateChoosingCandidate alloc] initWithComposingBuffer:currentState.composingBuffer cursorIndex:currentState.cursorIndex candidates:candidatesArray useVerticalMode:useVerticalMode]; return state; } diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index 4995702d..d9bc1ea9 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -82,3 +82,5 @@ "Half-width punctuation off" = "Half-width punctuation off"; "Associated Phrases" = "Associated Phrases"; + +"There are special phrases in your text. We don't support adding new phrases in this case." = "There are special phrases in your text. We don't support adding new phrases in this case."; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index 69964d8a..68278358 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -82,3 +82,5 @@ "Half-width punctuation off" = "已經切回到全型標點模式"; "Associated Phrases" = "聯想詞"; + +"There are special phrases in your text. We don't support adding new phrases in this case." = "您輸入了特殊符號,我們還無法支援在這種狀況下手動加詞。";