mirror of
https://github.com/chylex/IntelliJ-Rainbow-Brackets.git
synced 2025-01-30 22:46:00 +01:00
Compare commits
3 Commits
ad617451c7
...
5b5cf0aeb5
Author | SHA1 | Date | |
---|---|---|---|
5b5cf0aeb5 | |||
e2e50e15a9 | |||
072011caee |
@ -4,6 +4,9 @@ This is a fork of the [🌈Rainbow Brackets](https://github.com/izhangzhihao/int
|
|||||||
|
|
||||||
## Key Changes
|
## Key Changes
|
||||||
|
|
||||||
- Support for CLion and Rider
|
- Support for C# (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+
|
||||||
|
@ -9,7 +9,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "com.chylex.intellij.coloredbrackets"
|
group = "com.chylex.intellij.coloredbrackets"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
apply(plugin = "org.jetbrains.kotlin.jvm")
|
apply(plugin = "org.jetbrains.kotlin.jvm")
|
||||||
|
@ -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 = 1000
|
var bigFilesLinesThreshold = 100_000
|
||||||
|
|
||||||
var languageBlacklist: Set<String> = setOf("hocon", "mxml")
|
var languageBlacklist: Set<String> = setOf("hocon", "mxml")
|
||||||
|
|
||||||
|
@ -9,28 +9,31 @@ 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 settings = RainbowSettings.instance
|
val processor = processor!!
|
||||||
val processor = Processor(settings)
|
|
||||||
val matching = processor.filterPairs(type, element) ?: return
|
val matching = processor.filterPairs(type, element) ?: return
|
||||||
|
|
||||||
val pair =
|
val pair = when (matching.size) {
|
||||||
if (matching.size == 1) {
|
1 -> matching[0]
|
||||||
matching[0]
|
else -> matching.find { processor.isValidBracket(element, it) } ?: return
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
matching.find { processor.isValidBracket(element, it) }
|
|
||||||
} ?: return
|
|
||||||
|
|
||||||
val level = processor.getBracketLevel(element, pair)
|
val level = processor.getBracketLevel(element, pair)
|
||||||
if (settings.isDoNOTRainbowifyTheFirstLevel) {
|
if (processor.settings.isDoNOTRainbowifyTheFirstLevel) {
|
||||||
if (level >= 1) {
|
if (level >= 1) {
|
||||||
rainbowPairs(element, pair, level)
|
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) {
|
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(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)
|
fun getBracketLevel(element: LeafPsiElement, pair: BracePair): Int = iterateBracketParents(element.parent, pair, -1)
|
||||||
|
|
||||||
@ -59,17 +84,19 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
|
|||||||
|
|
||||||
var nextCount = count
|
var nextCount = count
|
||||||
if (!settings.cycleCountOnAllBrackets) {
|
if (!settings.cycleCountOnAllBrackets) {
|
||||||
if (element.haveBrackets(
|
if (element.hasBrackets(
|
||||||
{ it.elementType() == pair.leftBraceType },
|
pair,
|
||||||
{ it.elementType() == pair.rightBraceType })
|
{ it.elementType == pair.leftBraceType },
|
||||||
|
{ it.elementType == pair.rightBraceType })
|
||||||
) {
|
) {
|
||||||
nextCount++
|
nextCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (element.haveBrackets(
|
if (element.hasBrackets(
|
||||||
{ element.language.braceTypeSet.contains(it.elementType()) },
|
pair,
|
||||||
{ element.language.braceTypeSet.contains(it.elementType()) })
|
{ element.language.braceTypeSet.contains(it.elementType) },
|
||||||
|
{ element.language.braceTypeSet.contains(it.elementType) })
|
||||||
) {
|
) {
|
||||||
nextCount++
|
nextCount++
|
||||||
}
|
}
|
||||||
@ -78,49 +105,59 @@ class DefaultRainbowVisitor : RainbowHighlightVisitor() {
|
|||||||
return iterateBracketParents(element.parent, pair, nextCount)
|
return iterateBracketParents(element.parent, pair, nextCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun PsiElement.haveBrackets(
|
private inline fun PsiElement.hasBrackets(
|
||||||
checkLeft: (PsiElement) -> Boolean,
|
pair: BracePair,
|
||||||
checkRight: (PsiElement) -> Boolean,
|
checkLeft: (LeafPsiElement) -> Boolean,
|
||||||
|
checkRight: (LeafPsiElement) -> Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
if (this is LeafPsiElement) {
|
if (this is LeafPsiElement) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var findLeftBracket = false
|
val cacheKey = HasBracketsCacheKey(this, pair)
|
||||||
var findRightBracket = false
|
val cacheValue = hasBracketsCache.getByte(cacheKey)
|
||||||
var left: PsiElement? = firstChild
|
if (cacheValue != CACHE_MISS) {
|
||||||
var right: PsiElement? = lastChild
|
return cacheValue == 1.toByte()
|
||||||
while (left != right && (!findLeftBracket || !findRightBracket)) {
|
}
|
||||||
val needBreak = left == null || left.nextSibling == right
|
|
||||||
|
val hasBrackets = run {
|
||||||
|
var findLeftBracket = false
|
||||||
|
var findRightBracket = false
|
||||||
|
var left: PsiElement? = firstChild
|
||||||
|
var right: PsiElement? = lastChild
|
||||||
|
|
||||||
if (left is LeafPsiElement && checkLeft(left)) {
|
while (left != right && (!findLeftBracket || !findRightBracket)) {
|
||||||
findLeftBracket = true
|
val needBreak = left == null || left.nextSibling == right
|
||||||
}
|
|
||||||
else {
|
if (left is LeafPsiElement && checkLeft(left)) {
|
||||||
left = left?.nextSibling
|
findLeftBracket = true
|
||||||
}
|
}
|
||||||
if (right is LeafPsiElement && checkRight(right)) {
|
else {
|
||||||
findRightBracket = true
|
left = left?.nextSibling
|
||||||
}
|
}
|
||||||
else {
|
if (right is LeafPsiElement && checkRight(right)) {
|
||||||
right = right?.prevSibling
|
findRightBracket = true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
right = right?.prevSibling
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needBreak) {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (needBreak) {
|
// For https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/830
|
||||||
break
|
if (settings.doNOTRainbowifyTemplateString && left?.prevSibling?.textMatches("$") == true) {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
findLeftBracket && findRightBracket
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//For https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/830
|
hasBracketsCache.put(cacheKey, if (hasBrackets) 1.toByte() else 0.toByte())
|
||||||
if (settings.doNOTRainbowifyTemplateString) {
|
return hasBrackets
|
||||||
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 {
|
||||||
@ -160,24 +197,21 @@ 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() -> {
|
filterBraceType.isNullOrEmpty() -> null
|
||||||
null
|
|
||||||
}
|
|
||||||
// https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198
|
// https://github.com/izhangzhihao/intellij-rainbow-brackets/issues/198
|
||||||
element.javaClass.simpleName == "OCMacroForeignLeafElement" -> {
|
element.javaClass.simpleName == "OCMacroForeignLeafElement" -> null
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
settings.isDoNOTRainbowifyBracketsWithoutContent -> {
|
settings.isDoNOTRainbowifyBracketsWithoutContent -> filterBraceType
|
||||||
filterBraceType
|
.filterNot { it.leftBraceType == type && element.nextSibling?.elementType() == it.rightBraceType }
|
||||||
.filterNot { it.leftBraceType == type && element.nextSibling?.elementType() == it.rightBraceType }
|
.filterNot { it.rightBraceType == type && element.prevSibling?.elementType() == it.leftBraceType }
|
||||||
.filterNot { it.rightBraceType == type && element.prevSibling?.elementType() == it.leftBraceType }
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
else -> filterBraceType
|
||||||
filterBraceType
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun PsiElement.elementType(): IElementType? {
|
||||||
|
return (this as? LeafPsiElement)?.elementType
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,16 +8,26 @@
|
|||||||
<br><br>
|
<br><br>
|
||||||
<b>Key Changes</b>
|
<b>Key Changes</b>
|
||||||
<ul>
|
<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>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>Added support for CLion and Rider</li>
|
<li>Restored 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>
|
||||||
|
Loading…
Reference in New Issue
Block a user