Skip to content

Commit

Permalink
Highlight Kotlin label.
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Jul 9, 2018
1 parent 94bf5f4 commit c615532
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.izhangzhihao.rainbow.brackets.annotator

import com.github.izhangzhihao.rainbow.brackets.RainbowInfo
import com.github.izhangzhihao.rainbow.brackets.settings.RainbowSettings
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.lexer.KtTokens
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.ui.JBColor
import org.jetbrains.kotlin.psi.*
import java.awt.Font

Expand All @@ -18,22 +19,55 @@ import java.awt.Font
*/
class KotlinLabelAnnotator : Annotator {
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if (element is KtLabelReferenceExpression) {
var refElement = element.reference?.resolve()
if (refElement == null && (element.lastChild as? LeafPsiElement)?.elementType == KtTokens.AT) {
// FIXME 由于这个时候默认的着色器还未遍历到后面的元素,因此这里会着色失败。
refElement = element.parent?.parent?.lastChild?.lastChild
} else if (refElement != null && refElement is KtClass) {
refElement = refElement.lastChild
}
if (!RainbowSettings.instance.isRainbowifyKotlinLabel) {
return
}

refElement
?.let { RainbowInfo.RAINBOW_INFO_KEY[it] }
?.color
?.let {
holder.createInfoAnnotation(element, null)
.enforcedTextAttributes = TextAttributes(it, null, null, EffectType.BOXED, Font.PLAIN)
val target: PsiElement
var refElement: PsiElement?
when (element) {
is KtLabelReferenceExpression -> {
target = element
refElement = try {
element.reference?.resolve()
} catch (e: Throwable) {
null
}

refElement = when (refElement) {
is KtBlockExpression,
is KtFunctionLiteral -> refElement
is KtFunction -> refElement.lastChild.takeIf { it is KtBlockExpression }
is KtClass -> refElement.lastChild.takeIf { it is KtClassBody }
is KtCallExpression,
is KtLambdaExpression -> PsiTreeUtil.findChildOfType(refElement, KtFunctionLiteral::class.java)
else -> null
}
}
is KtLabeledExpression -> {
target = element.firstChild.firstChild.takeIf { it is KtLabelReferenceExpression } ?: return
refElement = element.lastChild.let {
when (it) {
is KtBlockExpression,
is KtFunctionLiteral -> it
is KtCallExpression,
is KtLambdaExpression -> PsiTreeUtil.findChildOfType(it, KtFunctionLiteral::class.java)
else -> null
}
} ?: return
}
else -> return
}

refElement
.let { RainbowInfo.RAINBOW_INFO_KEY[it]?.color ?: DEFAULT_LABEL_COLOR }
.let {
holder.createInfoAnnotation(target, null)
.enforcedTextAttributes = TextAttributes(it, null, null, EffectType.BOXED, Font.PLAIN)
}
}

companion object {
private val DEFAULT_LABEL_COLOR = JBColor(0x4a86e8, 0x467cda)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class RainbowComponent : ApplicationComponent {
TextAttributes(null, null, null, EffectType.BOXED, Font.BOLD))
settings.isRainbowifyKotlinFunctionLiteralBracesAndArrow = true
}

if (settings.isRainbowifyKotlinLabel) {
globalScheme.setAttributes(createTextAttributesKey("KOTLIN_LABEL"),
TextAttributes(null, null, null, EffectType.BOXED, Font.PLAIN))
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class RainbowSettings : PersistentStateComponent<RainbowSettings> {
var version = "Unknown"
var isRainbowifyHTMLInsideJS = false
var isRainbowifyKotlinFunctionLiteralBracesAndArrow = false
var isRainbowifyKotlinLabel = true

var lightRoundBracketsColors = arrayOf(
"0xE66A01",
Expand Down

0 comments on commit c615532

Please sign in to comment.