auto-space code cleanup
This commit is contained in:
parent
23074a3bc2
commit
4fd1fb4378
5 changed files with 39 additions and 94 deletions
|
|
@ -221,11 +221,11 @@ public abstract class TypingHandler extends KeyPadHandler {
|
||||||
|
|
||||||
|
|
||||||
private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually, int nextKey) {
|
private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually, int nextKey) {
|
||||||
if (!inputType.isRustDesk() && mInputMode.shouldDeletePrecedingSpace(inputType)) {
|
if (!inputType.isRustDesk() && mInputMode.shouldDeletePrecedingSpace(inputType, textField)) {
|
||||||
textField.deletePrecedingSpace(currentWord);
|
textField.deletePrecedingSpace(currentWord);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mInputMode.shouldAddPrecedingSpace(textField)) {
|
if (mInputMode.shouldAddPrecedingSpace(inputType, textField)) {
|
||||||
textField.addPrecedingSpace(currentWord);
|
textField.addPrecedingSpace(currentWord);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -200,8 +200,8 @@ public class TextField extends InputField {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* deletePrecedingSpace
|
* deletePrecedingSpace
|
||||||
* Deletes the preceding space before the given word. The word must be before the cursor.
|
* Deletes the space before the given word. The word must be before the cursor.
|
||||||
* No action is taken when there is double space or when it's the beginning of the text field.
|
* No action is taken when there is no such word.
|
||||||
*/
|
*/
|
||||||
public void deletePrecedingSpace(String word) {
|
public void deletePrecedingSpace(String word) {
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
|
|
@ -211,13 +211,8 @@ public class TextField extends InputField {
|
||||||
String searchText = " " + word;
|
String searchText = " " + word;
|
||||||
|
|
||||||
connection.beginBatchEdit();
|
connection.beginBatchEdit();
|
||||||
CharSequence beforeText = connection.getTextBeforeCursor(searchText.length() + 1, 0);
|
CharSequence beforeText = connection.getTextBeforeCursor(searchText.length(), 0);
|
||||||
if (
|
if (beforeText == null || !beforeText.equals(searchText)) {
|
||||||
beforeText == null
|
|
||||||
|| beforeText.length() < searchText.length() + 1
|
|
||||||
|| beforeText.charAt(1) != ' ' // preceding char must be " "
|
|
||||||
|| beforeText.charAt(0) == ' ' // but do nothing when there is double space
|
|
||||||
) {
|
|
||||||
connection.endBatchEdit();
|
connection.endBatchEdit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,8 +107,8 @@ abstract public class InputMode {
|
||||||
public boolean shouldAcceptPreviousSuggestion() { return false; }
|
public boolean shouldAcceptPreviousSuggestion() { return false; }
|
||||||
public boolean shouldAcceptPreviousSuggestion(int nextKey) { return false; }
|
public boolean shouldAcceptPreviousSuggestion(int nextKey) { return false; }
|
||||||
public boolean shouldAddTrailingSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) { return false; }
|
public boolean shouldAddTrailingSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) { return false; }
|
||||||
public boolean shouldAddPrecedingSpace(TextField textField) { return false; }
|
public boolean shouldAddPrecedingSpace(InputType inputType, TextField textField) { return false; }
|
||||||
public boolean shouldDeletePrecedingSpace(InputType inputType) { return false; }
|
public boolean shouldDeletePrecedingSpace(InputType inputType, TextField textField) { return false; }
|
||||||
public boolean shouldIgnoreText(String text) { return text == null || text.isEmpty(); }
|
public boolean shouldIgnoreText(String text) { return text == null || text.isEmpty(); }
|
||||||
public boolean shouldSelectNextSuggestion() { return false; }
|
public boolean shouldSelectNextSuggestion() { return false; }
|
||||||
public boolean recompose(String word) { return false; }
|
public boolean recompose(String word) { return false; }
|
||||||
|
|
|
||||||
|
|
@ -465,30 +465,19 @@ public class ModePredictive extends InputMode {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldAddTrailingSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) {
|
public boolean shouldAddTrailingSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int nextKey) {
|
||||||
return autoSpace
|
return autoSpace.shouldAddTrailingSpace(textField, inputType, isWordAcceptedManually, nextKey);
|
||||||
.setLastWord(lastAcceptedWord)
|
|
||||||
.setLastSequence()
|
|
||||||
.setInputType(inputType)
|
|
||||||
.setTextField(textField)
|
|
||||||
.shouldAddTrailingSpace(isWordAcceptedManually, nextKey);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldAddPrecedingSpace(TextField textField) {
|
public boolean shouldAddPrecedingSpace(InputType inputType, TextField textField) {
|
||||||
return autoSpace
|
return autoSpace.shouldAddBeforePunctuation(inputType, textField);
|
||||||
.setTextField(textField)
|
|
||||||
.shouldAddBeforePunctuation();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldDeletePrecedingSpace(InputType inputType) {
|
public boolean shouldDeletePrecedingSpace(InputType inputType, TextField textField) {
|
||||||
return autoSpace
|
return autoSpace.shouldDeletePrecedingSpace(inputType, textField);
|
||||||
.setLastWord(lastAcceptedWord)
|
|
||||||
.setInputType(inputType)
|
|
||||||
.setTextField(null)
|
|
||||||
.shouldDeletePrecedingSpace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package io.github.sspanak.tt9.ime.modes.helpers;
|
package io.github.sspanak.tt9.ime.modes.helpers;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.hacks.InputType;
|
import io.github.sspanak.tt9.hacks.InputType;
|
||||||
import io.github.sspanak.tt9.ime.helpers.TextField;
|
import io.github.sspanak.tt9.ime.helpers.TextField;
|
||||||
import io.github.sspanak.tt9.languages.Language;
|
import io.github.sspanak.tt9.languages.Language;
|
||||||
|
|
@ -9,11 +11,14 @@ import io.github.sspanak.tt9.util.Characters;
|
||||||
import io.github.sspanak.tt9.util.Text;
|
import io.github.sspanak.tt9.util.Text;
|
||||||
|
|
||||||
public class AutoSpace {
|
public class AutoSpace {
|
||||||
private final SettingsStore settings;
|
private static final Set<Character> PRECEDING_SPACE_PUNCTUATION = Set.of('(', '«', '„');
|
||||||
|
private static final Set<Character> PRECEDING_SPACE_FRENCH_PUNCTUATION = Set.of(';', ':', '!', '?', '»');
|
||||||
|
private static final Set<Character> TRAILING_SPACE_PUNCTUATION = Set.of('.', ',', ';', '!', '?', ')', '%', '»', '؟', '“');
|
||||||
|
|
||||||
private InputType inputType;
|
private static final Set<Character> NO_PRECEDING_SPACE_PUNCTUATION = Set.of('.', ',', ')', '\'', '@', '“', '؟');
|
||||||
private TextField textField;
|
private static final Set<Character> NOT_FRENCH_NO_PRECEDING_SPACE_PUNCTUATION = Set.of(';', ':', '!', '?', '»');
|
||||||
private String lastWord;
|
|
||||||
|
private final SettingsStore settings;
|
||||||
|
|
||||||
private boolean isLanguageFrench;
|
private boolean isLanguageFrench;
|
||||||
private boolean isLanguageWithSpaceBetweenWords;
|
private boolean isLanguageWithSpaceBetweenWords;
|
||||||
|
|
@ -26,41 +31,19 @@ public class AutoSpace {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public AutoSpace setInputType(InputType inputType) {
|
|
||||||
this.inputType = inputType;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public AutoSpace setTextField(TextField textField) {
|
|
||||||
this.textField = textField;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public AutoSpace setLastWord(String lastWord) {
|
|
||||||
this.lastWord = lastWord;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public AutoSpace setLanguage(Language language) {
|
public AutoSpace setLanguage(Language language) {
|
||||||
isLanguageFrench = LanguageKind.isFrench(language);
|
isLanguageFrench = LanguageKind.isFrench(language);
|
||||||
isLanguageWithSpaceBetweenWords = language != null && language.hasSpaceBetweenWords();
|
isLanguageWithSpaceBetweenWords = language != null && language.hasSpaceBetweenWords();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AutoSpace setLastSequence() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines whether to automatically add a space at the end of a sentence or after accepting a
|
* Determines whether to automatically add a space at the end of a sentence or after accepting a
|
||||||
* suggestion. This allows faster typing, without pressing space. See the helper functions for
|
* suggestion. This allows faster typing, without pressing space. See the helper functions for
|
||||||
* the list of rules.
|
* the list of rules.
|
||||||
*/
|
*/
|
||||||
public boolean shouldAddTrailingSpace(boolean isWordAcceptedManually, int nextKey) {
|
public boolean shouldAddTrailingSpace(TextField textField, InputType inputType, boolean isWordAcceptedManually, int nextKey) {
|
||||||
if (!isLanguageWithSpaceBetweenWords) {
|
if (!isLanguageWithSpaceBetweenWords) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -84,12 +67,12 @@ public class AutoSpace {
|
||||||
* Determines the special French rules for space before punctuation, as well as some standard ones.
|
* Determines the special French rules for space before punctuation, as well as some standard ones.
|
||||||
* For example, should we transform "word?" to "word ?", or "something(" to "something ("
|
* For example, should we transform "word?" to "word ?", or "something(" to "something ("
|
||||||
*/
|
*/
|
||||||
public boolean shouldAddBeforePunctuation() {
|
public boolean shouldAddBeforePunctuation(InputType inputType, TextField textField) {
|
||||||
String previousChars = textField.getStringBeforeCursor(2);
|
String previousChars = textField.getStringBeforeCursor(2);
|
||||||
char penultimateChar = previousChars.length() < 2 ? 0 : previousChars.charAt(previousChars.length() - 2);
|
char penultimateChar = previousChars.length() < 2 ? 0 : previousChars.charAt(previousChars.length() - 2);
|
||||||
char previousChar = previousChars.isEmpty() ? 0 : previousChars.charAt(previousChars.length() - 1);
|
char previousChar = previousChars.isEmpty() ? 0 : previousChars.charAt(previousChars.length() - 1);
|
||||||
|
|
||||||
if (previousChar == '¡' || previousChar == '¿' && settings.getAutoSpace()) {
|
if (previousChar == '¡' || previousChar == '¿' && settings.getAutoSpace()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -99,18 +82,8 @@ public class AutoSpace {
|
||||||
&& !inputType.isSpecialized()
|
&& !inputType.isSpecialized()
|
||||||
&& Character.isAlphabetic(penultimateChar)
|
&& Character.isAlphabetic(penultimateChar)
|
||||||
&& (
|
&& (
|
||||||
previousChar == '('
|
PRECEDING_SPACE_PUNCTUATION.contains(previousChar)
|
||||||
|| previousChar == '['
|
|| (isLanguageFrench && PRECEDING_SPACE_FRENCH_PUNCTUATION.contains(previousChar))
|
||||||
|| previousChar == '«'
|
|
||||||
|| previousChar == '„'
|
|
||||||
|| isLanguageFrench && (
|
|
||||||
previousChar == ';'
|
|
||||||
|| previousChar == ':'
|
|
||||||
|| previousChar == '!'
|
|
||||||
|| previousChar == '?'
|
|
||||||
|| previousChar == ')'
|
|
||||||
|| previousChar == '»'
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,22 +102,12 @@ public class AutoSpace {
|
||||||
&& !Text.nextIsPunctuation(nextChars.toString())
|
&& !Text.nextIsPunctuation(nextChars.toString())
|
||||||
&& !nextChars.startsWithNumber()
|
&& !nextChars.startsWithNumber()
|
||||||
&& (
|
&& (
|
||||||
previousChar == '.'
|
TRAILING_SPACE_PUNCTUATION.contains(previousChar)
|
||||||
|| previousChar == ','
|
|
||||||
|| previousChar == ';'
|
|
||||||
|| (previousChar == ':' && !Character.isDigit(penultimateChar))
|
|| (previousChar == ':' && !Character.isDigit(penultimateChar))
|
||||||
|| previousChar == '!'
|
|
||||||
|| previousChar == '?'
|
|
||||||
|| previousChar == ')'
|
|
||||||
|| previousChar == ']'
|
|
||||||
|| previousChar == '%'
|
|
||||||
|| previousChar == '»'
|
|
||||||
|| previousChar == '؟'
|
|
||||||
|| previousChar == '“'
|
|
||||||
|| (isLanguageFrench && previousChar == '«')
|
|| (isLanguageFrench && previousChar == '«')
|
||||||
|| (penultimateChar == ' ' && previousChar == '-')
|
|| (penultimateChar == ' ' && previousChar == '-')
|
||||||
|| (penultimateChar == ' ' && previousChar == '/')
|
|| (penultimateChar == ' ' && previousChar == '/')
|
||||||
|| (Character.isDigit(penultimateChar) && Characters.Currency.contains(previousChar + ""))
|
|| (Character.isDigit(penultimateChar) && Characters.Currency.contains(String.valueOf(previousChar)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,22 +127,20 @@ public class AutoSpace {
|
||||||
/**
|
/**
|
||||||
* Determines whether to transform: "word ." to: "word."
|
* Determines whether to transform: "word ." to: "word."
|
||||||
*/
|
*/
|
||||||
public boolean shouldDeletePrecedingSpace() {
|
public boolean shouldDeletePrecedingSpace(InputType inputType, TextField textField) {
|
||||||
|
String previousChars = textField.getStringBeforeCursor(3);
|
||||||
|
char prePenultimateChar = previousChars.length() < 3 ? 0 : previousChars.charAt(previousChars.length() - 3);
|
||||||
|
char penultimateChar = previousChars.length() < 2 ? 0 : previousChars.charAt(previousChars.length() - 2);
|
||||||
|
char previousChar = previousChars.isEmpty() ? 0 : previousChars.charAt(previousChars.length() - 1);
|
||||||
|
|
||||||
return
|
return
|
||||||
isLanguageWithSpaceBetweenWords
|
isLanguageWithSpaceBetweenWords
|
||||||
&& settings.getAutoSpace()
|
&& settings.getAutoSpace()
|
||||||
|
&& !Character.isWhitespace(prePenultimateChar)
|
||||||
|
&& Character.isWhitespace(penultimateChar)
|
||||||
&& (
|
&& (
|
||||||
lastWord.equals(".")
|
NO_PRECEDING_SPACE_PUNCTUATION.contains(previousChar)
|
||||||
|| lastWord.equals(",")
|
|| (!isLanguageFrench && NOT_FRENCH_NO_PRECEDING_SPACE_PUNCTUATION.contains(previousChar))
|
||||||
|| (!isLanguageFrench && lastWord.equals(";"))
|
|
||||||
|| (!isLanguageFrench && lastWord.equals(":"))
|
|
||||||
|| (!isLanguageFrench && lastWord.equals("!"))
|
|
||||||
|| (!isLanguageFrench && lastWord.equals("?"))
|
|
||||||
|| lastWord.equals("؟")
|
|
||||||
|| (!isLanguageFrench && lastWord.equals(")"))
|
|
||||||
|| lastWord.equals("]")
|
|
||||||
|| lastWord.equals("'")
|
|
||||||
|| lastWord.equals("@")
|
|
||||||
)
|
)
|
||||||
&& !inputType.isSpecialized();
|
&& !inputType.isSpecialized();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue