aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/EditingUtil.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--java/src/com/android/inputmethod/latin/EditingUtil.java (renamed from java/src/com/android/inputmethod/voice/EditingUtil.java)99
1 files changed, 83 insertions, 16 deletions
diff --git a/java/src/com/android/inputmethod/voice/EditingUtil.java b/java/src/com/android/inputmethod/latin/EditingUtil.java
index 6316d8ccf..be31cb787 100644
--- a/java/src/com/android/inputmethod/voice/EditingUtil.java
+++ b/java/src/com/android/inputmethod/latin/EditingUtil.java
@@ -14,16 +14,23 @@
* the License.
*/
-package com.android.inputmethod.voice;
+package com.android.inputmethod.latin;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
+import java.util.regex.Pattern;
+
/**
* Utility methods to deal with editing text through an InputConnection.
*/
public class EditingUtil {
+ /**
+ * Number of characters we want to look back in order to identify the previous word
+ */
+ private static final int LOOKBACK_CHARACTER_NUM = 15;
+
private EditingUtil() {};
/**
@@ -75,9 +82,21 @@ public class EditingUtil {
* represents the cursor, then "hello " will be returned.
*/
public static String getWordAtCursor(
- InputConnection connection, String separators) {
- Range range = getWordRangeAtCursor(connection, separators);
- return (range == null) ? null : range.word;
+ InputConnection connection, String separators) {
+ return getWordAtCursor(connection, separators, null);
+ }
+
+ /**
+ * @param connection connection to the current text field.
+ * @param sep characters which may separate words
+ * @return the word that surrounds the cursor, including up to one trailing
+ * separator. For example, if the field contains "he|llo world", where |
+ * represents the cursor, then "hello " will be returned.
+ */
+ public static String getWordAtCursor(
+ InputConnection connection, String separators, Range range) {
+ Range r = getWordRangeAtCursor(connection, separators, range);
+ return (r == null) ? null : r.word;
}
/**
@@ -87,7 +106,7 @@ public class EditingUtil {
public static void deleteWordAtCursor(
InputConnection connection, String separators) {
- Range range = getWordRangeAtCursor(connection, separators);
+ Range range = getWordRangeAtCursor(connection, separators, null);
if (range == null) return;
connection.finishComposingText();
@@ -101,18 +120,20 @@ public class EditingUtil {
/**
* Represents a range of text, relative to the current cursor position.
*/
- private static class Range {
+ public static class Range {
/** Characters before selection start */
- int charsBefore;
+ public int charsBefore;
/**
* Characters after selection start, including one trailing word
* separator.
*/
- int charsAfter;
+ public int charsAfter;
/** The actual characters that make up a word */
- String word;
+ public String word;
+
+ public Range() {}
public Range(int charsBefore, int charsAfter, String word) {
if (charsBefore < 0 || charsAfter < 0) {
@@ -125,7 +146,7 @@ public class EditingUtil {
}
private static Range getWordRangeAtCursor(
- InputConnection connection, String sep) {
+ InputConnection connection, String sep, Range range) {
if (connection == null || sep == null) {
return null;
}
@@ -137,20 +158,22 @@ public class EditingUtil {
// Find first word separator before the cursor
int start = before.length();
- while (--start > 0 && !isWhitespace(before.charAt(start - 1), sep));
+ while (start > 0 && !isWhitespace(before.charAt(start - 1), sep)) start--;
// Find last word separator after the cursor
int end = -1;
while (++end < after.length() && !isWhitespace(after.charAt(end), sep));
- if (end < after.length() - 1) {
- end++; // Include trailing space, if it exists, in word
- }
int cursor = getCursorPosition(connection);
if (start >= 0 && cursor + end <= after.length() + before.length()) {
String word = before.toString().substring(start, before.length())
- + after.toString().substring(0, end);
- return new Range(before.length() - start, end, word);
+ + after.toString().substring(0, end);
+
+ Range returnRange = range != null? range : new Range();
+ returnRange.charsBefore = before.length() - start;
+ returnRange.charsAfter = end;
+ returnRange.word = word;
+ return returnRange;
}
return null;
@@ -159,4 +182,48 @@ public class EditingUtil {
private static boolean isWhitespace(int code, String whitespace) {
return whitespace.contains(String.valueOf((char) code));
}
+
+ private static final Pattern spaceRegex = Pattern.compile("\\s+");
+
+ public static CharSequence getPreviousWord(InputConnection connection,
+ String sentenceSeperators) {
+ //TODO: Should fix this. This could be slow!
+ CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
+ if (prev == null) {
+ return null;
+ }
+ String[] w = spaceRegex.split(prev);
+ if (w.length >= 2 && w[w.length-2].length() > 0) {
+ char lastChar = w[w.length-2].charAt(w[w.length-2].length() -1);
+ if (sentenceSeperators.contains(String.valueOf(lastChar))) {
+ return null;
+ }
+ return w[w.length-2];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Checks if the cursor is touching/inside a word or the selection is for a whole
+ * word and no more and no less.
+ * @param range the Range object that contains the bounds of the word around the cursor
+ * @param start the start of the selection
+ * @param end the end of the selection, which could be the same as the start, if text is not
+ * in selection mode
+ * @return false if the selection is a partial word or straddling multiple words, true if
+ * the selection is a full word or there is no selection.
+ */
+ public static boolean isFullWordOrInside(Range range, int start, int end) {
+ // Is the cursor inside or touching a word?
+ if (start == end) return true;
+
+ // Is it a selection? Then is the start of the selection the start of the word and
+ // the size of the selection the size of the word? Then return true
+ if (start < end
+ && (range.charsBefore == 0 && range.charsAfter == end - start)) {
+ return true;
+ }
+ return false;
+ }
}