Skip to content

Commit

Permalink
catch regex exception (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
MolchanovDmitry authored Sep 17, 2022
1 parent 411542a commit 0106069
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'dmitriy.molchanov'
version '1.4'
version '1.4.1'

repositories {
mavenCentral()
Expand All @@ -27,6 +27,10 @@ intellij {
}
patchPluginXml {
changeNotes """
<b>1.4.1</b><br>
<ul>
<li>Regex bugfix.</li>
</ul>
<b>1.4</b><br>
<ul>
<li>Added the ability to add additional text around the result regex value.</li>
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/dmitriy/molchanov/ui/add/AddRuleDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ class AddRuleDialog(
shouldOkButtonActive = false
) to null
}
val regex = Regex(prefixEdit.text)
val regex = prefixEdit.text.getRegexOrNull()
val checkStr = checkStringEdit.text
regex.find(checkStr)?.range?.apply {
regex?.find(checkStr)?.range?.apply {
val lastMatch = if (last == checkStr.length) last else last + 1
val startText = checkStr.substring(0, first)
val matchText = checkStr.substring(first, lastMatch)
Expand All @@ -187,7 +187,7 @@ class AddRuleDialog(
private fun String.formatByParams(): String {
val prefix = startWithEdit.text
val suffix = endWithEdit.text
val registeredValue = when(shouldRuleBuUpperCase()){
val registeredValue = when (shouldRuleBuUpperCase()) {
true -> uppercase()
false -> lowercase()
else -> this
Expand All @@ -213,6 +213,13 @@ class AddRuleDialog(
else -> Strings.REGISTER_NONE
}

private fun String.getRegexOrNull(): Regex? =
try {
Regex(this)
} catch (throwable: Throwable) {
null
}

private class DialogStatus(val message: String, val shouldOkButtonActive: Boolean)

private companion object {
Expand Down

0 comments on commit 0106069

Please sign in to comment.