From d54b2b43f74b987b4a646e3b5458ffbe46945f93 Mon Sep 17 00:00:00 2001 From: sspanak Date: Wed, 16 Apr 2025 17:35:46 +0300 Subject: [PATCH] cleaned up the recomposing code in ModeWords --- .../sspanak/tt9/ime/helpers/TextField.java | 48 +------------------ .../sspanak/tt9/ime/modes/ModeWords.java | 3 +- .../java/io/github/sspanak/tt9/util/Text.java | 4 +- 3 files changed, 6 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java index ed918fef..84c71c84 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/helpers/TextField.java @@ -11,8 +11,6 @@ import android.view.inputmethod.InputConnection; import androidx.annotation.NonNull; -import java.util.ArrayList; - import io.github.sspanak.tt9.hacks.InputType; import io.github.sspanak.tt9.ime.modes.InputMode; import io.github.sspanak.tt9.languages.Language; @@ -42,13 +40,13 @@ public class TextField extends InputField { @NonNull public String getStringAfterCursor(int numberOfChars) { - CharSequence chars = connection != null ? connection.getTextAfterCursor(numberOfChars, 0) : null; + CharSequence chars = connection != null && numberOfChars > 0 ? connection.getTextAfterCursor(numberOfChars, 0) : null; return chars != null ? chars.toString() : ""; } @NonNull public String getStringBeforeCursor(int numberOfChars) { - CharSequence chars = connection != null ? connection.getTextBeforeCursor(numberOfChars, 0) : null; + CharSequence chars = connection != null && numberOfChars > 0 ? connection.getTextBeforeCursor(numberOfChars, 0) : null; return chars != null ? chars.toString() : ""; } @@ -109,48 +107,6 @@ public class TextField extends InputField { } - /** - * Returns a word (String containing of alphabetic) characters before the cursor, only if the cursor is - * not in the middle of that word. "skipWords" can be used to return the N-th word before the cursor. - * "stopAtPunctuation" can be used to stop searching at the first punctuation character. In case - * no complete word is found due to any reason, an empty string is returned. - */ - @NonNull public String getWordBeforeCursor(Language language, int skipWords, boolean stopAtPunctuation) { - if (getTextAfterCursor(1).startsWithWord()) { - return ""; - } - - String before = getStringBeforeCursor(); - if (before.isEmpty() || !Character.isAlphabetic(before.charAt(before.length() - 1))) { - return ""; - } - - int endIndex = before.length(); - ArrayList punctuation = language.getKeyCharacters(1); - - for (int i = before.length() - 1; i >= 0; i--) { - char currentLetter = before.charAt(i); - - if (stopAtPunctuation && punctuation.contains(String.valueOf(currentLetter))) { - return ""; - } - - if ( - !Character.isAlphabetic(currentLetter) - && !(currentLetter == '\'' && (LanguageKind.isHebrew(language) || LanguageKind.isUkrainian(language))) - ) { - if (skipWords-- <= 0 || i == 0) { - return i + 1 >= endIndex ? "" : before.substring(i + 1, endIndex); - } else { - endIndex = i; - } - } - } - - return endIndex == before.length() ? before : before.substring(0, endIndex); - } - - /** * Returns the length of the first word before the cursor including any whitespace after it. * If the cursor is inside a word, 0 is returned, because there is no full word before it. diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/modes/ModeWords.java b/app/src/main/java/io/github/sspanak/tt9/ime/modes/ModeWords.java index 0fe61d9f..622bef91 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/modes/ModeWords.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/modes/ModeWords.java @@ -137,7 +137,8 @@ class ModeWords extends ModeCheonjiin { return null; } - String previousWord = textField.getWordBeforeCursor(language, 0, false); + boolean includeApostrophes = LanguageKind.isUkrainian(language) || LanguageKind.isHebrew(language); + String previousWord = textField.getTextBeforeCursor().getPreviousWord(false, includeApostrophes); if (previousWord.length() < 2 || previousWord.contains(" ")) { Logger.d(LOG_TAG, "Not recomposing invalid word: '" + previousWord + "'"); textCase = settings.getTextCase(); diff --git a/app/src/main/java/io/github/sspanak/tt9/util/Text.java b/app/src/main/java/io/github/sspanak/tt9/util/Text.java index 12e10be5..dbbf3d89 100644 --- a/app/src/main/java/io/github/sspanak/tt9/util/Text.java +++ b/app/src/main/java/io/github/sspanak/tt9/util/Text.java @@ -63,13 +63,13 @@ public class Text extends TextTools { @NonNull - public String getPreviousWord(boolean skipOne, boolean isLanguageWithApostrophes) { + public String getPreviousWord(boolean skipOne, boolean includeApostrophes) { if (text == null || text.isEmpty()) { return ""; } Matcher matcher; - if (isLanguageWithApostrophes) { + if (includeApostrophes) { // Ukrainian and Hebrew matcher = skipOne ? PENULTIMATE_WORD_WITH_APOSTROPHES.matcher(text) : PREVIOUS_WORD_WITH_APOSTROPHES.matcher(text); } else { matcher = skipOne ? PENULTIMATE_WORD.matcher(text) : PREVIOUS_WORD.matcher(text);