Improve avoiding autocorrect w/ URLs/emails

This commit is contained in:
Aleksandras Kostarevas 2024-02-01 22:58:08 +02:00
parent c7113297fb
commit 6d5252e60a
4 changed files with 10 additions and 2 deletions

View File

@ -448,6 +448,10 @@ public final class StringUtils {
int codePoint = 0;
while (i > 0) {
codePoint = Character.codePointBefore(text, i);
if (Constants.CODE_COMMERCIAL_AT == codePoint) {
// If it's an email address, it's essentially a URL, we don't want to correct those
return true;
}
if (codePoint < Constants.CODE_PERIOD || codePoint > 'z') {
// Handwavy heuristic to see if that's a URL character. Anything between period
// and z. This includes all lower- and upper-case ascii letters, period,

View File

@ -222,7 +222,9 @@ public final class Suggest {
// If the first suggestion is a shortcut we never auto-correct to it, regardless
// of how strong it is (allowlist entries are not KIND_SHORTCUT but KIND_WHITELIST).
// TODO: we may want to have shortcut-only entries auto-correct in the future.
|| suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)) {
|| suggestionResults.first().isKindOf(SuggestedWordInfo.KIND_SHORTCUT)
// Don't do it if it looks like a URL (or email address)
|| StringUtils.lastPartLooksLikeURL(typedWordString)) {
hasAutoCorrection = false;
} else {
final SuggestedWordInfo firstSuggestion = suggestionResults.first();

View File

@ -60,7 +60,7 @@ public class SuggestedWords {
// Note: this INCLUDES cases where the word will auto-correct to itself. A good definition
// of what this flag means would be "the top suggestion is strong enough to auto-correct",
// whether this exactly matches the user entry or not.
public final boolean mWillAutoCorrect;
public boolean mWillAutoCorrect;
public final boolean mIsObsoleteSuggestions;
// How the input for these suggested words was done by the user. Must be one of the
// INPUT_STYLE_* constants above.

View File

@ -585,6 +585,8 @@ public final class InputLogic {
// Especially, how do we deal with InputMethodService.onDisplayCompletions?
public void setSuggestedWords(final SuggestedWords suggestedWords) {
if (!suggestedWords.isEmpty()) {
suggestedWords.mWillAutoCorrect = suggestedWords.mWillAutoCorrect
&& !mConnection.textBeforeCursorLooksLikeURL();
final SuggestedWordInfo suggestedWordInfo;
if (suggestedWords.mWillAutoCorrect) {
suggestedWordInfo = suggestedWords.getInfo(SuggestedWords.INDEX_OF_AUTO_CORRECTION);