diff --git a/package/PackageInfo b/package/PackageInfo index 8bbb238..8c7df33 100644 --- a/package/PackageInfo +++ b/package/PackageInfo @@ -1,2 +1,2 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/InputController.mm b/src/InputController.mm index 67b15c8..2baa2f0 100644 --- a/src/InputController.mm +++ b/src/InputController.mm @@ -88,7 +88,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender { NSString *bufferedSentence = [self sentenceBuffer]; bool hasBufferedText = bufferedText && bufferedText.length > 0; bool hasSentence = bufferedSentence && bufferedSentence.length > 0; - + BOOL isCandidatesVisible = [sharedCandidates isVisible]; if (keyCode == KEY_DELETE) { if (hasBufferedText) { @@ -99,15 +99,25 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender { } if (keyCode == KEY_SPACE) { - if (hasBufferedText || hasSentence) { + if (hasBufferedText) { + [self commitComposition:sender]; + return YES; + } + + if (hasSentence && isCandidatesVisible) { [self commitComposition:sender]; return YES; } + return NO; } if (keyCode == KEY_RETURN) { - if (hasBufferedText || hasSentence) { + if (hasBufferedText) { + [self commitCompositionWithoutSpace:sender]; + return YES; + } + if (hasSentence && isCandidatesVisible) { [self commitCompositionWithoutSpace:sender]; return YES; } @@ -117,6 +127,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender { if (keyCode == KEY_ESC) { [self cancelComposition]; [sender insertText:@""]; + [self setSentenceBuffer:@""]; [self reset]; return YES; } @@ -131,7 +142,6 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender { } if ([self isMojaveAndLaterSystem]) { - BOOL isCandidatesVisible = [sharedCandidates isVisible]; if (isCandidatesVisible) { if (keyCode == KEY_ARROW_DOWN) { [sharedCandidates moveDown:self]; @@ -147,9 +157,10 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender { } if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) { - if (!(hasBufferedText || hasSentence)) { + if (!hasBufferedText || !hasSentence) { [self appendToComposedBuffer:characters]; [self commitCompositionWithoutSpace:sender]; + [self setSentenceBuffer:@""]; return YES; } @@ -191,15 +202,11 @@ - (BOOL)isMojaveAndLaterSystem { - (BOOL)deleteBackward:(id)sender { NSMutableString *originalText = [self originalBuffer]; - NSMutableString *sensence = [self sentenceBuffer]; if (_insertionIndex > 0) { --_insertionIndex; NSString *convertedString = [originalText substringToIndex:originalText.length - 1]; - NSString *convertedSentence = [sensence substringToIndex:sensence.length - 1]; - [self setSentenceBuffer: convertedSentence]; - [self setComposedBuffer:convertedString]; [self setOriginalBuffer:convertedString]; @@ -216,12 +223,32 @@ - (BOOL)deleteBackward:(id)sender { return NO; } +- (BOOL)lastCharIsSymbol:(NSString *)text { + if (text.length > 0) { + unichar lastChar = [text characterAtIndex:text.length - 1]; + return [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:lastChar] || + [[NSCharacterSet symbolCharacterSet] characterIsMember:lastChar] || + [[NSCharacterSet punctuationCharacterSet] characterIsMember:lastChar]; + } + return NO; +} + - (void)commitComposition:(id)sender { NSString *text = [self composedBuffer]; if (text == nil || text.length == 0) { text = [self originalBuffer]; } + BOOL isCandidatesVisible = [sharedCandidates isVisible]; + if ((text == nil || text.length == 0) && isCandidatesVisible && _candidates.count > 0) { + NSInteger selectedCandidateIndex = [sharedCandidates selectedCandidate]; + if (selectedCandidateIndex != NSNotFound) { + text = [_candidates objectAtIndex: selectedCandidateIndex]; + } + } + + [self appendSentenceBuffer: text]; + BOOL commitWordWithSpace = [preference boolForKey:@"commitWordWithSpace"]; if (commitWordWithSpace && text.length > 0) { @@ -236,8 +263,37 @@ - (void)commitComposition:(id)sender { [self reset]; + [self getPredictions]; + +} + +- (void)commitCompositionWithoutSpace:(id)sender { + NSString *text = [self composedBuffer]; + + if (text == nil || text.length == 0) { + text = [self originalBuffer]; + } + + BOOL isCandidatesVisible = [sharedCandidates isVisible]; + if ((text == nil || text.length == 0) && isCandidatesVisible && _candidates.count > 0) { + NSInteger selectedCandidateIndex = [sharedCandidates selectedCandidate]; + if (selectedCandidateIndex != NSNotFound) { + text = [_candidates objectAtIndex: selectedCandidateIndex]; + } + } + + [self appendSentenceBuffer: text]; + + [sender insertText:text replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; + + [self reset]; + + [self getPredictions]; +} + +- (void) getPredictions { NSLog(@"Current Sentence Buffer: %@", self.sentenceBuffer); - if ([self doesSentenceBufferIncludeSpace]) { + if (self.sentenceBuffer && self.sentenceBuffer.length > 0 && ![self lastCharIsSymbol: self.sentenceBuffer]) { [self fetchPredictionsForText:self.sentenceBuffer completion:^(NSDictionary *responseDict, NSArray *bertArray, NSError *error) { if (error) { NSLog(@"Error: %@", error.localizedDescription); @@ -250,24 +306,7 @@ - (void)commitComposition:(id)sender { } }]; } - -} -- (BOOL)doesSentenceBufferIncludeSpace { - NSRange range = [self.sentenceBuffer rangeOfString:@" "]; - return range.location != NSNotFound; -} - -- (void)commitCompositionWithoutSpace:(id)sender { - NSString *text = [self composedBuffer]; - - if (text == nil || text.length == 0) { - text = [self originalBuffer]; - } - - [sender insertText:text replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; - - [self reset]; } - (NSString *) fetchAPIURL { @@ -352,10 +391,6 @@ - (NSMutableString *)composedBuffer { - (void)setComposedBuffer:(NSString *)string { NSMutableString *buffer = [self composedBuffer]; - if (string && string.length > 0) { - NSString * sentence = self.sentenceBuffer; - [self setSentenceBuffer: [NSString stringWithFormat:@"%@ %@", sentence, string]]; - } [buffer setString:string]; } @@ -366,6 +401,13 @@ - (NSMutableString *)sentenceBuffer { return _sentenceBuffer; } +- (void)appendSentenceBuffer:(NSString *)input { + NSMutableString *buffer = [self sentenceBuffer]; + [buffer appendString: @" "]; + [buffer appendString:input]; + [buffer appendString: @" "]; +} + - (void)setSentenceBuffer:(NSString *)string { NSMutableString *buffer = [self sentenceBuffer]; [buffer setString:string];