[json schema] nested completions - properly handle the case with multiple child nodes (named + regex at once)

GitOrigin-RevId: 6a2b408f0d84612d6c1373d0f2740a8ec1d588df
This commit is contained in:
Anton Lobov
2024-09-23 11:21:59 +02:00
committed by intellij-monorepo-bot
parent 4690be09a5
commit a976ac8093

View File

@@ -80,7 +80,16 @@ private fun JsonPointerPosition.toPathItems() =
private tailrec fun NestedCompletionsNode.navigate(index: Int, steps: List<String>): NestedCompletionsNode? =
if (index !in steps.indices) this
else children.firstOrNull { it.matches(steps[index]) }?.node?.navigate(index + 1, steps)
else {
val matchingNodes = children.filter { it.matches(steps[index]) }
matchingNodes.getPreferredChild()?.node?.navigate(index + 1, steps)
}
// some schemas provide both named and regex nodes for the same name
// we need to prioritize named options over regex options
private fun Collection<ChildNode>.getPreferredChild(): ChildNode? {
return firstOrNull { it is ChildNode.NamedChildNode } ?: firstOrNull()
}
private fun ChildNode.matches(name: String): Boolean = when (this) {
is ChildNode.Isolated.RegexNode -> regex.matches(name)