aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/EditingUtils.java
diff options
context:
space:
mode:
authorKurt Partridge <kep@google.com>2012-05-09 16:04:26 -0700
committerKurt Partridge <kep@google.com>2012-05-14 09:55:26 -0700
commitaec44d50a7534d8704a7006b4f90f5e8040a931b (patch)
treee7e378738239c8b9a39ffd633a9fe5985705284a /java/src/com/android/inputmethod/latin/EditingUtils.java
parent2b49579961198bc9416a8b02567362ca460303a7 (diff)
downloadlatinime-aec44d50a7534d8704a7006b4f90f5e8040a931b.tar.gz
latinime-aec44d50a7534d8704a7006b4f90f5e8040a931b.tar.xz
latinime-aec44d50a7534d8704a7006b4f90f5e8040a931b.zip
include text context in researchLogger
when logging LatinIME.onUpdateSelection(), now include the current word and preceding word. no escaping of the word is performed; this is temporary until the output format is cleaned up. also fix EditingUtils.getWordRangeAtCursor to support supplementary UTF-16 characters. Bug: 6188932 Change-Id: If4612a2627537d5d8bb2f9585a3ad1b4e56c2e26
Diffstat (limited to 'java/src/com/android/inputmethod/latin/EditingUtils.java')
-rw-r--r--java/src/com/android/inputmethod/latin/EditingUtils.java52
1 files changed, 44 insertions, 8 deletions
diff --git a/java/src/com/android/inputmethod/latin/EditingUtils.java b/java/src/com/android/inputmethod/latin/EditingUtils.java
index 93106ac27..be4cb7787 100644
--- a/java/src/com/android/inputmethod/latin/EditingUtils.java
+++ b/java/src/com/android/inputmethod/latin/EditingUtils.java
@@ -54,7 +54,7 @@ public class EditingUtils {
*/
public static String getWordAtCursor(InputConnection connection, String separators) {
// getWordRangeAtCursor returns null if the connection is null
- Range r = getWordRangeAtCursor(connection, separators);
+ Range r = getWordRangeAtCursor(connection, separators, 0);
return (r == null) ? null : r.mWord;
}
@@ -84,7 +84,17 @@ public class EditingUtils {
}
}
- private static Range getWordRangeAtCursor(InputConnection connection, String sep) {
+ /**
+ * Returns the text surrounding the cursor.
+ *
+ * @param connection the InputConnection to the TextView
+ * @param sep a string of characters that split words.
+ * @param additionalPrecedingWordsCount the number of words before the current word that should
+ * be included in the returned range
+ * @return a range containing the text surrounding the cursor
+ */
+ public static Range getWordRangeAtCursor(InputConnection connection, String sep,
+ int additionalPrecedingWordsCount) {
if (connection == null || sep == null) {
return null;
}
@@ -94,14 +104,40 @@ public class EditingUtils {
return null;
}
- // Find first word separator before the cursor
+ // Going backward, alternate skipping non-separators and separators until enough words
+ // have been read.
int start = before.length();
- while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--;
+ boolean isStoppingAtWhitespace = true; // toggles to indicate what to stop at
+ while (true) { // see comments below for why this is guaranteed to halt
+ while (start > 0) {
+ final int codePoint = Character.codePointBefore(before, start);
+ if (isStoppingAtWhitespace == isSeparator(codePoint, sep)) {
+ break; // inner loop
+ }
+ --start;
+ if (Character.isSupplementaryCodePoint(codePoint)) {
+ --start;
+ }
+ }
+ // isStoppingAtWhitespace is true every other time through the loop,
+ // so additionalPrecedingWordsCount is guaranteed to become < 0, which
+ // guarantees outer loop termination
+ if (isStoppingAtWhitespace && (--additionalPrecedingWordsCount < 0)) {
+ break; // outer loop
+ }
+ isStoppingAtWhitespace = !isStoppingAtWhitespace;
+ }
// Find last word separator after the cursor
int end = -1;
- while (++end < after.length() && !isWhitespace(after.charAt(end), sep)) {
- // Nothing to do here.
+ while (++end < after.length()) {
+ final int codePoint = Character.codePointAt(after, end);
+ if (isSeparator(codePoint, sep)) {
+ break;
+ }
+ if (Character.isSupplementaryCodePoint(codePoint)) {
+ ++end;
+ }
}
int cursor = getCursorPosition(connection);
@@ -114,8 +150,8 @@ public class EditingUtils {
return null;
}
- private static boolean isWhitespace(int code, String whitespace) {
- return whitespace.contains(String.valueOf((char) code));
+ private static boolean isSeparator(int code, String sep) {
+ return sep.indexOf(code) != -1;
}
private static final Pattern spaceRegex = Pattern.compile("\\s+");