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

Compare commits

...

3 Commits

Author SHA1 Message Date
5b5cf0aeb5
Release 1.1.0 2024-12-03 15:22:54 +01:00
e2e50e15a9
Increase default setting for maximum line count to 100K 2024-12-03 15:21:31 +01:00
072011caee
Improve highlighting performance 2024-12-03 15:14:10 +01:00
5 changed files with 115 additions and 68 deletions

View File

@ -4,6 +4,9 @@ This is a fork of the [🌈Rainbow Brackets](https://github.com/izhangzhihao/int
## Key Changes
- Support for CLion and Rider
- Support for C# (Rider)
- Support for C++ (Rider, CLion, CLion Nova)
- 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+

View File

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

View File

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

View File

@ -9,28 +9,31 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.tree.IElementType
import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap
class DefaultRainbowVisitor : RainbowHighlightVisitor() {
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) {
val type = (element as? LeafPsiElement)?.elementType ?: return
val settings = RainbowSettings.instance
val processor = Processor(settings)
val processor = processor!!
val matching = processor.filterPairs(type, element) ?: return
val pair =
if (matching.size == 1) {
matching[0]
val pair = when (matching.size) {
1 -> matching[0]
else -> matching.find { processor.isValidBracket(element, it) } ?: return
}
else {
matching.find { processor.isValidBracket(element, it) }
} ?: return
val level = processor.getBracketLevel(element, pair)
if (settings.isDoNOTRainbowifyTheFirstLevel) {
if (processor.settings.isDoNOTRainbowifyTheFirstLevel) {
if (level >= 1) {
rainbowPairs(element, pair, level)
}
@ -42,13 +45,35 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
}
}
override fun onAfterAnalyze() {
processor = null
}
private fun rainbowPairs(element: LeafPsiElement, pair: BracePair, level: Int) {
val startElement = element.takeIf { it.elementType == pair.leftBraceType }
val endElement = element.takeIf { it.elementType == pair.rightBraceType }
element.setHighlightInfo(element.parent, level, startElement, endElement)
}
private class Processor(private val settings: RainbowSettings) {
private class Processor(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)
@ -59,17 +84,19 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
var nextCount = count
if (!settings.cycleCountOnAllBrackets) {
if (element.haveBrackets(
{ it.elementType() == pair.leftBraceType },
{ it.elementType() == pair.rightBraceType })
if (element.hasBrackets(
pair,
{ it.elementType == pair.leftBraceType },
{ it.elementType == pair.rightBraceType })
) {
nextCount++
}
}
else {
if (element.haveBrackets(
{ element.language.braceTypeSet.contains(it.elementType()) },
{ element.language.braceTypeSet.contains(it.elementType()) })
if (element.hasBrackets(
pair,
{ element.language.braceTypeSet.contains(it.elementType) },
{ element.language.braceTypeSet.contains(it.elementType) })
) {
nextCount++
}
@ -78,18 +105,27 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
return iterateBracketParents(element.parent, pair, nextCount)
}
private inline fun PsiElement.haveBrackets(
checkLeft: (PsiElement) -> Boolean,
checkRight: (PsiElement) -> Boolean,
private inline fun PsiElement.hasBrackets(
pair: BracePair,
checkLeft: (LeafPsiElement) -> Boolean,
checkRight: (LeafPsiElement) -> Boolean,
): Boolean {
if (this is LeafPsiElement) {
return false
}
val cacheKey = HasBracketsCacheKey(this, pair)
val cacheValue = hasBracketsCache.getByte(cacheKey)
if (cacheValue != CACHE_MISS) {
return cacheValue == 1.toByte()
}
val hasBrackets = run {
var findLeftBracket = false
var findRightBracket = false
var left: PsiElement? = firstChild
var right: PsiElement? = lastChild
while (left != right && (!findLeftBracket || !findRightBracket)) {
val needBreak = left == null || left.nextSibling == right
@ -112,15 +148,16 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
}
// For https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/830
if (settings.doNOTRainbowifyTemplateString) {
if (left?.prevSibling?.textMatches("$") == true) return false
if (settings.doNOTRainbowifyTemplateString && left?.prevSibling?.textMatches("$") == true) {
false
}
else {
findLeftBracket && findRightBracket
}
}
return findLeftBracket && findRightBracket
}
private fun PsiElement.elementType(): IElementType? {
return (this as? LeafPsiElement)?.elementType
hasBracketsCache.put(cacheKey, if (hasBrackets) 1.toByte() else 0.toByte())
return hasBrackets
}
fun isValidBracket(element: LeafPsiElement, pair: BracePair): Boolean {
@ -160,24 +197,21 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
val pairs = element.language.bracePairs ?: return null
val filterBraceType = pairs[type.toString()]
return when {
filterBraceType.isNullOrEmpty() -> {
null
}
// https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198
element.javaClass.simpleName == "OCMacroForeignLeafElement" -> {
null
}
filterBraceType.isNullOrEmpty() -> null
settings.isDoNOTRainbowifyBracketsWithoutContent -> {
filterBraceType
// https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198
element.javaClass.simpleName == "OCMacroForeignLeafElement" -> null
settings.isDoNOTRainbowifyBracketsWithoutContent -> filterBraceType
.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,16 +8,26 @@
<br><br>
<b>Key Changes</b>
<ul>
<li>Support for CLion and Rider</li>
<li>Support for C# (Rider)</li>
<li>Support for C++ (Rider, CLion, CLion Nova)</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>
</ul>
]]></description>
<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>
<ul>
<li>Added support for CLion and Rider</li>
<li>Restored support for CLion and Rider</li>
<li>Added support for Settings Sync</li>
<li>Fixed service initialization warnings reported by IJ 2024.2+</li>
</ul>