Skip to content

Commit

Permalink
[WIP] predict next words, step2
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed May 2, 2024
1 parent 75a9edd commit d97997f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/InputController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
NSString *characters = event.characters;

NSString *bufferedText = [self originalBuffer];
NSString *bufferedSentence = [self sentenceBuffer];
bool hasBufferedText = bufferedText && bufferedText.length > 0;
bool hasSentence = bufferedSentence && bufferedSentence.length > 0;


if (keyCode == KEY_DELETE) {
if (hasBufferedText) {
Expand All @@ -96,15 +99,15 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if (keyCode == KEY_SPACE) {
if (hasBufferedText) {
if (hasBufferedText || hasSentence) {
[self commitComposition:sender];
return YES;
}
return NO;
}

if (keyCode == KEY_RETURN) {
if (hasBufferedText) {
if (hasBufferedText || hasSentence) {
[self commitCompositionWithoutSpace:sender];
return YES;
}
Expand Down Expand Up @@ -144,7 +147,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:ch]) {
if (!hasBufferedText) {
if (!(hasBufferedText || hasSentence)) {
[self appendToComposedBuffer:characters];
[self commitCompositionWithoutSpace:sender];
return YES;
Expand All @@ -170,7 +173,7 @@ - (BOOL)onKeyEvent:(NSEvent *)event client:(id)sender {
}

if ([[NSCharacterSet punctuationCharacterSet] characterIsMember:ch] || [[NSCharacterSet symbolCharacterSet] characterIsMember:ch]) {
if (hasBufferedText) {
if (hasBufferedText || hasSentence) {
[self appendToComposedBuffer:characters];
[self commitCompositionWithoutSpace:sender];
[self setSentenceBuffer: @""];
Expand All @@ -188,11 +191,14 @@ - (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];
Expand Down Expand Up @@ -449,6 +455,7 @@ - (void)activateServer:(id)sender {
}

- (void)deactivateServer:(id)sender {
[self setSentenceBuffer:@""];
[self reset];
}

Expand Down

0 comments on commit d97997f

Please sign in to comment.