diff options
author | 2013-01-17 09:07:20 +0000 | |
---|---|---|
committer | 2013-01-17 09:07:20 +0000 | |
commit | 15ea1d40967c25ab27849e1d8c0acf55d8f07e29 (patch) | |
tree | e1ffc0a6dc90d2752099edfead3aafb4f28ed2f8 /java/src | |
parent | 40c59727cf3c1b07a60c28f8df64876eb8ea03e3 (diff) | |
parent | ae3b96b26ef86a9f342b4aeb2047abdaac5e57a5 (diff) | |
download | latinime-15ea1d40967c25ab27849e1d8c0acf55d8f07e29.tar.gz latinime-15ea1d40967c25ab27849e1d8c0acf55d8f07e29.tar.xz latinime-15ea1d40967c25ab27849e1d8c0acf55d8f07e29.zip |
Merge "Simplify the space-before and space-after logic."
Diffstat (limited to 'java/src')
3 files changed, 42 insertions, 84 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 819625225..24d3c089c 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1493,12 +1493,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mSpaceState = SPACE_STATE_PHANTOM; } else { final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor(); - // TODO: reverse this logic. We should have the means to determine whether a character - // should usually be followed by a space, and it should be more readable. - if (Constants.NOT_A_CODE != codePointBeforeCursor - && !Character.isWhitespace(codePointBeforeCursor) - && !mSettings.getCurrent().isPhantomSpacePromotingSymbol(codePointBeforeCursor) - && !mSettings.getCurrent().isWeakSpaceStripper(codePointBeforeCursor)) { + if (mSettings.getCurrent().isUsuallyFollowedBySpace(codePointBeforeCursor)) { mSpaceState = SPACE_STATE_PHANTOM; } } @@ -1775,25 +1770,22 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } } + /* + * Strip a trailing space if necessary and returns whether it's a swap weak space situation. + */ private boolean maybeStripSpace(final int code, final int spaceState, final boolean isFromSuggestionStrip) { if (Constants.CODE_ENTER == code && SPACE_STATE_SWAP_PUNCTUATION == spaceState) { mConnection.removeTrailingSpace(); return false; - } else if ((SPACE_STATE_WEAK == spaceState - || SPACE_STATE_SWAP_PUNCTUATION == spaceState) + } + if ((SPACE_STATE_WEAK == spaceState || SPACE_STATE_SWAP_PUNCTUATION == spaceState) && isFromSuggestionStrip) { - if (mSettings.getCurrent().isWeakSpaceSwapper(code)) { - return true; - } else { - if (mSettings.getCurrent().isWeakSpaceStripper(code)) { - mConnection.removeTrailingSpace(); - } - return false; - } - } else { - return false; + if (mSettings.getCurrent().isUsuallyPrecededBySpace(code)) return false; + if (mSettings.getCurrent().isUsuallyFollowedBySpace(code)) return true; + mConnection.removeTrailingSpace(); } + return false; } private void handleCharacter(final int primaryCode, final int x, @@ -1801,7 +1793,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction boolean isComposingWord = mWordComposer.isComposingWord(); if (SPACE_STATE_PHANTOM == spaceState && - !mSettings.getCurrent().isSymbolExcludedFromWordSeparators(primaryCode)) { + !mSettings.getCurrent().isWordConnector(primaryCode)) { if (isComposingWord) { // Sanity check throw new RuntimeException("Should not be composing here"); @@ -1813,7 +1805,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // dozen milliseconds. Avoid calling it as much as possible, since we are on the UI // thread here. if (!isComposingWord && (isAlphabet(primaryCode) - || mSettings.getCurrent().isSymbolExcludedFromWordSeparators(primaryCode)) + || mSettings.getCurrent().isWordConnector(primaryCode)) && mSettings.getCurrent().isSuggestionsRequested(mDisplayOrientation) && !mConnection.isCursorTouchingWord(mSettings.getCurrent())) { // Reset entirely the composing state anyway, then start composing a new word unless @@ -1885,7 +1877,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction Constants.SUGGESTION_STRIP_COORDINATE == x); if (SPACE_STATE_PHANTOM == spaceState && - mSettings.getCurrent().isPhantomSpacePromotingSymbol(primaryCode)) { + mSettings.getCurrent().isUsuallyPrecededBySpace(primaryCode)) { promotePhantomSpace(); } sendKeyCodePoint(primaryCode); @@ -1900,16 +1892,13 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } mHandler.startDoubleSpacePeriodTimer(); - if (!mConnection.isCursorTouchingWord(mSettings.getCurrent())) { - mHandler.postUpdateSuggestionStrip(); - } + mHandler.postUpdateSuggestionStrip(); } else { if (swapWeakSpace) { swapSwapperAndSpace(); mSpaceState = SPACE_STATE_SWAP_PUNCTUATION; } else if (SPACE_STATE_PHANTOM == spaceState - && !mSettings.getCurrent().isWeakSpaceStripper(primaryCode) - && !mSettings.getCurrent().isPhantomSpacePromotingSymbol(primaryCode)) { + && mSettings.getCurrent().isUsuallyFollowedBySpace(primaryCode)) { // If we are in phantom space state, and the user presses a separator, we want to // stay in phantom space state so that the next keypress has a chance to add the // space. For example, if I type "Good dat", pick "day" from the suggestion strip @@ -2158,9 +2147,9 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // In the batch input mode, a manually picked suggested word should just replace // the current batch input text and there is no need for a phantom space. && !mWordComposer.isBatchMode()) { - int firstChar = Character.codePointAt(suggestion, 0); - if ((!mSettings.getCurrent().isWeakSpaceStripper(firstChar)) - && (!mSettings.getCurrent().isWeakSpaceSwapper(firstChar))) { + final int firstChar = Character.codePointAt(suggestion, 0); + if (!mSettings.getCurrent().isWordSeparator(firstChar) + || mSettings.getCurrent().isUsuallyPrecededBySpace(firstChar)) { promotePhantomSpace(); } } diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java index f7268fc33..0e75533f5 100644 --- a/java/src/com/android/inputmethod/latin/RichInputConnection.java +++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java @@ -577,11 +577,11 @@ public final class RichInputConnection { final CharSequence before = getTextBeforeCursor(1, 0); final CharSequence after = getTextAfterCursor(1, 0); if (!TextUtils.isEmpty(before) && !settingsValues.isWordSeparator(before.charAt(0)) - && !settingsValues.isSymbolExcludedFromWordSeparators(before.charAt(0))) { + && !settingsValues.isWordConnector(before.charAt(0))) { return true; } if (!TextUtils.isEmpty(after) && !settingsValues.isWordSeparator(after.charAt(0)) - && !settingsValues.isSymbolExcludedFromWordSeparators(after.charAt(0))) { + && !settingsValues.isWordConnector(after.charAt(0))) { return true; } return false; @@ -633,12 +633,9 @@ public final class RichInputConnection { final char firstChar = word.charAt(0); // we just tested that word is not empty if (word.length() == 1 && !Character.isLetter(firstChar)) return null; - // We only suggest on words that start with a letter or a symbol that is excluded from - // word separators (see #handleCharacterWhileInBatchEdit). - if (!(Character.isLetter(firstChar) - || settings.isSymbolExcludedFromWordSeparators(firstChar))) { - return null; - } + // We don't restart suggestion if the first character is not a letter, because we don't + // start composing when the first character is not a letter. + if (!Character.isLetter(firstChar)) return null; return word; } diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index f17ac9ca9..1e3bdf0fd 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -37,11 +37,10 @@ public final class SettingsValues { // From resources: public final int mDelayUpdateOldSuggestions; - public final String mWeakSpaceStrippers; - public final String mWeakSpaceSwappers; - private final String mPhantomSpacePromotingSymbols; + public final int[] mSymbolsPrecededBySpace; + public final int[] mSymbolsFollowedBySpace; + public final int[] mWordConnectors; public final SuggestedWords mSuggestPuncList; - private final String mSymbolsExcludedFromWordSeparators; public final String mWordSeparators; public final CharSequence mHintToSaveText; @@ -79,25 +78,19 @@ public final class SettingsValues { final InputAttributes inputAttributes) { // Get the resources mDelayUpdateOldSuggestions = res.getInteger(R.integer.config_delay_update_old_suggestions); - mWeakSpaceStrippers = res.getString(R.string.weak_space_stripping_symbols); - mWeakSpaceSwappers = res.getString(R.string.weak_space_swapping_symbols); - mPhantomSpacePromotingSymbols = res.getString(R.string.phantom_space_promoting_symbols); - if (LatinImeLogger.sDBG) { - final int length = mWeakSpaceStrippers.length(); - for (int i = 0; i < length; i = mWeakSpaceStrippers.offsetByCodePoints(i, 1)) { - if (isWeakSpaceSwapper(mWeakSpaceStrippers.codePointAt(i))) { - throw new RuntimeException("Char code " + mWeakSpaceStrippers.codePointAt(i) - + " is both a weak space swapper and stripper."); - } - } - } + mSymbolsPrecededBySpace = + StringUtils.toCodePointArray(res.getString(R.string.symbols_preceded_by_space)); + Arrays.sort(mSymbolsPrecededBySpace); + mSymbolsFollowedBySpace = + StringUtils.toCodePointArray(res.getString(R.string.symbols_followed_by_space)); + Arrays.sort(mSymbolsFollowedBySpace); + mWordConnectors = + StringUtils.toCodePointArray(res.getString(R.string.symbols_word_connectors)); + Arrays.sort(mWordConnectors); final String[] suggestPuncsSpec = KeySpecParser.parseCsvString( res.getString(R.string.suggested_punctuations), null); mSuggestPuncList = createSuggestPuncList(suggestPuncsSpec); - mSymbolsExcludedFromWordSeparators = - res.getString(R.string.symbols_excluded_from_word_separators); - mWordSeparators = createWordSeparators(mWeakSpaceStrippers, mWeakSpaceSwappers, - mSymbolsExcludedFromWordSeparators, res); + mWordSeparators = res.getString(R.string.symbols_word_separators); mHintToSaveText = res.getText(R.string.hint_add_to_dictionary); // Store the input attributes @@ -169,25 +162,16 @@ public final class SettingsValues { return mWordSeparators.contains(String.valueOf((char)code)); } - public boolean isSymbolExcludedFromWordSeparators(final int code) { - return mSymbolsExcludedFromWordSeparators.contains(String.valueOf((char)code)); - } - - // TODO: use "Phantom" instead of "Weak" in this method name - public boolean isWeakSpaceStripper(final int code) { - // TODO: this does not work if the code does not fit in a char - return mWeakSpaceStrippers.contains(String.valueOf((char)code)); + public boolean isWordConnector(final int code) { + return Arrays.binarySearch(mWordConnectors, code) >= 0; } - // TODO: use "Phantom" instead of "Weak" in this method name - public boolean isWeakSpaceSwapper(final int code) { - // TODO: this does not work if the code does not fit in a char - return mWeakSpaceSwappers.contains(String.valueOf((char)code)); + public boolean isUsuallyPrecededBySpace(final int code) { + return Arrays.binarySearch(mSymbolsPrecededBySpace, code) >= 0; } - public boolean isPhantomSpacePromotingSymbol(final int code) { - // TODO: this does not work if the code does not fit in a char - return mPhantomSpacePromotingSymbols.contains(String.valueOf((char)code)); + public boolean isUsuallyFollowedBySpace(final int code) { + return Arrays.binarySearch(mSymbolsFollowedBySpace, code) >= 0; } public boolean shouldInsertSpacesAutomatically() { @@ -239,18 +223,6 @@ public final class SettingsValues { false /* isPrediction */); } - private static String createWordSeparators(final String weakSpaceStrippers, - final String weakSpaceSwappers, final String symbolsExcludedFromWordSeparators, - final Resources res) { - String wordSeparators = weakSpaceStrippers + weakSpaceSwappers - + res.getString(R.string.phantom_space_promoting_symbols); - for (int i = symbolsExcludedFromWordSeparators.length() - 1; i >= 0; --i) { - wordSeparators = wordSeparators.replace( - symbolsExcludedFromWordSeparators.substring(i, i + 1), ""); - } - return wordSeparators; - } - private static final int SUGGESTION_VISIBILITY_SHOW_VALUE = R.string.prefs_suggestion_visibility_show_value; private static final int SUGGESTION_VISIBILITY_SHOW_ONLY_PORTRAIT_VALUE = |