1
0
mirror of https://github.com/chylex/IntelliJ-Rainbow-Brackets.git synced 2025-01-31 07:46:00 +01:00

Compare commits

..

No commits in common. "5b5cf0aeb54a75b2e2069debf9fdb8c66a24cb20" and "ad617451c7288f9226ef666228499d13b322dadc" have entirely different histories.

5 changed files with 68 additions and 115 deletions

View File

@ -4,9 +4,6 @@ This is a fork of the [🌈Rainbow Brackets](https://github.com/izhangzhihao/int
## Key Changes ## Key Changes
- Support for C# (Rider) - Support for CLion and Rider
- Support for C++ (Rider, CLion, CLion Nova)
- Support for Settings Sync - Support for Settings Sync
- Improved highlighting performance
- Increased default setting for maximum line count from 1K to 100K
- Fixed service initialization warnings reported by 2024.2+ - Fixed service initialization warnings reported by 2024.2+

View File

@ -9,7 +9,7 @@ plugins {
} }
group = "com.chylex.intellij.coloredbrackets" group = "com.chylex.intellij.coloredbrackets"
version = "1.1.0" version = "1.0.0"
allprojects { allprojects {
apply(plugin = "org.jetbrains.kotlin.jvm") apply(plugin = "org.jetbrains.kotlin.jvm")

View File

@ -38,7 +38,7 @@ class RainbowSettings : PersistentStateComponent<RainbowSettings> {
var rainbowifyTagNameInXML = false var rainbowifyTagNameInXML = false
var doNOTRainbowifyTemplateString = false var doNOTRainbowifyTemplateString = false
var doNOTRainbowifyBigFiles = true var doNOTRainbowifyBigFiles = true
var bigFilesLinesThreshold = 100_000 var bigFilesLinesThreshold = 1000
var languageBlacklist: Set<String> = setOf("hocon", "mxml") var languageBlacklist: Set<String> = setOf("hocon", "mxml")

View File

@ -9,31 +9,28 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.psi.impl.source.tree.LeafPsiElement import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap
class DefaultRainbowVisitor : RainbowHighlightVisitor() { class DefaultRainbowVisitor : RainbowHighlightVisitor() {
override fun clone(): HighlightVisitor = DefaultRainbowVisitor() override fun clone(): HighlightVisitor = DefaultRainbowVisitor()
private var processor: Processor? = null
override fun onBeforeAnalyze(file: PsiFile, updateWholeFile: Boolean) {
processor = Processor(RainbowSettings.instance)
}
override fun visit(element: PsiElement) { override fun visit(element: PsiElement) {
val type = (element as? LeafPsiElement)?.elementType ?: return val type = (element as? LeafPsiElement)?.elementType ?: return
val processor = processor!! val settings = RainbowSettings.instance
val processor = Processor(settings)
val matching = processor.filterPairs(type, element) ?: return val matching = processor.filterPairs(type, element) ?: return
val pair = when (matching.size) { val pair =
1 -> matching[0] if (matching.size == 1) {
else -> matching.find { processor.isValidBracket(element, it) } ?: return matching[0]
} }
else {
matching.find { processor.isValidBracket(element, it) }
} ?: return
val level = processor.getBracketLevel(element, pair) val level = processor.getBracketLevel(element, pair)
if (processor.settings.isDoNOTRainbowifyTheFirstLevel) { if (settings.isDoNOTRainbowifyTheFirstLevel) {
if (level >= 1) { if (level >= 1) {
rainbowPairs(element, pair, level) rainbowPairs(element, pair, level)
} }
@ -45,35 +42,13 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
} }
} }
override fun onAfterAnalyze() {
processor = null
}
private fun rainbowPairs(element: LeafPsiElement, pair: BracePair, level: Int) { private fun rainbowPairs(element: LeafPsiElement, pair: BracePair, level: Int) {
val startElement = element.takeIf { it.elementType == pair.leftBraceType } val startElement = element.takeIf { it.elementType == pair.leftBraceType }
val endElement = element.takeIf { it.elementType == pair.rightBraceType } val endElement = element.takeIf { it.elementType == pair.rightBraceType }
element.setHighlightInfo(element.parent, level, startElement, endElement) element.setHighlightInfo(element.parent, level, startElement, endElement)
} }
private class Processor(val settings: RainbowSettings) { private class Processor(private val settings: RainbowSettings) {
private companion object {
private const val CACHE_MISS = (-1).toByte()
}
private data class HasBracketsCacheKey(val element: PsiElement, val pair: BracePair) {
private val hashCode = (31 * System.identityHashCode(element)) + System.identityHashCode(pair)
override fun equals(other: Any?): Boolean {
return other is HasBracketsCacheKey && element === other.element && pair === other.pair
}
override fun hashCode(): Int {
return hashCode
}
}
private val hasBracketsCache = Object2ByteOpenHashMap<HasBracketsCacheKey>().apply { defaultReturnValue(CACHE_MISS) }
fun getBracketLevel(element: LeafPsiElement, pair: BracePair): Int = iterateBracketParents(element.parent, pair, -1) fun getBracketLevel(element: LeafPsiElement, pair: BracePair): Int = iterateBracketParents(element.parent, pair, -1)
@ -84,19 +59,17 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
var nextCount = count var nextCount = count
if (!settings.cycleCountOnAllBrackets) { if (!settings.cycleCountOnAllBrackets) {
if (element.hasBrackets( if (element.haveBrackets(
pair, { it.elementType() == pair.leftBraceType },
{ it.elementType == pair.leftBraceType }, { it.elementType() == pair.rightBraceType })
{ it.elementType == pair.rightBraceType })
) { ) {
nextCount++ nextCount++
} }
} }
else { else {
if (element.hasBrackets( if (element.haveBrackets(
pair, { element.language.braceTypeSet.contains(it.elementType()) },
{ element.language.braceTypeSet.contains(it.elementType) }, { element.language.braceTypeSet.contains(it.elementType()) })
{ element.language.braceTypeSet.contains(it.elementType) })
) { ) {
nextCount++ nextCount++
} }
@ -105,59 +78,49 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
return iterateBracketParents(element.parent, pair, nextCount) return iterateBracketParents(element.parent, pair, nextCount)
} }
private inline fun PsiElement.hasBrackets( private inline fun PsiElement.haveBrackets(
pair: BracePair, checkLeft: (PsiElement) -> Boolean,
checkLeft: (LeafPsiElement) -> Boolean, checkRight: (PsiElement) -> Boolean,
checkRight: (LeafPsiElement) -> Boolean,
): Boolean { ): Boolean {
if (this is LeafPsiElement) { if (this is LeafPsiElement) {
return false return false
} }
val cacheKey = HasBracketsCacheKey(this, pair) var findLeftBracket = false
val cacheValue = hasBracketsCache.getByte(cacheKey) var findRightBracket = false
if (cacheValue != CACHE_MISS) { var left: PsiElement? = firstChild
return cacheValue == 1.toByte() var right: PsiElement? = lastChild
} while (left != right && (!findLeftBracket || !findRightBracket)) {
val needBreak = left == null || left.nextSibling == right
val hasBrackets = run { if (left is LeafPsiElement && checkLeft(left)) {
var findLeftBracket = false findLeftBracket = true
var findRightBracket = false
var left: PsiElement? = firstChild
var right: PsiElement? = lastChild
while (left != right && (!findLeftBracket || !findRightBracket)) {
val needBreak = left == null || left.nextSibling == right
if (left is LeafPsiElement && checkLeft(left)) {
findLeftBracket = true
}
else {
left = left?.nextSibling
}
if (right is LeafPsiElement && checkRight(right)) {
findRightBracket = true
}
else {
right = right?.prevSibling
}
if (needBreak) {
break
}
}
// For https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/830
if (settings.doNOTRainbowifyTemplateString && left?.prevSibling?.textMatches("$") == true) {
false
} }
else { else {
findLeftBracket && findRightBracket left = left?.nextSibling
}
if (right is LeafPsiElement && checkRight(right)) {
findRightBracket = true
}
else {
right = right?.prevSibling
}
if (needBreak) {
break
} }
} }
hasBracketsCache.put(cacheKey, if (hasBrackets) 1.toByte() else 0.toByte()) //For https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/830
return hasBrackets if (settings.doNOTRainbowifyTemplateString) {
if (left?.prevSibling?.textMatches("$") == true) return false
}
return findLeftBracket && findRightBracket
}
private fun PsiElement.elementType(): IElementType? {
return (this as? LeafPsiElement)?.elementType
} }
fun isValidBracket(element: LeafPsiElement, pair: BracePair): Boolean { fun isValidBracket(element: LeafPsiElement, pair: BracePair): Boolean {
@ -197,21 +160,24 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
val pairs = element.language.bracePairs ?: return null val pairs = element.language.bracePairs ?: return null
val filterBraceType = pairs[type.toString()] val filterBraceType = pairs[type.toString()]
return when { return when {
filterBraceType.isNullOrEmpty() -> null filterBraceType.isNullOrEmpty() -> {
null
}
// https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198 // https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198
element.javaClass.simpleName == "OCMacroForeignLeafElement" -> null element.javaClass.simpleName == "OCMacroForeignLeafElement" -> {
null
}
settings.isDoNOTRainbowifyBracketsWithoutContent -> filterBraceType settings.isDoNOTRainbowifyBracketsWithoutContent -> {
.filterNot { it.leftBraceType == type && element.nextSibling?.elementType() == it.rightBraceType } filterBraceType
.filterNot { it.rightBraceType == type && element.prevSibling?.elementType() == it.leftBraceType } .filterNot { it.leftBraceType == type && element.nextSibling?.elementType() == it.rightBraceType }
.filterNot { it.rightBraceType == type && element.prevSibling?.elementType() == it.leftBraceType }
}
else -> filterBraceType else -> {
filterBraceType
}
} }
} }
private fun PsiElement.elementType(): IElementType? {
return (this as? LeafPsiElement)?.elementType
}
} }
} }

View File

@ -8,26 +8,16 @@
<br><br> <br><br>
<b>Key Changes</b> <b>Key Changes</b>
<ul> <ul>
<li>Support for C# (Rider)</li> <li>Support for CLion and Rider</li>
<li>Support for C++ (Rider, CLion, CLion Nova)</li>
<li>Support for Settings Sync</li> <li>Support for Settings Sync</li>
<li>Improved highlighting performance</li>
<li>Increased default setting for maximum line count from 1K to 100K</li>
<li>Fixed service initialization warnings reported by 2024.2+</li> <li>Fixed service initialization warnings reported by 2024.2+</li>
</ul> </ul>
]]></description> ]]></description>
<change-notes><![CDATA[ <change-notes><![CDATA[
<b>1.1.0</b>
<ul>
<li>Added support for C++ in Rider and CLion Nova</li>
<li>Fixed broken option to not color parentheses without content in C#</li>
<li>Improved highlighting performance</li>
<li>Increased default setting for maximum line count from 1K to 100K</li>
</ul>
<b>1.0.0</b> <b>1.0.0</b>
<ul> <ul>
<li>Restored support for CLion and Rider</li> <li>Added support for CLion and Rider</li>
<li>Added support for Settings Sync</li> <li>Added support for Settings Sync</li>
<li>Fixed service initialization warnings reported by IJ 2024.2+</li> <li>Fixed service initialization warnings reported by IJ 2024.2+</li>
</ul> </ul>