-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type names filtering for integration classes
Fixes #363
- Loading branch information
Showing
17 changed files
with
352 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/util/acceptanceRules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.jetbrains.kotlinx.jupyter.util | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.jetbrains.kotlinx.jupyter.api.TypeName | ||
import org.jetbrains.kotlinx.jupyter.api.libraries.VariablesSubstitutionAware | ||
|
||
/** | ||
* Acceptance rule either says it accepts object or delegates it to some other rule returning null | ||
*/ | ||
fun interface AcceptanceRule<T> { | ||
fun accepts(obj: T): Boolean? | ||
} | ||
|
||
/** | ||
* Acceptance rule that has only two answers: yes/no (depending on the [acceptsFlag]) and "don't know" | ||
*/ | ||
interface FlagAcceptanceRule<T> : AcceptanceRule<T> { | ||
val acceptsFlag: Boolean | ||
fun appliesTo(obj: T): Boolean | ||
|
||
override fun accepts(obj: T): Boolean? { | ||
return if (appliesTo(obj)) acceptsFlag else null | ||
} | ||
} | ||
|
||
/** | ||
* Acceptance rule for type names | ||
*/ | ||
class NameAcceptanceRule( | ||
override val acceptsFlag: Boolean, | ||
private val appliesPredicate: (TypeName) -> Boolean | ||
) : FlagAcceptanceRule<TypeName> { | ||
override fun appliesTo(obj: TypeName): Boolean { | ||
return appliesPredicate(obj) | ||
} | ||
} | ||
|
||
/** | ||
* Acceptance rule for type names based on [pattern]. | ||
* Pattern may consist of any characters and of 3 special combinations: | ||
* 1) `?` - any single character or no character | ||
* 2) `*` - any character sequence excluding dot (`.`) | ||
* 3) `**` - any character sequence | ||
* | ||
* For example, pattern `org.jetbrains.kotlin?.**.jupyter.*` matches following names: | ||
* - `org.jetbrains.kotlin.my.package.jupyter.Integration` | ||
* - `org.jetbrains.kotlinx.some_package.jupyter.SomeClass` | ||
* | ||
* It doesn't match name `org.jetbrains.kotlin.my.package.jupyter.integration.MyClass` | ||
*/ | ||
@Serializable(PatternNameAcceptanceRuleSerializer::class) | ||
class PatternNameAcceptanceRule( | ||
override val acceptsFlag: Boolean, | ||
val pattern: String, | ||
) : FlagAcceptanceRule<TypeName>, VariablesSubstitutionAware<PatternNameAcceptanceRule> { | ||
private val regex by lazy { | ||
buildString { | ||
var i = 0 | ||
while (i < pattern.length) { | ||
val c = pattern[i] | ||
val nextC = pattern.getOrNull(i + 1) | ||
|
||
when (c) { | ||
'.' -> append("\\.") | ||
'*' -> when (nextC) { | ||
'*' -> { | ||
append(".*") | ||
++i | ||
} | ||
else -> append("[^.]*") | ||
} | ||
'?' -> append(".?") | ||
'[', ']', '(', ')', '{', '}', '\\', '$', '^', '+', '|' -> { | ||
append('\\') | ||
append(c) | ||
} | ||
else -> append(c) | ||
} | ||
++i | ||
} | ||
}.toRegex() | ||
} | ||
|
||
override fun appliesTo(obj: TypeName): Boolean { | ||
return regex.matches(obj) | ||
} | ||
|
||
override fun replaceVariables(mapping: Map<String, String>): PatternNameAcceptanceRule { | ||
val newPattern = replaceVariables(pattern, mapping) | ||
if (pattern == newPattern) return this | ||
return PatternNameAcceptanceRule(acceptsFlag, newPattern) | ||
} | ||
} | ||
|
||
/** | ||
* List of acceptance rules: | ||
* 1) accepts [obj] if latest not-null acceptance result is `true` | ||
* 2) doesn't accept [obj] if latest not-null acceptance result is `false` | ||
* 3) returns `null` if all acceptance results are `null` or the iterable is empty | ||
*/ | ||
fun <T> Iterable<AcceptanceRule<T>>.accepts(obj: T): Boolean? { | ||
return mapNotNull { it.accepts(obj) }.lastOrNull() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.