From 1dea64b2ff102b6a7ef0524c307103d988badde3 Mon Sep 17 00:00:00 2001 From: AZADAYAZ <95386329+AZADAYAZ@users.noreply.github.com> Date: Sun, 26 Jul 2026 04:46:50 +0300 Subject: [PATCH 1/2] Update TextExpanderUtils.kt --- .../keyboard/latin/utils/TextExpanderUtils.kt | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt index f4164b8bb..1e0dd3624 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt @@ -32,8 +32,6 @@ object TextExpanderUtils { return context.prefs().getBoolean(PREF_BACKSPACE_REVERTS, false) } - - data class ShortcutEntry( val template: String, val prefix: String = "" @@ -220,43 +218,43 @@ object TextExpanderUtils { context: Context, ): Boolean = getShortcuts(context).any { (key, entry) -> - if (key.startsWith(REGEX_PREFIX) || key.length < entry.prefix.length) { + val cleanKey = if (key.startsWith(REGEX_PREFIX)) key.substring(REGEX_PREFIX.length) else key + if (key.startsWith(REGEX_PREFIX) || cleanKey.length < entry.prefix.length) { false } else { - val shortcut = key.substring(entry.prefix.length) + val shortcut = cleanKey.substring(entry.prefix.length) + val target = entry.prefix + word !shortcut.equals(word, ignoreCase = true) && shortcut.startsWith(word, ignoreCase = true) && - textBeforeCursor.endsWith(entry.prefix + word, ignoreCase = true) + textBeforeCursor.endsWith(target, ignoreCase = true) } } fun getExpandedWordForTyped(word: String?, textBeforeCursor: String?, context: Context): ExpandedResult? { - if (word == null || textBeforeCursor == null || !isEnabled(context)) return null + if (textBeforeCursor == null || !isEnabled(context)) return null val shortcuts = getShortcuts(context) for ((key, entry) in shortcuts) { val isRegex = key.startsWith(REGEX_PREFIX) val cleanKey = if (isRegex) key.substring(REGEX_PREFIX.length) else key - - if (isRegex) { - val prefix = entry.prefix - val patternStr = cleanKey - try { - val regex = Regex(patternStr, RegexOption.IGNORE_CASE) - val expectedSuffix = prefix + word - if (textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true)) { + val prefix = entry.prefix + val expectedSuffix = prefix + cleanKey + + val matchesWord = word != null && expectedSuffix.equals(prefix + word, ignoreCase = true) + val matchesDirectSuffix = textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true) + + if (matchesWord || matchesDirectSuffix) { + if (isRegex) { + try { + val regex = Regex(cleanKey, RegexOption.IGNORE_CASE) if (regex.matches(expectedSuffix)) { val replaced = regex.replace(expectedSuffix, entry.template) return ExpandedResult(expand(replaced, context), prefix.length, expectedSuffix) } + } catch (e: java.lang.Exception) { + // ignore } - } catch (e: java.lang.Exception) { - // ignore - } - } else { - val prefix = entry.prefix - val expectedSuffix = cleanKey - if (expectedSuffix.equals(prefix + word, ignoreCase = true)) { + } else { if (textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true)) { return ExpandedResult(expand(entry.template, context), prefix.length, expectedSuffix) } From 614bc13aa4f60240938089ec7bc074f94345eabe Mon Sep 17 00:00:00 2001 From: AZADAYAZ <95386329+AZADAYAZ@users.noreply.github.com> Date: Sun, 26 Jul 2026 15:48:29 +0300 Subject: [PATCH 2/2] Update TextExpanderUtils.kt --- .../keyboard/latin/utils/TextExpanderUtils.kt | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt index 1e0dd3624..6e0189468 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt @@ -6,7 +6,6 @@ package helium314.keyboard.latin.utils import android.content.ClipboardManager import android.content.Context -import android.content.res.ColorStateList import org.json.JSONObject import java.text.SimpleDateFormat import java.util.Date @@ -218,43 +217,44 @@ object TextExpanderUtils { context: Context, ): Boolean = getShortcuts(context).any { (key, entry) -> - val cleanKey = if (key.startsWith(REGEX_PREFIX)) key.substring(REGEX_PREFIX.length) else key - if (key.startsWith(REGEX_PREFIX) || cleanKey.length < entry.prefix.length) { + if (key.startsWith(REGEX_PREFIX) || key.length < entry.prefix.length) { false } else { - val shortcut = cleanKey.substring(entry.prefix.length) - val target = entry.prefix + word + val shortcut = key.substring(entry.prefix.length) !shortcut.equals(word, ignoreCase = true) && shortcut.startsWith(word, ignoreCase = true) && - textBeforeCursor.endsWith(target, ignoreCase = true) + textBeforeCursor.endsWith(entry.prefix + word, ignoreCase = true) } } fun getExpandedWordForTyped(word: String?, textBeforeCursor: String?, context: Context): ExpandedResult? { - if (textBeforeCursor == null || !isEnabled(context)) return null + if (word == null || textBeforeCursor == null || !isEnabled(context)) return null val shortcuts = getShortcuts(context) for ((key, entry) in shortcuts) { val isRegex = key.startsWith(REGEX_PREFIX) val cleanKey = if (isRegex) key.substring(REGEX_PREFIX.length) else key - val prefix = entry.prefix - val expectedSuffix = prefix + cleanKey - - val matchesWord = word != null && expectedSuffix.equals(prefix + word, ignoreCase = true) - val matchesDirectSuffix = textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true) - - if (matchesWord || matchesDirectSuffix) { - if (isRegex) { - try { - val regex = Regex(cleanKey, RegexOption.IGNORE_CASE) - if (regex.matches(expectedSuffix)) { - val replaced = regex.replace(expectedSuffix, entry.template) + + if (isRegex) { + val prefix = entry.prefix + val patternStr = cleanKey + try { + val regex = Regex(patternStr, RegexOption.IGNORE_CASE) + val expectedSuffix = prefix + word + if (textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true)) { + val matchInput = if (expectedSuffix.startsWith(prefix)) expectedSuffix.substring(prefix.length) else expectedSuffix + if (regex.matches(matchInput)) { + val replaced = regex.replace(matchInput, entry.template) return ExpandedResult(expand(replaced, context), prefix.length, expectedSuffix) } - } catch (e: java.lang.Exception) { - // ignore } - } else { + } catch (e: java.lang.Exception) { + // ignore + } + } else { + val prefix = entry.prefix + val expectedSuffix = cleanKey + if (expectedSuffix.equals(prefix + word, ignoreCase = true)) { if (textBeforeCursor.endsWith(expectedSuffix, ignoreCase = true)) { return ExpandedResult(expand(entry.template, context), prefix.length, expectedSuffix) } @@ -274,3 +274,4 @@ object TextExpanderUtils { return null } } +