Fix getNthPreviousWord.

This should take into accounts word connectors.

Change-Id: Ic7fa5c837cd65a43ba43d7ae9d299b8d20019892
main
Jean Chalard 2013-11-27 18:20:10 +09:00
parent 4735e1d32a
commit da459787e2
3 changed files with 21 additions and 18 deletions

View File

@ -2541,7 +2541,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (currentSettings.mCurrentLanguageHasSpaces) {
// If we are typing in a language with spaces we can just look up the previous
// word from textview.
return mConnection.getNthPreviousWord(currentSettings.mWordSeparators,
return mConnection.getNthPreviousWord(currentSettings,
mWordComposer.isComposingWord() ? 2 : 1);
} else {
return LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord ? null
@ -2832,7 +2832,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
final UserHistoryDictionary userHistoryDictionary = mUserHistoryDictionary;
if (userHistoryDictionary == null) return null;
final String prevWord = mConnection.getNthPreviousWord(currentSettings.mWordSeparators, 2);
final String prevWord = mConnection.getNthPreviousWord(currentSettings, 2);
final String secondWord;
if (mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps()) {
secondWord = suggestion.toLowerCase(mSubtypeSwitcher.getCurrentSubtypeLocale());

View File

@ -538,7 +538,7 @@ public final class RichInputConnection {
}
@SuppressWarnings("unused")
public String getNthPreviousWord(final String sentenceSeperators, final int n) {
public String getNthPreviousWord(final SettingsValues currentSettingsValues, final int n) {
mIC = mParent.getCurrentInputConnection();
if (null == mIC) return null;
final CharSequence prev = getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
@ -557,7 +557,7 @@ public final class RichInputConnection {
}
}
}
return getNthPreviousWord(prev, sentenceSeperators, n);
return getNthPreviousWord(prev, currentSettingsValues, n);
}
private static boolean isSeparator(int code, String sep) {
@ -581,7 +581,7 @@ public final class RichInputConnection {
// (n = 2) "abc |" -> null
// (n = 2) "abc. def|" -> null
public static String getNthPreviousWord(final CharSequence prev,
final String sentenceSeperators, final int n) {
final SettingsValues currentSettingsValues, final int n) {
if (prev == null) return null;
final String[] w = spaceRegex.split(prev);
@ -593,7 +593,8 @@ public final class RichInputConnection {
// If ends in a separator, return null
final char lastChar = nthPrevWord.charAt(length - 1);
if (sentenceSeperators.contains(String.valueOf(lastChar))) return null;
if (currentSettingsValues.isWordSeparator(lastChar)
|| currentSettingsValues.isWordConnector(lastChar)) return null;
return nthPrevWord;
}

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.latin;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.TextRange;
import android.inputmethodservice.InputMethodService;
@ -39,7 +40,8 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
// The following is meant to be a reasonable default for
// the "word_separators" resource.
private static final String sSeparators = ".,:;!?-";
private static final SettingsValues sSettings =
SettingsValues.makeDummySettingsValuesForTest(Locale.ENGLISH);
@Override
protected void setUp() throws Exception {
@ -137,9 +139,9 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
*/
public void testGetPreviousWord() {
// If one of the following cases breaks, the bigram suggestions won't work.
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 2), "abc");
assertNull(RichInputConnection.getNthPreviousWord("abc", sSeparators, 2));
assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSeparators, 2));
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSettings, 2), "abc");
assertNull(RichInputConnection.getNthPreviousWord("abc", sSettings, 2));
assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSettings, 2));
// The following tests reflect the current behavior of the function
// RichInputConnection#getNthPreviousWord.
@ -148,15 +150,15 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
// this function if needed - especially since it does not seem very
// logical. These tests are just there to catch any unintentional
// changes in the behavior of the RichInputConnection#getPreviousWord method.
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 2), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc ", sSeparators, 2));
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSettings, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSettings, 2), "abc");
assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSettings, 2), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc ", sSettings, 2));
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 1), "def");
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 1), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 1));
assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 1));
assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSettings, 1), "def");
assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSettings, 1), "def");
assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSettings, 1));
assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSettings, 1));
}
/**