1
0
Fork 0

Fix small typing regressions (#280)

* fix Predictive mode sometimes starting in UPPERCASE unexpectedly

* fixed incorrect extra auto-space when the cursor is before or in the middle of a word

* no auto-space when the next composing character is whitespace or a special one

* auto-space is added after an emoji
This commit is contained in:
Dimo Karaivanov 2023-06-07 11:01:40 +03:00 committed by GitHub
parent db53e740f4
commit f92ad96827
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 31 deletions

View file

@ -248,7 +248,7 @@ public class TraditionalT9 extends KeyPadHandler {
mInputMode.onAcceptSuggestion(word); mInputMode.onAcceptSuggestion(word);
commitCurrentSuggestion(); commitCurrentSuggestion();
autoCorrectSpace(word, true); autoCorrectSpace(word, true, KeyEvent.KEYCODE_ENTER);
resetKeyRepeat(); resetKeyRepeat();
return true; return true;
@ -329,7 +329,7 @@ public class TraditionalT9 extends KeyPadHandler {
// Automatically accept the current word, when the next one is a space or punctuation, // Automatically accept the current word, when the next one is a space or punctuation,
// instead of requiring "OK" before that. // instead of requiring "OK" before that.
if (mInputMode.shouldAcceptCurrentSuggestion(key, hold, repeat > 0)) { if (mInputMode.shouldAcceptCurrentSuggestion(key, hold, repeat > 0)) {
autoCorrectSpace(acceptIncompleteSuggestion(), false); autoCorrectSpace(acceptIncompleteSuggestion(), false, key);
currentWord = ""; currentWord = "";
} }
@ -359,7 +359,7 @@ public class TraditionalT9 extends KeyPadHandler {
String acceptedWord = acceptIncompleteSuggestion(); String acceptedWord = acceptIncompleteSuggestion();
if (mInputMode.onOtherKey(keyCode)) { if (mInputMode.onOtherKey(keyCode)) {
autoCorrectSpace(acceptedWord, false); autoCorrectSpace(acceptedWord, false, keyCode);
getSuggestions(); getSuggestions();
resetKeyRepeat(); resetKeyRepeat();
return true; return true;
@ -377,12 +377,12 @@ public class TraditionalT9 extends KeyPadHandler {
cancelAutoAccept(); cancelAutoAccept();
// accept the previously typed word (if any) // accept the previously typed word (if any)
autoCorrectSpace(acceptIncompleteSuggestion(), false); autoCorrectSpace(acceptIncompleteSuggestion(), false, -1);
// "type" and accept the text // "type" and accept the text
mInputMode.onAcceptSuggestion(text); mInputMode.onAcceptSuggestion(text);
textField.setText(text); textField.setText(text);
autoCorrectSpace(text, true); autoCorrectSpace(text, true, -1);
return true; return true;
} }
@ -670,12 +670,12 @@ public class TraditionalT9 extends KeyPadHandler {
} }
private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually) { private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually, int nextKey) {
if (mInputMode.shouldDeletePrecedingSpace(inputType)) { if (mInputMode.shouldDeletePrecedingSpace(inputType)) {
textField.deletePrecedingSpace(currentWord); textField.deletePrecedingSpace(currentWord);
} }
if (mInputMode.shouldAddAutoSpace(inputType, textField, isWordAcceptedManually)) { if (mInputMode.shouldAddAutoSpace(inputType, textField, isWordAcceptedManually, nextKey)) {
textField.setText(" "); textField.setText(" ");
} }
} }

View file

@ -98,7 +98,7 @@ abstract public class InputMode {
// Interaction with the IME. Return "true" if it should perform the respective action. // Interaction with the IME. Return "true" if it should perform the respective action.
public boolean shouldAcceptCurrentSuggestion(int key, boolean hold, boolean repeat) { return false; } public boolean shouldAcceptCurrentSuggestion(int key, boolean hold, boolean repeat) { return false; }
public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually) { return false; } public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) { return false; }
public boolean shouldDeletePrecedingSpace(InputType inputType) { return false; } public boolean shouldDeletePrecedingSpace(InputType inputType) { return false; }
public boolean shouldSelectNextSuggestion() { return false; } public boolean shouldSelectNextSuggestion() { return false; }

View file

@ -37,6 +37,7 @@ public class ModePredictive extends InputMode {
ModePredictive(SettingsStore settings, Language lang) { ModePredictive(SettingsStore settings, Language lang) {
changeLanguage(lang); changeLanguage(lang);
defaultTextCase();
autoSpace = new AutoSpace(settings); autoSpace = new AutoSpace(settings);
autoTextCase = new AutoTextCase(settings); autoTextCase = new AutoTextCase(settings);
@ -303,13 +304,13 @@ public class ModePredictive extends InputMode {
@Override @Override
public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually) { public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) {
return autoSpace return autoSpace
.setLastWord(lastAcceptedWord) .setLastWord(lastAcceptedWord)
.setLastSequence(lastAcceptedSequence) .setLastSequence(lastAcceptedSequence)
.setInputType(inputType) .setInputType(inputType)
.setTextField(textField) .setTextField(textField)
.shouldAddAutoSpace(isWordAcceptedManually); .shouldAddAutoSpace(isWordAcceptedManually, nextKey);
} }

View file

@ -8,7 +8,8 @@ import io.github.sspanak.tt9.preferences.SettingsStore;
public class AutoSpace { public class AutoSpace {
private final Pattern isNumber = Pattern.compile("\\s*\\d+\\s*"); private final Pattern isNumber = Pattern.compile("\\s*\\d+\\s*");
private final Pattern isPunctuation = Pattern.compile("\\p{Punct}"); private final Pattern nextIsLetter = Pattern.compile("^\\p{L}+");
private final Pattern nextIsPunctuation = Pattern.compile("^\\p{Punct}");
private final SettingsStore settings; private final SettingsStore settings;
@ -49,32 +50,37 @@ public class AutoSpace {
* *
* See the helper functions for the list of rules. * See the helper functions for the list of rules.
*/ */
public boolean shouldAddAutoSpace(boolean isWordAcceptedManually) { public boolean shouldAddAutoSpace(boolean isWordAcceptedManually, int nextKey) {
String previousChars = textField.getPreviousChars(2); String previousChars = textField.getPreviousChars(2);
String nextChars = textField.getNextChars(2); String nextChars = textField.getNextChars(2);
return return
settings.getAutoSpace() settings.getAutoSpace()
&& !isNumber.matcher(previousChars).find() && !inputType.isSpecialized()
&& (
shouldAddAutoSpaceAfterPunctuation(previousChars)
|| shouldAddAutoSpaceAfterWord(isWordAcceptedManually)
)
&& !nextChars.startsWith(" ") && !nextChars.startsWith(" ")
&& !isPunctuation.matcher(nextChars).find(); && !isNumber.matcher(previousChars).find()
&& !nextIsPunctuation.matcher(nextChars).find()
&& (
shouldAddAfterPunctuation(previousChars, nextKey)
|| shouldAddAfterWord(isWordAcceptedManually, nextChars)
);
} }
/** /**
* shouldAddAutoSpaceAfterPunctuation * shouldAddAfterPunctuation
* Determines whether to automatically adding a space after certain punctuation signs makes sense. * Determines whether to automatically adding a space after certain punctuation signs makes sense.
* The rules are similar to the ones in the standard Android keyboard (with some exceptions, * The rules are similar to the ones in the standard Android keyboard (with some exceptions,
* because we are not using a QWERTY keyboard here). * because we are not using a QWERTY keyboard here).
*/ */
private boolean shouldAddAutoSpaceAfterPunctuation(String previousChars) { private boolean shouldAddAfterPunctuation(String previousChars, int nextKey) {
return return
!inputType.isSpecialized() // no space after whitespace or special characters
&& !previousChars.endsWith(" ") && !previousChars.endsWith("\n") && !previousChars.endsWith("\t") !previousChars.endsWith(" ") && !previousChars.endsWith("\n") && !previousChars.endsWith("\t") // previous whitespace
&& !lastSequence.equals("0") // previous previous math/special char
&& nextKey != 0 // composing (upcoming) whitespace or special character
// add space after the these
&& ( && (
previousChars.endsWith(".") previousChars.endsWith(".")
|| previousChars.endsWith(",") || previousChars.endsWith(",")
@ -92,19 +98,15 @@ public class AutoSpace {
/** /**
* shouldAddAutoSpaceAfterPunctuation * shouldAddAfterPunctuation
* Similar to "shouldAddAutoSpaceAfterPunctuation()", but determines whether to add a space after * Similar to "shouldAddAfterPunctuation()", but determines whether to add a space after words.
* words.
*/ */
private boolean shouldAddAutoSpaceAfterWord(boolean isWordAcceptedManually) { private boolean shouldAddAfterWord(boolean isWordAcceptedManually, String nextChars) {
return return
// Do not add space when auto-accepting words, because it feels very confusing when typing. // Do not add space when auto-accepting words, because it feels very confusing when typing.
isWordAcceptedManually isWordAcceptedManually
// Secondary punctuation // Right before another word
&& !lastSequence.equals("0") && !nextIsLetter.matcher(nextChars).find();
// Emoji
&& !lastSequence.startsWith("1")
&& !inputType.isSpecialized();
} }