am a3f7a584: Don\'t commit a space before it\'s actually needed

* commit 'a3f7a5840aa7ac2a3cac02634dc74f288ca2222d':
  Don't commit a space before it's actually needed
This commit is contained in:
Jean Chalard 2014-06-25 01:51:37 +00:00 committed by Android Git Automerger
commit 4038f1d376

View file

@ -728,13 +728,13 @@ public final class InputLogic {
mConnection.setComposingText(getTextWithUnderline( mConnection.setComposingText(getTextWithUnderline(
mWordComposer.getTypedWord()), 1); mWordComposer.getTypedWord()), 1);
} else { } else {
final boolean swapWeakSpace = maybeStripSpace(inputTransaction, final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(
inputTransaction.mEvent.isSuggestionStripPress()); inputTransaction, inputTransaction.mEvent.isSuggestionStripPress());
sendKeyCodePoint(settingsValues, codePoint); if (swapWeakSpace && trySwapSwapperAndSpace(inputTransaction)) {
if (swapWeakSpace && swapSwapperAndSpace(inputTransaction)) {
mSpaceState = SpaceState.WEAK; mSpaceState = SpaceState.WEAK;
} else {
sendKeyCodePoint(settingsValues, codePoint);
} }
// In case the "add to dictionary" hint was still displayed. // In case the "add to dictionary" hint was still displayed.
mSuggestionStripViewAccessor.dismissAddToDictionaryHint(); mSuggestionStripViewAccessor.dismissAddToDictionaryHint();
@ -779,7 +779,8 @@ public final class InputLogic {
} }
} }
final boolean swapWeakSpace = maybeStripSpace(inputTransaction, isFromSuggestionStrip); final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(
inputTransaction, isFromSuggestionStrip);
final boolean isInsideDoubleQuoteOrAfterDigit = Constants.CODE_DOUBLE_QUOTE == codePoint final boolean isInsideDoubleQuoteOrAfterDigit = Constants.CODE_DOUBLE_QUOTE == codePoint
&& mConnection.isInsideDoubleQuoteOrAfterDigit(); && mConnection.isInsideDoubleQuoteOrAfterDigit();
@ -803,14 +804,14 @@ public final class InputLogic {
promotePhantomSpace(settingsValues); promotePhantomSpace(settingsValues);
} }
if (!shouldAvoidSendingCode) { if (tryPerformDoubleSpacePeriod(inputTransaction)) {
sendKeyCodePoint(settingsValues, codePoint); mSpaceState = SpaceState.DOUBLE;
} inputTransaction.setRequiresUpdateSuggestions();
} else if (swapWeakSpace && trySwapSwapperAndSpace(inputTransaction)) {
if (Constants.CODE_SPACE == codePoint) { mSpaceState = SpaceState.SWAP_PUNCTUATION;
if (maybeDoubleSpacePeriod(inputTransaction)) { mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
mSpaceState = SpaceState.DOUBLE; } else if (Constants.CODE_SPACE == codePoint) {
} else if (!mSuggestedWords.isPunctuationSuggestions()) { if (!mSuggestedWords.isPunctuationSuggestions()) {
mSpaceState = SpaceState.WEAK; mSpaceState = SpaceState.WEAK;
} }
@ -818,12 +819,12 @@ public final class InputLogic {
if (wasComposingWord || mSuggestedWords.isEmpty()) { if (wasComposingWord || mSuggestedWords.isEmpty()) {
inputTransaction.setRequiresUpdateSuggestions(); inputTransaction.setRequiresUpdateSuggestions();
} }
if (!shouldAvoidSendingCode) {
sendKeyCodePoint(settingsValues, codePoint);
}
} else { } else {
if (swapWeakSpace) { if ((SpaceState.PHANTOM == inputTransaction.mSpaceState
if (swapSwapperAndSpace(inputTransaction)) {
mSpaceState = SpaceState.SWAP_PUNCTUATION;
}
} else if ((SpaceState.PHANTOM == inputTransaction.mSpaceState
&& settingsValues.isUsuallyFollowedBySpace(codePoint)) && settingsValues.isUsuallyFollowedBySpace(codePoint))
|| (Constants.CODE_DOUBLE_QUOTE == codePoint || (Constants.CODE_DOUBLE_QUOTE == codePoint
&& isInsideDoubleQuoteOrAfterDigit)) { && isInsideDoubleQuoteOrAfterDigit)) {
@ -841,6 +842,8 @@ public final class InputLogic {
mSpaceState = SpaceState.PHANTOM; mSpaceState = SpaceState.PHANTOM;
} }
sendKeyCodePoint(settingsValues, codePoint);
// Set punctuation right away. onUpdateSelection will fire but tests whether it is // Set punctuation right away. onUpdateSelection will fire but tests whether it is
// already displayed or not, so it's okay. // already displayed or not, so it's okay.
mSuggestionStripViewAccessor.setNeutralSuggestionStrip(); mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
@ -1008,17 +1011,16 @@ public final class InputLogic {
* @param inputTransaction The transaction in progress. * @param inputTransaction The transaction in progress.
* @return true if the swap has been performed, false if it was prevented by preliminary checks. * @return true if the swap has been performed, false if it was prevented by preliminary checks.
*/ */
private boolean swapSwapperAndSpace(final InputTransaction inputTransaction) { private boolean trySwapSwapperAndSpace(final InputTransaction inputTransaction) {
final CharSequence lastTwo = mConnection.getTextBeforeCursor(2, 0); final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
// It is guaranteed lastTwo.charAt(1) is a swapper - else this method is not called. if (Constants.CODE_SPACE != codePointBeforeCursor) {
if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == Constants.CODE_SPACE) { return false;
mConnection.deleteSurroundingText(2, 0);
final String text = lastTwo.charAt(1) + " ";
mConnection.commitText(text, 1);
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
return true;
} }
return false; mConnection.deleteSurroundingText(1, 0);
final String text = inputTransaction.mEvent.getTextToCommit() + " ";
mConnection.commitText(text, 1);
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
return true;
} }
/* /*
@ -1027,8 +1029,8 @@ public final class InputLogic {
* @param isFromSuggestionStrip Whether this code point is coming from the suggestion strip. * @param isFromSuggestionStrip Whether this code point is coming from the suggestion strip.
* @return whether we should swap the space instead of removing it. * @return whether we should swap the space instead of removing it.
*/ */
private boolean maybeStripSpace(final InputTransaction inputTransaction, private boolean tryStripSpaceAndReturnWhetherShouldSwapInstead(
final boolean isFromSuggestionStrip) { final InputTransaction inputTransaction, final boolean isFromSuggestionStrip) {
final int codePoint = inputTransaction.mEvent.mCodePoint; final int codePoint = inputTransaction.mEvent.mCodePoint;
if (Constants.CODE_ENTER == codePoint && if (Constants.CODE_ENTER == codePoint &&
SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) { SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) {
@ -1069,36 +1071,37 @@ public final class InputLogic {
* period-space sequence of characters. This typically happens when the user presses space * period-space sequence of characters. This typically happens when the user presses space
* twice in a row quickly. * twice in a row quickly.
* This method will check that the double-space-to-period is active in settings, that the * This method will check that the double-space-to-period is active in settings, that the
* two spaces have been input close enough together, and that the previous character allows * two spaces have been input close enough together, that the typed character is a space
* for the transformation to take place. If all of these conditions are fulfilled, this * and that the previous character allows for the transformation to take place. If all of
* method applies the transformation and returns true. Otherwise, it does nothing and * these conditions are fulfilled, this method applies the transformation and returns true.
* returns false. * Otherwise, it does nothing and returns false.
* *
* @param inputTransaction The transaction in progress. * @param inputTransaction The transaction in progress.
* @return true if we applied the double-space-to-period transformation, false otherwise. * @return true if we applied the double-space-to-period transformation, false otherwise.
*/ */
private boolean maybeDoubleSpacePeriod(final InputTransaction inputTransaction) { private boolean tryPerformDoubleSpacePeriod(final InputTransaction inputTransaction) {
if (!inputTransaction.mSettingsValues.mUseDoubleSpacePeriod) return false; // Check the setting, the typed character and the countdown. If any of the conditions is
// This can't happen right now because we don't call this method when the code is not space // not fulfilled, return false.
if (Constants.CODE_SPACE != inputTransaction.mEvent.mCodePoint) return false; if (!inputTransaction.mSettingsValues.mUseDoubleSpacePeriod
if (!isDoubleSpacePeriodCountdownActive(inputTransaction)) return false; || Constants.CODE_SPACE != inputTransaction.mEvent.mCodePoint
// We only do this when we see two spaces and an accepted code point before the cursor. || !isDoubleSpacePeriodCountdownActive(inputTransaction)) {
// The code point may be a surrogate pair but the two spaces may not, so we need 4 chars. return false;
final CharSequence lastThree = mConnection.getTextBeforeCursor(4, 0); }
if (null == lastThree) return false; // We only do this when we see one space and an accepted code point before the cursor.
final int length = lastThree.length(); // The code point may be a surrogate pair but the space may not, so we need 3 chars.
if (length < 3) return false; final CharSequence lastTwo = mConnection.getTextBeforeCursor(3, 0);
if (lastThree.charAt(length - 1) != Constants.CODE_SPACE) return false; if (null == lastTwo) return false;
if (lastThree.charAt(length - 2) != Constants.CODE_SPACE) return false; final int length = lastTwo.length();
// We know there are spaces in pos -1 and -2, and we have at least three chars. if (length < 2) return false;
// If we have only three chars, isSurrogatePairs can't return true as charAt(1) is a space, if (lastTwo.charAt(length - 1) != Constants.CODE_SPACE) return false;
// so this is fine. // We know there is a space in pos -1, and we have at least two chars. If we have only two
// chars, isSurrogatePairs can't return true as charAt(1) is a space, so this is fine.
final int firstCodePoint = final int firstCodePoint =
Character.isSurrogatePair(lastThree.charAt(0), lastThree.charAt(1)) ? Character.isSurrogatePair(lastTwo.charAt(0), lastTwo.charAt(1)) ?
Character.codePointAt(lastThree, 0) : lastThree.charAt(length - 3); Character.codePointAt(lastTwo, length - 3) : lastTwo.charAt(length - 2);
if (canBeFollowedByDoubleSpacePeriod(firstCodePoint)) { if (canBeFollowedByDoubleSpacePeriod(firstCodePoint)) {
cancelDoubleSpacePeriodCountdown(); cancelDoubleSpacePeriodCountdown();
mConnection.deleteSurroundingText(2, 0); mConnection.deleteSurroundingText(1, 0);
final String textToInsert = inputTransaction.mSettingsValues.mSpacingAndPunctuations final String textToInsert = inputTransaction.mSettingsValues.mSpacingAndPunctuations
.mSentenceSeparatorAndSpace; .mSentenceSeparatorAndSpace;
mConnection.commitText(textToInsert, 1); mConnection.commitText(textToInsert, 1);