Straighten out logic for revert word paths

This also includes a new memorized string with simpler
logic that will allow to remove other members

Change-Id: I0b97243084902e68fcb835b6163c86fef8190ebc
This commit is contained in:
Jean Chalard 2011-12-12 15:09:17 +09:00
parent 86344836ee
commit 890d13189d

View file

@ -209,6 +209,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private int mCorrectionMode; private int mCorrectionMode;
private int mCommittedLength; private int mCommittedLength;
private String mWordSavedForAutoCorrectCancellation;
// Keep track of the last selection range to decide if we need to show word alternatives // Keep track of the last selection range to decide if we need to show word alternatives
private int mLastSelectionStart; private int mLastSelectionStart;
private int mLastSelectionEnd; private int mLastSelectionEnd;
@ -1431,11 +1432,15 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// TODO: Merge space state with TextEntryState // TODO: Merge space state with TextEntryState
TextEntryState.backspace(); TextEntryState.backspace();
if (TextEntryState.isUndoCommit()) { if (null != mWordSavedForAutoCorrectCancellation) {
revertLastWord(ic); cancelAutoCorrect(ic);
mWordSavedForAutoCorrectCancellation = null;
ic.endBatchEdit(); ic.endBatchEdit();
return; return;
} else {
mWordSavedForAutoCorrectCancellation = null;
} }
if (SPACE_STATE_DOUBLE == spaceState) { if (SPACE_STATE_DOUBLE == spaceState) {
if (revertDoubleSpace(ic)) { if (revertDoubleSpace(ic)) {
ic.endBatchEdit(); ic.endBatchEdit();
@ -1452,6 +1457,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) { if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) {
// Cancel multi-character input: remove the text we just entered.
// This is triggered on backspace after a key that inputs multiple characters,
// like the smiley key or the .com key.
ic.deleteSurroundingText(mEnteredText.length(), 0); ic.deleteSurroundingText(mEnteredText.length(), 0);
} else if (deleteChar) { } else if (deleteChar) {
if (mSuggestionsView != null && mSuggestionsView.dismissAddToDictionaryHint()) { if (mSuggestionsView != null && mSuggestionsView.dismissAddToDictionaryHint()) {
@ -1462,7 +1470,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
// different behavior only in the case of picking the first // different behavior only in the case of picking the first
// suggestion (typed word). It's intentional to have made this // suggestion (typed word). It's intentional to have made this
// inconsistent with backspacing after selecting other suggestions. // inconsistent with backspacing after selecting other suggestions.
revertLastWord(ic); restartSuggestionsOnManuallyPickedTypedWord(ic);
} else { } else {
ic.deleteSurroundingText(1, 0); ic.deleteSurroundingText(1, 0);
if (mDeleteCount > DELETE_ACCELERATE_AT) { if (mDeleteCount > DELETE_ACCELERATE_AT) {
@ -1601,6 +1609,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} else { } else {
commitTyped(ic); commitTyped(ic);
} }
} else {
mWordSavedForAutoCorrectCancellation = null;
} }
final boolean swapMagicSpace; final boolean swapMagicSpace;
@ -1863,6 +1873,9 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode); TextEntryState.acceptedDefault(mWordComposer.getTypedWord(), mBestWord, separatorCode);
mExpectingUpdateSelection = true; mExpectingUpdateSelection = true;
commitBestWord(mBestWord); commitBestWord(mBestWord);
if (!mBestWord.equals(mWordComposer.getTypedWord())) {
mWordSavedForAutoCorrectCancellation = mBestWord.toString();
}
// Add the word to the user unigram dictionary if it's not a known word // Add the word to the user unigram dictionary if it's not a known word
addToUserUnigramAndBigramDictionaries(mBestWord, addToUserUnigramAndBigramDictionaries(mBestWord,
UserUnigramDictionary.FREQUENCY_FOR_TYPED); UserUnigramDictionary.FREQUENCY_FOR_TYPED);
@ -2160,37 +2173,61 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
} }
// "ic" must not be null // "ic" must not be null
private void revertLastWord(final InputConnection ic) { private void cancelAutoCorrect(final InputConnection ic) {
if (mHasUncommittedTypedChars || mWordComposer.size() <= 0) { final int cancelLength = mWordSavedForAutoCorrectCancellation.length();
sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
return;
}
final CharSequence separator = ic.getTextBeforeCursor(1, 0); final CharSequence separator = ic.getTextBeforeCursor(1, 0);
ic.deleteSurroundingText(1, 0); if (DEBUG) {
final CharSequence textToTheLeft = ic.getTextBeforeCursor(mCommittedLength, 0); final String wordBeforeCursor =
ic.deleteSurroundingText(mCommittedLength, 0); ic.getTextBeforeCursor(cancelLength + 1, 0).subSequence(0, cancelLength)
.toString();
// Re-insert "separator" only when the deleted character was word separator and the if (!mWordSavedForAutoCorrectCancellation.equals(wordBeforeCursor)) {
// composing text wasn't equal to the auto-corrected text which can be found before throw new RuntimeException("cancelAutoCorrect check failed: we thought we were "
// the cursor. + "reverting \"" + mWordSavedForAutoCorrectCancellation
if (!TextUtils.isEmpty(separator) + "\", but before the cursor we found \"" + wordBeforeCursor + "\"");
&& mSettingsValues.isWordSeparator(separator.charAt(0)) }
&& !TextUtils.equals(mWordComposer.getTypedWord(), textToTheLeft)) { if (mWordComposer.getTypedWord().equals(wordBeforeCursor)) {
ic.commitText(mWordComposer.getTypedWord(), 1); throw new RuntimeException("cancelAutoCorrect check failed: we wanted to cancel "
TextEntryState.acceptedTyped(mWordComposer.getTypedWord()); + "auto correction and revert to \"" + mWordComposer.getTypedWord()
ic.commitText(separator, 1); + "\" but we found this very string before the cursor");
TextEntryState.typedCharacter(separator.charAt(0), true, }
WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
} else {
// Note: this relies on the last word still being held in the WordComposer
// Note: in the interest of code simplicity, we may want to just call
// restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving
// the old WordComposer allows to reuse the actual typed coordinates.
mHasUncommittedTypedChars = true;
ic.setComposingText(mWordComposer.getTypedWord(), 1);
TextEntryState.backspace();
} }
ic.deleteSurroundingText(cancelLength + 1, 0);
// Re-insert the separator
ic.commitText(mWordComposer.getTypedWord(), 1);
TextEntryState.acceptedTyped(mWordComposer.getTypedWord());
ic.commitText(separator, 1);
TextEntryState.typedCharacter(separator.charAt(0), true,
WordComposer.NOT_A_COORDINATE, WordComposer.NOT_A_COORDINATE);
mHandler.cancelUpdateBigramPredictions();
mHandler.postUpdateSuggestions();
}
// "ic" must not be null
private void restartSuggestionsOnManuallyPickedTypedWord(final InputConnection ic) {
final CharSequence separator = ic.getTextBeforeCursor(1, 0);
final int restartLength = mCommittedLength;
if (DEBUG) {
final String wordBeforeCursor =
ic.getTextBeforeCursor(restartLength + 1, 0).subSequence(0, restartLength)
.toString();
if (!mWordComposer.getTypedWord().equals(wordBeforeCursor)) {
throw new RuntimeException("restartSuggestionsOnManuallyPickedTypedWord "
+ "check failed: we thought we were reverting \""
+ mWordComposer.getTypedWord()
+ "\", but before the cursor we found \""
+ wordBeforeCursor + "\"");
}
}
ic.deleteSurroundingText(restartLength + 1, 0);
// Note: this relies on the last word still being held in the WordComposer
// Note: in the interest of code simplicity, we may want to just call
// restartSuggestionsOnWordBeforeCursorIfAtEndOfWord instead, but retrieving
// the old WordComposer allows to reuse the actual typed coordinates.
mHasUncommittedTypedChars = true;
ic.setComposingText(mWordComposer.getTypedWord(), 1);
TextEntryState.backspace();
mHandler.cancelUpdateBigramPredictions(); mHandler.cancelUpdateBigramPredictions();
mHandler.postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} }