From da459787e23bc790317d8c0d74a4675b9a8f2de8 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 27 Nov 2013 18:20:10 +0900 Subject: [PATCH] Fix getNthPreviousWord. This should take into accounts word connectors. Change-Id: Ic7fa5c837cd65a43ba43d7ae9d299b8d20019892 --- .../android/inputmethod/latin/LatinIME.java | 4 +-- .../latin/RichInputConnection.java | 9 ++++--- .../RichInputConnectionAndTextRangeTests.java | 26 ++++++++++--------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 2e5bb19d6..d14066c54 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -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()); diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java index b5ea0de01..37311acf2 100644 --- a/java/src/com/android/inputmethod/latin/RichInputConnection.java +++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java @@ -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; } diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java index c0dd9933c..6ad125053 100644 --- a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java +++ b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java @@ -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)); } /**