Merge "Make prevWord = null if the next word starts from a connector."

main
Keisuke Kuroyanagi 2014-05-30 11:40:55 +00:00 committed by Android (Google) Code Review
commit 83d2bc656f
3 changed files with 31 additions and 9 deletions

View File

@ -547,14 +547,17 @@ public final class RichInputConnection {
// Get information of the nth word before cursor. n = 1 retrieves the word immediately before // Get information of the nth word before cursor. n = 1 retrieves the word immediately before
// the cursor, n = 2 retrieves the word before that, and so on. This splits on whitespace only. // the cursor, n = 2 retrieves the word before that, and so on. This splits on whitespace only.
// Also, it won't return words that end in a separator (if the nth word before the cursor // Also, it won't return words that end in a separator (if the nth word before the cursor
// ends in a separator, it returns information represents beginning-of-sentence). // ends in a separator, it returns information representing beginning-of-sentence).
// Example : // Example :
// (n = 1) "abc def|" -> def // (n = 1) "abc def|" -> def
// (n = 1) "abc def |" -> def // (n = 1) "abc def |" -> def
// (n = 1) "abc 'def|" -> 'def
// (n = 1) "abc def. |" -> beginning-of-sentence // (n = 1) "abc def. |" -> beginning-of-sentence
// (n = 1) "abc def . |" -> beginning-of-sentence // (n = 1) "abc def . |" -> beginning-of-sentence
// (n = 2) "abc def|" -> abc // (n = 2) "abc def|" -> abc
// (n = 2) "abc def |" -> abc // (n = 2) "abc def |" -> abc
// (n = 2) "abc 'def|" -> empty. The context is different from "abc def", but we cannot
// represent this situation using PrevWordsInfo. See TODO in the method.
// (n = 2) "abc def. |" -> abc // (n = 2) "abc def. |" -> abc
// (n = 2) "abc def . |" -> def // (n = 2) "abc def . |" -> def
// (n = 2) "abc|" -> beginning-of-sentence // (n = 2) "abc|" -> beginning-of-sentence
@ -565,6 +568,19 @@ public final class RichInputConnection {
if (prev == null) return PrevWordsInfo.EMPTY_PREV_WORDS_INFO; if (prev == null) return PrevWordsInfo.EMPTY_PREV_WORDS_INFO;
final String[] w = spaceRegex.split(prev); final String[] w = spaceRegex.split(prev);
// Referring to the word after the nth word.
if ((n - 1) > 0 && (n - 1) <= w.length) {
final String wordFollowingTheNthPrevWord = w[w.length - n + 1];
if (!wordFollowingTheNthPrevWord.isEmpty()) {
final char firstChar = wordFollowingTheNthPrevWord.charAt(0);
if (spacingAndPunctuations.isWordConnector(firstChar)) {
// The word following the n-th prev word is starting with a word connector.
// TODO: Return meaningful context for this case.
return PrevWordsInfo.EMPTY_PREV_WORDS_INFO;
}
}
}
// If we can't find n words, or we found an empty word, the context is // If we can't find n words, or we found an empty word, the context is
// beginning-of-sentence. // beginning-of-sentence.
if (w.length < n) { if (w.length < n) {

View File

@ -704,11 +704,10 @@ public final class InputLogic {
(!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations) (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations)
|| !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) { || !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) {
// Reset entirely the composing state anyway, then start composing a new word unless // Reset entirely the composing state anyway, then start composing a new word unless
// the character is a single quote or a dash. The idea here is, single quote and dash // the character is a word connector. The idea here is, word connectors are not
// are not separators and they should be treated as normal characters, except in the // separators and they should be treated as normal characters, except in the first
// first position where they should not start composing a word. // position where they should not start composing a word.
isComposingWord = (Constants.CODE_SINGLE_QUOTE != codePoint isComposingWord = !settingsValues.mSpacingAndPunctuations.isWordConnector(codePoint);
&& Constants.CODE_DASH != codePoint);
// Here we don't need to reset the last composed word. It will be reset // Here we don't need to reset the last composed word. It will be reset
// when we commit this one, if we ever do; if on the other hand we backspace // when we commit this one, if we ever do; if on the other hand we backspace
// it entirely and resume suggestions on the previous word, we'd like to still // it entirely and resume suggestions on the previous word, we'd like to still
@ -719,12 +718,15 @@ public final class InputLogic {
mWordComposer.processEvent(inputTransaction.mEvent); mWordComposer.processEvent(inputTransaction.mEvent);
// If it's the first letter, make note of auto-caps state // If it's the first letter, make note of auto-caps state
if (mWordComposer.isSingleLetter()) { if (mWordComposer.isSingleLetter()) {
// We pass 1 to getPreviousWordForSuggestion because we were not composing a word // We pass 2 to getPreviousWordForSuggestion when the previous code point is a word
// yet, so the word we want is the 1st word before the cursor. // connector. Otherwise, we pass 1 because we were not composing a word yet, so the
// word we want is the 1st word before the cursor.
mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime( mWordComposer.setCapitalizedModeAndPreviousWordAtStartComposingTime(
inputTransaction.mShiftState, inputTransaction.mShiftState,
getPrevWordsInfoFromNthPreviousWordForSuggestion( getPrevWordsInfoFromNthPreviousWordForSuggestion(
settingsValues.mSpacingAndPunctuations, 1 /* nthPreviousWord */)); settingsValues.mSpacingAndPunctuations,
settingsValues.mSpacingAndPunctuations.isWordConnector(
mConnection.getCodePointBeforeCursor()) ? 2 : 1));
} }
mConnection.setComposingText(getTextWithUnderline( mConnection.setComposingText(getTextWithUnderline(
mWordComposer.getTypedWord()), 1); mWordComposer.getTypedWord()), 1);

View File

@ -186,6 +186,8 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
"abc def", mSpacingAndPunctuations, 1).mPrevWord, "def"); "abc def", mSpacingAndPunctuations, 1).mPrevWord, "def");
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord( assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
"abc def ", mSpacingAndPunctuations, 1).mPrevWord, "def"); "abc def ", mSpacingAndPunctuations, 1).mPrevWord, "def");
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
"abc 'def", mSpacingAndPunctuations, 1).mPrevWord, "'def");
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord( assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
"abc def.", mSpacingAndPunctuations, 1), PrevWordsInfo.BEGINNING_OF_SENTENCE); "abc def.", mSpacingAndPunctuations, 1), PrevWordsInfo.BEGINNING_OF_SENTENCE);
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord( assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
@ -196,6 +198,8 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
"abc? def", mSpacingAndPunctuations, 2), PrevWordsInfo.EMPTY_PREV_WORDS_INFO); "abc? def", mSpacingAndPunctuations, 2), PrevWordsInfo.EMPTY_PREV_WORDS_INFO);
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord( assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
"abc! def", mSpacingAndPunctuations, 2), PrevWordsInfo.EMPTY_PREV_WORDS_INFO); "abc! def", mSpacingAndPunctuations, 2), PrevWordsInfo.EMPTY_PREV_WORDS_INFO);
assertEquals(RichInputConnection.getPrevWordsInfoFromNthPreviousWord(
"abc 'def", mSpacingAndPunctuations, 2), PrevWordsInfo.EMPTY_PREV_WORDS_INFO);
} }
/** /**