[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance when using BracePair #48

Merged
merged 5 commits into from
May 2, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do NOT check brackets is valid or not
  • Loading branch information
izhangzhihao committed May 1, 2018
commit 147cf2966689c9cb4eb31602a6befb0153e20630
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {

val matching = filterPairs(type, element) ?: return

val pair = matching.takeIf { element.isValidBracket(it) } ?: return
val level = element.getBracketLevel(pair)
val level = element.getBracketLevel(matching)
if (level >= 0) {
element.setHighlightInfo(level)
}
Expand Down Expand Up @@ -84,36 +83,6 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
return (this as? LeafPsiElement)?.elementType
}

private fun LeafPsiElement.isValidBracket(pair: BracePair): Boolean {
val pairType = when (elementType) {
pair.leftBraceType -> pair.rightBraceType
pair.rightBraceType -> pair.leftBraceType
else -> return false
}

return if (pairType == pair.leftBraceType) {
checkBracePair(this, parent.firstChild, pairType, PsiElement::getNextSibling)
} else {
checkBracePair(this, parent.lastChild, pairType, PsiElement::getPrevSibling)
}
}

private fun checkBracePair(brace: PsiElement,
start: PsiElement,
type: IElementType,
next: PsiElement.() -> PsiElement?): Boolean {
var element: PsiElement? = start
while (element != null && element != brace) {
if (element is LeafPsiElement && element.elementType == type) {
return true
}

element = element.next()
}

return false
}

private fun filterPairs(type: IElementType, element: LeafPsiElement): BracePair? {
val pairs = element.language.bracePairs ?: return null
val filterBraceType = pairs[type.toString()]
Expand Down