Disables adding phrases with shift-arraw keys when the length of the composing buffer and readings do not match.

This commit is contained in:
zonble 2022-02-01 08:11:09 +08:00
parent 74509ce3cb
commit 347560e36b
5 changed files with 36 additions and 34 deletions

View File

@ -82,3 +82,5 @@
"Half-width punctuation off" = "Half-width punctuation off"; "Half-width punctuation off" = "Half-width punctuation off";
"Associated Phrases" = "Associated Phrases"; "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.";

View File

@ -106,16 +106,14 @@ class InputState: NSObject {
class NotEmpty: InputState { class NotEmpty: InputState {
@objc private(set) var composingBuffer: String @objc private(set) var composingBuffer: String
@objc private(set) var cursorIndex: UInt @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.composingBuffer = composingBuffer
self.cursorIndex = cursorIndex self.cursorIndex = cursorIndex
self.phrases = phrases
} }
override var description: String { override var description: String {
"<InputState.NotEmpty, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), phrases:\(phrases)>" "<InputState.NotEmpty, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>"
} }
} }
@ -124,8 +122,8 @@ class InputState: NSObject {
class Inputting: NotEmpty { class Inputting: NotEmpty {
@objc var poppedText: String = "" @objc var poppedText: String = ""
@objc override init(composingBuffer: String, cursorIndex: UInt, phrases: [InputPhrase]) { @objc override init(composingBuffer: String, cursorIndex: UInt) {
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
} }
@objc var attributedString: NSAttributedString { @objc var attributedString: NSAttributedString {
@ -137,7 +135,7 @@ class InputState: NSObject {
} }
override var description: String { override var description: String {
"<InputState.Inputting, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), phrases:\(phrases)>, poppedText:\(poppedText)>" "<InputState.Inputting, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>, poppedText:\(poppedText)>"
} }
} }
@ -152,6 +150,10 @@ class InputState: NSObject {
@objc private(set) var markedRange: NSRange @objc private(set) var markedRange: NSRange
@objc var tooltip: String { @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 { if Preferences.phraseReplacementEnabled {
return NSLocalizedString("Phrase replacement mode is on. Not suggested to add phrase in the mode.", comment: "") 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) 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 self.markerIndex = markerIndex
let begin = min(cursorIndex, markerIndex) let begin = min(cursorIndex, markerIndex)
let end = max(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex)
markedRange = NSMakeRange(Int(begin), Int(end - begin)) 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 { @objc var attributedString: NSAttributedString {
@ -200,34 +205,25 @@ class InputState: NSObject {
} }
override var description: String { override var description: String {
"<InputState.Marking, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), markedRange:\(markedRange), phrases:\(phrases)>" "<InputState.Marking, composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex), markedRange:\(markedRange)>"
} }
@objc func convertToInputting() -> Inputting { @objc func convertToInputting() -> Inputting {
let state = Inputting(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) let state = Inputting(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
return state return state
} }
@objc var validToWrite: Bool { @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 { @objc var userPhrase: String {
let text = (composingBuffer as NSString).substring(with: markedRange) let text = (composingBuffer as NSString).substring(with: markedRange)
let end = markedRange.location + markedRange.length let end = markedRange.location + markedRange.length
var selectedPhrases = [InputPhrase]() let readings = readings[markedRange.location..<end]
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 joined = readings.joined(separator: "-") let joined = readings.joined(separator: "-")
return "\(text) \(joined)" return "\(text) \(joined)"
} }
@ -239,10 +235,10 @@ class InputState: NSObject {
@objc private(set) var candidates: [String] @objc private(set) var candidates: [String]
@objc private(set) var useVerticalMode: Bool @objc private(set) var useVerticalMode: Bool
@objc init(composingBuffer: String, cursorIndex: UInt, candidates: [String], phrases: [InputPhrase], useVerticalMode: Bool) { @objc init(composingBuffer: String, cursorIndex: UInt, candidates: [String], useVerticalMode: Bool) {
self.candidates = candidates self.candidates = candidates
self.useVerticalMode = useVerticalMode self.useVerticalMode = useVerticalMode
super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex, phrases: phrases) super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex)
} }
@objc var attributedString: NSAttributedString { @objc var attributedString: NSAttributedString {
@ -254,7 +250,7 @@ class InputState: NSObject {
} }
override var description: String { override var description: String {
"<InputState.ChoosingCandidate, candidates:\(candidates), useVerticalMode:\(useVerticalMode), phrases:\(phrases), composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>" "<InputState.ChoosingCandidate, candidates:\(candidates), useVerticalMode:\(useVerticalMode), composingBuffer:\(composingBuffer), cursorIndex:\(cursorIndex)>"
} }
} }

View File

@ -579,7 +579,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + left // Shift + left
if (currentState.cursorIndex > 0) { if (currentState.cursorIndex > 0) {
NSInteger previousPosition = [StringUtils previousUtf16PositionForIndex:currentState.cursorIndex in:currentState.composingBuffer]; 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); stateCallback(marking);
} else { } else {
errorCallback(); errorCallback();
@ -616,7 +616,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + Right // Shift + Right
if (currentState.cursorIndex < currentState.composingBuffer.length) { if (currentState.cursorIndex < currentState.composingBuffer.length) {
NSInteger nextPosition = [StringUtils nextUtf16PositionForIndex:currentState.cursorIndex in:currentState.composingBuffer]; 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); stateCallback(marking);
} else { } else {
errorCallback(); errorCallback();
@ -843,7 +843,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index > 0) { if (index > 0) {
index = [StringUtils previousUtf16PositionForIndex:index in:state.composingBuffer]; 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); stateCallback(marking);
} else { } else {
errorCallback(); errorCallback();
@ -858,7 +858,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index < state.composingBuffer.length) { if (index < state.composingBuffer.length) {
index = [StringUtils nextUtf16PositionForIndex:index in:state.composingBuffer]; 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); stateCallback(marking);
} else { } else {
errorCallback(); errorCallback();
@ -1177,7 +1177,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSString *composedText = [head stringByAppendingString:[reading stringByAppendingString:tail]]; NSString *composedText = [head stringByAppendingString:[reading stringByAppendingString:tail]];
NSInteger cursorIndex = composedStringCursorIndex + [reading length]; 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; 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; return state;
} }

View File

@ -82,3 +82,5 @@
"Half-width punctuation off" = "Half-width punctuation off"; "Half-width punctuation off" = "Half-width punctuation off";
"Associated Phrases" = "Associated Phrases"; "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.";

View File

@ -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." = "您輸入了特殊符號,我們還無法支援在這種狀況下手動加詞。";