1
0
Fork 0

cleaned up the recomposing code in ModeWords

This commit is contained in:
sspanak 2025-04-16 17:35:46 +03:00 committed by Dimo Karaivanov
parent 546d20a9bc
commit d54b2b43f7
3 changed files with 6 additions and 49 deletions

View file

@ -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<String> 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.

View file

@ -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();

View file

@ -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);