From c73c26790fa9dcd836a918774d6efa39a05c0152 Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Tue, 13 Dec 2011 23:12:22 +0900
Subject: [PATCH] Make the word composer aware of commits.

Change-Id: I04e691fbc9227d4df195429bca89edea93575347
---
 .../android/inputmethod/latin/LatinIME.java   |  8 ++++--
 .../inputmethod/latin/WordComposer.java       | 27 ++++++++++++++++---
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 20c87ad13a..20e49fbca3 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1148,6 +1148,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         if (!mHasUncommittedTypedChars) return;
         mHasUncommittedTypedChars = false;
         final CharSequence typedWord = mWordComposer.getTypedWord();
+        mWordComposer.onCommitWord();
         if (typedWord.length() > 0) {
             if (ic != null) {
                 ic.commitText(typedWord, 1);
@@ -2032,6 +2033,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
             }
         }
         mHasUncommittedTypedChars = false;
+        mWordComposer.onCommitWord();
     }
 
     private static final WordComposer sEmptyWordComposer = new WordComposer();
@@ -2201,10 +2203,11 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
             }
         }
         ic.deleteSurroundingText(cancelLength + 1, 0);
-
-        // Re-insert the separator
+        mWordComposer.resumeSuggestionOnKeptWord();
         ic.commitText(mWordComposer.getTypedWord(), 1);
+        // Re-insert the separator
         ic.commitText(separator, 1);
+        mWordComposer.onCommitWord();
         Utils.Stats.onSeparator(separator.charAt(0), WordComposer.NOT_A_COORDINATE,
                 WordComposer.NOT_A_COORDINATE);
         mHandler.cancelUpdateBigramPredictions();
@@ -2233,6 +2236,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
         // restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving
         // the old WordComposer allows to reuse the actual typed coordinates.
         mHasUncommittedTypedChars = true;
+        mWordComposer.resumeSuggestionOnKeptWord();
         ic.setComposingText(mWordComposer.getTypedWord(), 1);
         mHandler.cancelUpdateBigramPredictions();
         mHandler.postUpdateSuggestions();
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index 60a9685bc1..c0204c2a62 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -61,11 +61,10 @@ public class WordComposer {
         }
     }
 
-    // The currently typing word.
-    // NOTE: this is not reset as soon as the word is committed because it may be needed again
-    // to resume suggestion if backspaced. TODO: separate cleanly what is actually being
-    // composed and what is kept for possible resuming.
+    // The currently typing word. May not be null.
     private CharacterStore mCurrentWord;
+    // The information being kept for resuming suggestion. May be null if wiped.
+    private CharacterStore mWordKeptForSuggestionResuming;
     // An auto-correction for this word out of the dictionary.
     private CharSequence mAutoCorrection;
 
@@ -82,6 +81,7 @@ public class WordComposer {
 
     public WordComposer() {
         mCurrentWord = new CharacterStore();
+        mWordKeptForSuggestionResuming = null;
         mTrailingSingleQuotesCount = 0;
         mAutoCorrection = null;
     }
@@ -92,6 +92,7 @@ public class WordComposer {
 
     public void init(WordComposer source) {
         mCurrentWord = new CharacterStore(source.mCurrentWord);
+        mWordKeptForSuggestionResuming = source.mWordKeptForSuggestionResuming;
         mCapsCount = source.mCapsCount;
         mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
         mAutoCapitalized = source.mAutoCapitalized;
@@ -104,6 +105,7 @@ public class WordComposer {
      */
     public void reset() {
         mCurrentWord.reset();
+        mWordKeptForSuggestionResuming = null;
         mCapsCount = 0;
         mIsFirstCharCapitalized = false;
         mTrailingSingleQuotesCount = 0;
@@ -323,4 +325,21 @@ public class WordComposer {
     public CharSequence getAutoCorrectionOrNull() {
         return mAutoCorrection;
     }
+
+    // TODO: pass the information about what was committed and how. Was it an auto-correction?
+    // Was it a completion? Was is what the user typed?
+    public void onCommitWord() {
+        mWordKeptForSuggestionResuming = mCurrentWord;
+        // TODO: improve performance by swapping buffers instead of creating a new object.
+        mCurrentWord = new CharacterStore();
+    }
+
+    public boolean hasWordKeptForSuggestionResuming() {
+        return null != mWordKeptForSuggestionResuming;
+    }
+
+    public void resumeSuggestionOnKeptWord() {
+        mCurrentWord = mWordKeptForSuggestionResuming;
+        mWordKeptForSuggestionResuming = null;
+    }
 }
-- 
GitLab