[Parameter Name Hints] allow to use *smth* (contains "smth") syntax for blacklist setup

This commit is contained in:
Yaroslav Lepenkin
2016-10-18 17:12:36 +03:00
parent 30a1c18fa9
commit d1395fb382
2 changed files with 25 additions and 11 deletions
@@ -27,29 +27,36 @@ object StringMatcherBuilder {
fun create(matcher: String): StringMatcher? {
if (matcher.isEmpty()) return StringMatcherImpl { true }
val asterisksCount = matcher.count { it == '*' }
if (asterisksCount > 1) return null
if (asterisksCount == 1) return createAsterisksMatcher(matcher)
return StringMatcherImpl { it == matcher }
return createAsterisksMatcher(matcher)
}
private fun createAsterisksMatcher(matcher: String): StringMatcher? {
val asterisksCount = matcher.count { it == '*' }
if (asterisksCount > 2) return null
if (asterisksCount == 0) {
return StringMatcherImpl { it == matcher }
}
if (matcher == "*") {
return StringMatcherImpl { true }
}
if (matcher.startsWith('*')) {
if (matcher.startsWith('*') && asterisksCount == 1) {
val target = matcher.substring(1)
return StringMatcherImpl { it.endsWith(target) }
}
if (matcher.endsWith('*')) {
if (matcher.endsWith('*') && asterisksCount == 1) {
val target = matcher.substring(0, matcher.length - 1)
return StringMatcherImpl { it.startsWith(target) }
}
if (matcher.startsWith('*') && matcher.endsWith('*')) {
val target = matcher.substring(1, matcher.length - 1)
return StringMatcherImpl { it.contains(target) }
}
return null
}
@@ -29,24 +29,31 @@ class StringMatchingTest : TestCase() {
}
fun `test simple`() {
val matcher = com.intellij.codeInsight.hints.filtering.StringMatcherBuilder.create("aaa")!!
val matcher = StringMatcherBuilder.create("aaa")!!
matcher.assertMatches("aaa")
matcher.assertNotMatches("aaaa", "aab", "", "*", "a", "baaa")
}
fun `test asterisks before`() {
val matcher = com.intellij.codeInsight.hints.filtering.StringMatcherBuilder.create("aaa*")!!
val matcher = StringMatcherBuilder.create("aaa*")!!
matcher.assertMatches("aaa", "aaaa", "aaaaaa", "aaaqwe")
matcher.assertNotMatches("baaa", "nnaaa", "qweaaa")
}
fun `test asterisks after`() {
val matcher = com.intellij.codeInsight.hints.filtering.StringMatcherBuilder.create("*aaa")!!
val matcher = StringMatcherBuilder.create("*aaa")!!
matcher.assertMatches("aaa", "aaaa", "baaa", "aawweraaa")
matcher.assertNotMatches("aaab", "aaabaa")
}
fun `test multiple asterisks`() {
val matcher = StringMatcherBuilder.create("*aax*")!!
matcher.assertMatches("qaaxq", "qqaaxqqq")
matcher.assertNotMatches("ax", "axx")
}
}