Merge "Rework the logic that tells if the cursor touches words"
commit
1dda9107ca
|
@ -687,13 +687,23 @@ public final class RichInputConnection {
|
|||
}
|
||||
|
||||
public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) {
|
||||
final int codePointBeforeCursor = getCodePointBeforeCursor();
|
||||
if (Constants.NOT_A_CODE == codePointBeforeCursor
|
||||
|| spacingAndPunctuations.isWordSeparator(codePointBeforeCursor)
|
||||
|| spacingAndPunctuations.isWordConnector(codePointBeforeCursor)) {
|
||||
return isCursorFollowedByWordCharacter(spacingAndPunctuations);
|
||||
if (isCursorFollowedByWordCharacter(spacingAndPunctuations)) {
|
||||
// If what's after the cursor is a word character, then we're touching a word.
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
final String textBeforeCursor = mCommittedTextBeforeComposingText.toString();
|
||||
int indexOfCodePointInJavaChars = textBeforeCursor.length();
|
||||
int consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE
|
||||
: textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars);
|
||||
// Search for the first non word-connector char
|
||||
if (spacingAndPunctuations.isWordConnector(consideredCodePoint)) {
|
||||
indexOfCodePointInJavaChars -= Character.charCount(consideredCodePoint);
|
||||
consideredCodePoint = 0 == indexOfCodePointInJavaChars ? Constants.NOT_A_CODE
|
||||
: textBeforeCursor.codePointBefore(indexOfCodePointInJavaChars);
|
||||
}
|
||||
return !(Constants.NOT_A_CODE == consideredCodePoint
|
||||
|| spacingAndPunctuations.isWordSeparator(consideredCodePoint)
|
||||
|| spacingAndPunctuations.isWordConnector(consideredCodePoint));
|
||||
}
|
||||
|
||||
public boolean isCursorFollowedByWordCharacter(
|
||||
|
|
|
@ -89,6 +89,10 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
|
|||
mExtractedText = extractedText;
|
||||
}
|
||||
|
||||
public int cursorPos() {
|
||||
return mTextBefore.length();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int)
|
||||
*/
|
||||
|
@ -131,13 +135,16 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
|
|||
}
|
||||
|
||||
private class MockInputMethodService extends InputMethodService {
|
||||
InputConnection mInputConnection;
|
||||
public void setInputConnection(final InputConnection inputConnection) {
|
||||
mInputConnection = inputConnection;
|
||||
private MockConnection mMockConnection;
|
||||
public void setInputConnection(final MockConnection mockConnection) {
|
||||
mMockConnection = mockConnection;
|
||||
}
|
||||
public int cursorPos() {
|
||||
return mMockConnection.cursorPos();
|
||||
}
|
||||
@Override
|
||||
public InputConnection getCurrentInputConnection() {
|
||||
return mInputConnection;
|
||||
return mMockConnection;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -336,4 +343,82 @@ public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
|
|||
suggestions = r.getSuggestionSpansAtWord();
|
||||
assertEquals(suggestions.length, 0);
|
||||
}
|
||||
|
||||
public void testCursorTouchingWord() {
|
||||
final MockInputMethodService ims = new MockInputMethodService();
|
||||
final RichInputConnection ic = new RichInputConnection(ims);
|
||||
final SpacingAndPunctuations sap = mSpacingAndPunctuations;
|
||||
|
||||
ims.setInputConnection(new MockConnection("users", 5));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("users'", 5));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("users'", 6));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("'users'", 6));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("'users'", 7));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("users '", 6));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("users '", 7));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("re-", 3));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("re--", 4));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("-", 1));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection("--", 2));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" -", 2));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" --", 3));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" users '", 1));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" users '", 3));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" users '", 7));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" users are", 7));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertTrue(ic.isCursorTouchingWord(sap));
|
||||
|
||||
ims.setInputConnection(new MockConnection(" users 'are", 7));
|
||||
ic.resetCachesUponCursorMoveAndReturnSuccess(ims.cursorPos(), ims.cursorPos(), true);
|
||||
assertFalse(ic.isCursorTouchingWord(sap));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue