Revert "Revert "Fix a race condition.""

This reverts commit 38401272446dbaec785e656b921cfbd31162ba09.

Change-Id: I7f0d975ec9e18d94fe3505250360c2b79ccbe96d
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index cc42d87..e0adacd 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -1114,8 +1114,11 @@
                 keyboardSwitcher.getKeyboard());
         mWordComposer.setCursorPositionWithinWord(
                 typedWord.codePointCount(0, numberOfCharsInWordBeforeCursor));
-        mConnection.setComposingRegion(expectedCursorPosition - numberOfCharsInWordBeforeCursor,
-                expectedCursorPosition + range.getNumberOfCharsInWordAfterCursor());
+        // TODO: Change these two lines to setComposingRegion(cursorPosition,
+        //         cursorPosition + range.getNumberOfCharsInWordAfterCursor());
+        mConnection.deleteSurroundingText(numberOfCharsInWordBeforeCursor,
+              typedWord.length() - numberOfCharsInWordBeforeCursor);
+        mConnection.setComposingText(typedWord, 1);
         if (suggestions.isEmpty()) {
             // We come here if there weren't any suggestion spans on this word. We will try to
             // compute suggestions for it instead.