1
0
mirror of https://github.com/chylex/IntelliJ-Rainbow-Brackets.git synced 2025-05-10 18:34:05 +02:00

Initial commit for rainbowify Python keywords

This commit is contained in:
张志豪 2021-08-22 17:35:15 +08:00
parent c9887658b9
commit b2edd310ff
4 changed files with 106 additions and 20 deletions
build.gradle.kts
src/main
kotlin/com/github/izhangzhihao/rainbow/brackets/visitor
resources/META-INF

View File

@ -35,25 +35,26 @@ intellij {
updateSinceUntilBuild.set(false)
plugins.set(
listOf(
"java",
"java-i18n",
"JavaScriptLanguage",
"DatabaseTools",
"CSS",
"platform-images",
"Groovy",
"properties",
"yaml",
"org.jetbrains.kotlin:203-$kotlinVersion-release-IJ5981.133-1",
"org.intellij.scala:2020.3.21",
"Dart:203.5981.155",
"org.jetbrains.plugins.ruby:203.5981.155",
"com.jetbrains.php:203.5981.155",
"com.jetbrains.sh:203.5981.37",
"com.jetbrains.plugins.jade:203.5981.155",
"org.jetbrains.plugins.go-template:203.5981.155"
)
listOf(
"java",
"java-i18n",
"JavaScriptLanguage",
"DatabaseTools",
"CSS",
"platform-images",
"Groovy",
"properties",
"yaml",
"org.jetbrains.kotlin:203-$kotlinVersion-release-IJ5981.133-1",
"org.intellij.scala:2020.3.21",
"Dart:203.5981.155",
"org.jetbrains.plugins.ruby:203.5981.155",
"com.jetbrains.php:203.5981.155",
"com.jetbrains.sh:203.5981.37",
"com.jetbrains.plugins.jade:203.5981.155",
"org.jetbrains.plugins.go-template:203.5981.155",
"Pythonid:203.5981.155",
)
)
}

View File

@ -0,0 +1,79 @@
package com.github.izhangzhihao.rainbow.brackets.visitor
import com.github.izhangzhihao.rainbow.brackets.settings.RainbowSettings
import com.intellij.codeInsight.daemon.impl.HighlightVisitor
import com.intellij.lang.BracePair
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.jetbrains.python.PyTokenTypes.*
import com.jetbrains.python.psi.PyStatement
class PythonRainbowVisitor : RainbowHighlightVisitor() {
override fun suitableForFile(file: PsiFile)
: Boolean = super.suitableForFile(file) &&
RainbowSettings.instance.isEnableRainbowAngleBrackets &&
(file.language.id == "Python" ||
file.viewProvider.allFiles.any { it.language.id == "Python" }
)
override fun clone(): HighlightVisitor = PythonRainbowVisitor()
override fun visit(element: PsiElement) {
val type = (element as? LeafPsiElement)?.elementType ?: return
val pair = statementKeywords[type]
if (pair != null) {
val level = element.getBracketLevel(pair)
if (RainbowSettings.instance.isDoNOTRainbowifyTheFirstLevel) {
if (level >= 1) {
rainbowPairs(element, pair, level)
}
} else {
if (level >= 0) {
rainbowPairs(element, pair, level)
}
}
}
}
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)
}
companion object {
val statementKeywords = mapOf(
IF_KEYWORD to BracePair(IF_KEYWORD, ELSE_KEYWORD, true),
ELSE_KEYWORD to BracePair(IF_KEYWORD, ELSE_KEYWORD, true),
ELIF_KEYWORD to BracePair(ELIF_KEYWORD, ELSE_KEYWORD, true),
)
private fun LeafPsiElement.getBracketLevel(pair: BracePair): Int =
iterateBracketParents(this, pair, -1)
private tailrec fun iterateBracketParents(
element: PsiElement?,
pair: BracePair,
count: Int
): Int {
if (element == null || element is PsiFile) {
return count
}
var nextCount = count
if (element is PyStatement) {
nextCount++
}
return iterateBracketParents(
element.parent,
pair,
nextCount,
)
}
}
}

View File

@ -686,7 +686,7 @@
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Build+Number+Ranges for description -->
<idea-version since-build="203"/>
<!-- please see http://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products
<!-- please see https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html
on how to target different products -->
<depends>com.intellij.modules.lang</depends>
<depends optional="true" config-file="kotlin-brackets.xml">org.jetbrains.kotlin</depends>
@ -702,6 +702,7 @@
<depends optional="true" config-file="php-brackets.xml">com.jetbrains.php</depends>
<depends optional="true" config-file="go-template-brackets.xml">org.jetbrains.plugins.go-template</depends>
<depends optional="true" config-file="jade-rainbow-visitor.xml">com.jetbrains.plugins.jade</depends>
<depends optional="true" config-file="python-brackets.xml">com.intellij.modules.python</depends>
<extensionPoints>
<extensionPoint name="bracePairProvider" beanClass="com.intellij.lang.LanguageExtensionPoint" dynamic="true">

View File

@ -0,0 +1,5 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<highlightVisitor implementation="com.github.izhangzhihao.rainbow.brackets.visitor.PythonRainbowVisitor"/>
</extensions>
</idea-plugin>