1
0
Fork 0

fixed fast deleting deleting too many characters when there is a new line

This commit is contained in:
sspanak 2024-10-16 16:43:57 +03:00 committed by Dimo Karaivanov
parent 1e47bee232
commit d47528f550
2 changed files with 46 additions and 4 deletions

View file

@ -161,13 +161,13 @@ public class TextField extends InputField {
return 0; return 0;
} }
String before = getStringBeforeCursor(); Text before = getTextBeforeCursor();
if (before.isEmpty()) { if (before.isEmpty()) {
return 0; return 0;
} }
int spaceShift = Math.max(before.lastIndexOf(' '), 0); int whitespaceShift = Math.max(before.lastWhitespaceBlockIndex(), 0);
return Math.min(before.length() - spaceShift, (int) (SettingsStore.BACKSPACE_ACCELERATION_MAX_CHARS * 1.5)); return Math.min(before.length() - whitespaceShift, (int) (SettingsStore.BACKSPACE_ACCELERATION_MAX_CHARS * 1.5));
} }

View file

@ -11,16 +11,19 @@ public class Text extends TextTools {
private final Language language; private final Language language;
private final String text; private final String text;
public Text(Language language, String text) { public Text(Language language, String text) {
this.language = language; this.language = language;
this.text = text; this.text = text;
} }
public Text(String text) { public Text(String text) {
this.language = null; this.language = null;
this.text = text; this.text = text;
} }
public String capitalize() { public String capitalize() {
if (language == null || text == null || text.isEmpty() || !language.hasUpperCase()) { if (language == null || text == null || text.isEmpty() || !language.hasUpperCase()) {
return text; return text;
@ -32,11 +35,11 @@ public class Text extends TextTools {
} }
public boolean endsWithGraphic() { public boolean endsWithGraphic() {
return text != null && !text.isEmpty() && Characters.isGraphic(text.charAt(text.length() - 1)); return text != null && !text.isEmpty() && Characters.isGraphic(text.charAt(text.length() - 1));
} }
public int getTextCase() { public int getTextCase() {
if (isUpperCase()) { if (isUpperCase()) {
return InputMode.CASE_UPPER; return InputMode.CASE_UPPER;
@ -49,6 +52,7 @@ public class Text extends TextTools {
} }
} }
public boolean isAlphabetic() { public boolean isAlphabetic() {
for (int i = 0, end = text == null ? 0 : text.length(); i < end; i++) { for (int i = 0, end = text == null ? 0 : text.length(); i < end; i++) {
if (!Character.isAlphabetic(text.charAt(i))) { if (!Character.isAlphabetic(text.charAt(i))) {
@ -59,6 +63,7 @@ public class Text extends TextTools {
return true; return true;
} }
public boolean isNumeric() { public boolean isNumeric() {
if (text == null) { if (text == null) {
return false; return false;
@ -73,10 +78,12 @@ public class Text extends TextTools {
return true; return true;
} }
public boolean isEmpty() { public boolean isEmpty() {
return text == null || text.isEmpty(); return text == null || text.isEmpty();
} }
private boolean isCapitalized() { private boolean isCapitalized() {
if (text == null || text.length() < 2) { if (text == null || text.length() < 2) {
return false; return false;
@ -95,6 +102,7 @@ public class Text extends TextTools {
return true; return true;
} }
public boolean isMixedCase() { public boolean isMixedCase() {
return return
language != null language != null
@ -103,10 +111,12 @@ public class Text extends TextTools {
&& !text.toUpperCase(language.getLocale()).equals(text); && !text.toUpperCase(language.getLocale()).equals(text);
} }
public boolean isUpperCase() { public boolean isUpperCase() {
return language != null && text != null && text.toUpperCase(language.getLocale()).equals(text); return language != null && text != null && text.toUpperCase(language.getLocale()).equals(text);
} }
public String leaveEndingGraphics() { public String leaveEndingGraphics() {
if (text == null) { if (text == null) {
return ""; return "";
@ -127,6 +137,7 @@ public class Text extends TextTools {
return sb.toString(); return sb.toString();
} }
public String leaveStartingGraphics() { public String leaveStartingGraphics() {
if (text == null) { if (text == null) {
return ""; return "";
@ -148,22 +159,49 @@ public class Text extends TextTools {
} }
public int length() {
return text == null ? 0 : text.length();
}
public int lastWhitespaceBlockIndex() {
if (text == null) {
return -1;
}
for (int i = text.length() - 1; i >= 0; i--) {
if (
Character.isWhitespace(text.charAt(i))
&& (i == 0 || !Character.isWhitespace(text.charAt(i - 1)))
) {
return i;
}
}
return -1;
}
public boolean startsWithWhitespace() { public boolean startsWithWhitespace() {
return text != null && !text.isEmpty() && Character.isWhitespace(text.charAt(0)) && !text.startsWith("\n"); return text != null && !text.isEmpty() && Character.isWhitespace(text.charAt(0)) && !text.startsWith("\n");
} }
public boolean startsWithNumber() { public boolean startsWithNumber() {
return text != null && !text.isEmpty() && Character.isDigit(text.charAt(0)); return text != null && !text.isEmpty() && Character.isDigit(text.charAt(0));
} }
public boolean startsWithGraphic() { public boolean startsWithGraphic() {
return text != null && !text.isEmpty() && Characters.isGraphic(text.charAt(0)); return text != null && !text.isEmpty() && Characters.isGraphic(text.charAt(0));
} }
public boolean startsWithWord() { public boolean startsWithWord() {
return text != null && !text.isEmpty() && Character.isAlphabetic(text.charAt(0)); return text != null && !text.isEmpty() && Character.isAlphabetic(text.charAt(0));
} }
public String subStringEndingWord(boolean keepApostrophe, boolean keepQuote) { public String subStringEndingWord(boolean keepApostrophe, boolean keepQuote) {
if (text == null) { if (text == null) {
return ""; return "";
@ -184,6 +222,7 @@ public class Text extends TextTools {
return sub.toString(); return sub.toString();
} }
public String subStringStartingWord(boolean keepApostrophe, boolean keepQuote) { public String subStringStartingWord(boolean keepApostrophe, boolean keepQuote) {
if (text == null) { if (text == null) {
return ""; return "";
@ -204,6 +243,7 @@ public class Text extends TextTools {
return sub.toString(); return sub.toString();
} }
public String toLowerCase() { public String toLowerCase() {
if (text == null) { if (text == null) {
return ""; return "";
@ -212,6 +252,7 @@ public class Text extends TextTools {
} }
} }
public String toUpperCase() { public String toUpperCase() {
if (text == null) { if (text == null) {
return ""; return "";
@ -220,6 +261,7 @@ public class Text extends TextTools {
} }
} }
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {