1
0
mirror of https://github.com/chylex/IntelliJ-Rainbow-Brackets.git synced 2025-04-11 03:15:49 +02:00

Fix Wrong coloring in lambda expressions with braces

(cherry picked from commit c5a2888173f9cd4403391f87afaf8729856c24e2)
This commit is contained in:
张志豪 2021-02-27 23:25:41 +08:00
parent fb88cdc4f1
commit f227fd8b2f

View File

@ -59,28 +59,67 @@ class CSharpRainbowVisitor : RainbowHighlightVisitor() {
private fun LeafPsiElement.getBracketLevel(pair: BracePair, type: IElementType): Int = iterateBracketParents(this, pair, -1, type)
private tailrec fun iterateBracketParents(element: PsiElement?, pair: BracePair, count: Int, type: IElementType): Int {
if (element == null || element is CSharpDummyNode || element is PsiFile) {
if (element == null || element is PsiFile) {
return count
}
var nextCount = count
if (element is LeafPsiElement && type == pair.leftBraceType && element.elementType == pair.rightBraceType) {
nextCount --
nextCount--
}
if (element is LeafPsiElement && type == pair.rightBraceType && element.elementType == pair.leftBraceType) {
nextCount --
nextCount--
}
if (element is LeafPsiElement && element.elementType == type) {
nextCount ++
nextCount++
}
return if (type == pair.leftBraceType) {
iterateBracketParents(element.prevSibling, pair, nextCount, type)
val prev = element.prevSibling
if (prev == null) {
iterateBracketParents(element.parent.prevSibling.iterForPreDummyNode()?.lastChild, pair, nextCount, type)
} else {
iterateBracketParents(prev, pair, nextCount, type)
}
} else {
iterateBracketParents(element.nextSibling, pair, nextCount, type)
val next = element.nextSibling
if (next == null) {
iterateBracketParents(element.parent.nextSibling.iterForNextDummyNode()?.firstChild, pair, nextCount, type)
} else {
iterateBracketParents(next, pair, nextCount, type)
}
}
}
private tailrec fun PsiElement?.iterForNextDummyNode(): PsiElement? {
return if (this == null) {
null
} else if (this is CSharpDummyNode) {
this
} else {
if (this.nextSibling == null) {
null
} else {
this.nextSibling.iterForNextDummyNode()
}
}
}
private tailrec fun PsiElement?.iterForPreDummyNode(): PsiElement? {
return if (this == null) {
null
} else if (this is CSharpDummyNode) {
this
} else {
if (this.prevSibling == null) {
null
} else {
this.prevSibling.iterForPreDummyNode()
}
}
}
}