aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/RichInputConnection.java
diff options
context:
space:
mode:
authorSatoshi Kataoka <satok@google.com>2012-07-09 18:23:49 +0900
committerSatoshi Kataoka <satok@google.com>2012-07-09 18:23:49 +0900
commit516a0bb626a4d4406f67e1eca640b20f53251b5f (patch)
tree4b7d40c53186daf0d9f39480599bc5eda6d865b4 /java/src/com/android/inputmethod/latin/RichInputConnection.java
parentf4eb4f0c79f8116402a59339f151ca2c62fd7868 (diff)
parent7389c601e34dc96807aa1c3c0fef30b77198ca58 (diff)
downloadlatinime-516a0bb626a4d4406f67e1eca640b20f53251b5f.tar.gz
latinime-516a0bb626a4d4406f67e1eca640b20f53251b5f.tar.xz
latinime-516a0bb626a4d4406f67e1eca640b20f53251b5f.zip
Merge remote-tracking branch 'goog/master' into mergescriptpackage
Diffstat (limited to 'java/src/com/android/inputmethod/latin/RichInputConnection.java')
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputConnection.java70
1 files changed, 24 insertions, 46 deletions
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index a37f480b7..5786978a8 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -139,12 +139,11 @@ public class RichInputConnection {
if (null != mIC) mIC.commitCompletion(completionInfo);
}
- public CharSequence getPreviousWord(final String sentenceSeperators) {
+ public CharSequence getNthPreviousWord(final String sentenceSeperators, final int n) {
mIC = mParent.getCurrentInputConnection();
- //TODO: Should fix this. This could be slow!
if (null == mIC) return null;
- CharSequence prev = mIC.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
- return getPreviousWord(prev, sentenceSeperators);
+ final CharSequence prev = mIC.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
+ return getNthPreviousWord(prev, sentenceSeperators, n);
}
/**
@@ -177,56 +176,35 @@ public class RichInputConnection {
return sep.indexOf(code) != -1;
}
- // Get the word before the whitespace preceding the non-whitespace preceding the cursor.
- // Also, it won't return words that end in a separator.
- // Example :
- // "abc def|" -> abc
- // "abc def |" -> abc
- // "abc def. |" -> abc
- // "abc def . |" -> def
- // "abc|" -> null
- // "abc |" -> null
- // "abc. def|" -> null
- public static CharSequence getPreviousWord(CharSequence prev, String sentenceSeperators) {
- if (prev == null) return null;
- String[] w = spaceRegex.split(prev);
-
- // If we can't find two words, or we found an empty word, return null.
- if (w.length < 2 || w[w.length - 2].length() <= 0) return null;
-
- // If ends in a separator, return null
- 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];
- }
-
- public CharSequence getThisWord(String sentenceSeperators) {
- mIC = mParent.getCurrentInputConnection();
- if (null == mIC) return null;
- final CharSequence prev = mIC.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0);
- return getThisWord(prev, sentenceSeperators);
- }
-
- // Get the word immediately before the cursor, even if there is whitespace between it and
- // the cursor - but not if there is punctuation.
+ // Get 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.
+ // Also, it won't return words that end in a separator (if the nth word before the cursor
+ // ends in a separator, it returns null).
// Example :
- // "abc def|" -> def
- // "abc def |" -> def
- // "abc def. |" -> null
- // "abc def . |" -> null
- public static CharSequence getThisWord(CharSequence prev, String sentenceSeperators) {
+ // (n = 1) "abc def|" -> def
+ // (n = 1) "abc def |" -> def
+ // (n = 1) "abc def. |" -> null
+ // (n = 1) "abc def . |" -> null
+ // (n = 2) "abc def|" -> abc
+ // (n = 2) "abc def |" -> abc
+ // (n = 2) "abc def. |" -> abc
+ // (n = 2) "abc def . |" -> def
+ // (n = 2) "abc|" -> null
+ // (n = 2) "abc |" -> null
+ // (n = 2) "abc. def|" -> null
+ public static CharSequence getNthPreviousWord(final CharSequence prev,
+ final String sentenceSeperators, final int n) {
if (prev == null) return null;
String[] w = spaceRegex.split(prev);
- // No word : return null
- if (w.length < 1 || w[w.length - 1].length() <= 0) return null;
+ // If we can't find n words, or we found an empty word, return null.
+ if (w.length < n || w[w.length - n].length() <= 0) return null;
// If ends in a separator, return null
- char lastChar = w[w.length - 1].charAt(w[w.length - 1].length() - 1);
+ char lastChar = w[w.length - n].charAt(w[w.length - n].length() - 1);
if (sentenceSeperators.contains(String.valueOf(lastChar))) return null;
- return w[w.length - 1];
+ return w[w.length - n];
}
/**