Change dsl marker gutter icon to use different colors

#KT-27769 Fixed
This commit is contained in:
Pavel V. Talanov
2019-01-14 18:48:17 +01:00
parent 6b14271931
commit f1f2d5b4d3
2 changed files with 18 additions and 7 deletions
@@ -44,7 +44,7 @@ class DslHighlighterExtension : HighlighterExtension() {
fun externalKeyName(index: Int) = "KOTLIN_DSL_STYLE$index" fun externalKeyName(index: Int) = "KOTLIN_DSL_STYLE$index"
val descriptionsToStyles = (1..numStyles).associate { index -> val descriptionsToStyles = (1..numStyles).associate { index ->
"Dsl//${styleOptionDisplayName(index)}" to styles[index - 1] "Dsl//${styleOptionDisplayName(index)}" to styleById(index)
} }
fun styleOptionDisplayName(index: Int) = "Style$index" fun styleOptionDisplayName(index: Int) = "Style$index"
@@ -60,8 +60,10 @@ class DslHighlighterExtension : HighlighterExtension() {
}?.annotationClass ?: return null }?.annotationClass ?: return null
val styleId = styleIdByMarkerAnnotation(markerAnnotation) ?: return null val styleId = styleIdByMarkerAnnotation(markerAnnotation) ?: return null
return styles[styleId - 1] return styleById(styleId)
} }
fun styleById(styleId: Int): TextAttributesKey = styles[styleId - 1]
} }
} }
@@ -9,11 +9,13 @@ import com.intellij.application.options.colors.ColorAndFontOptions
import com.intellij.codeHighlighting.Pass import com.intellij.codeHighlighting.Pass
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler import com.intellij.codeInsight.daemon.GutterIconNavigationHandler
import com.intellij.codeInsight.daemon.LineMarkerInfo import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.icons.AllIcons
import com.intellij.ide.DataManager import com.intellij.ide.DataManager
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.markup.GutterIconRenderer import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.util.Function import com.intellij.util.Function
import com.intellij.util.ui.ColorsIcon
import com.intellij.util.ui.JBUI
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.idea.KotlinLanguage
@@ -21,6 +23,7 @@ import org.jetbrains.kotlin.idea.core.toDescriptor
import org.jetbrains.kotlin.idea.highlighter.dsl.DslHighlighterExtension import org.jetbrains.kotlin.idea.highlighter.dsl.DslHighlighterExtension
import org.jetbrains.kotlin.idea.highlighter.dsl.isDslHighlightingMarker import org.jetbrains.kotlin.idea.highlighter.dsl.isDslHighlightingMarker
import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtClass
import javax.swing.Icon
import javax.swing.JComponent import javax.swing.JComponent
private val navHandler = GutterIconNavigationHandler<PsiElement> { event, element -> private val navHandler = GutterIconNavigationHandler<PsiElement> { event, element ->
@@ -39,7 +42,7 @@ fun collectHighlightingColorsMarkers(
ktClass: KtClass, ktClass: KtClass,
result: MutableCollection<LineMarkerInfo<*>> result: MutableCollection<LineMarkerInfo<*>>
) { ) {
if (ktClass.styleIdForMarkerAnnotation() == null) return val styleId = ktClass.styleIdForMarkerAnnotation() ?: return
val anchor = ktClass.nameIdentifier ?: return val anchor = ktClass.nameIdentifier ?: return
@@ -47,7 +50,7 @@ fun collectHighlightingColorsMarkers(
LineMarkerInfo<PsiElement>( LineMarkerInfo<PsiElement>(
anchor, anchor,
anchor.textRange, anchor.textRange,
AllIcons.Gutter.Colors, createIcon(styleId),
Pass.LINE_MARKERS, Pass.LINE_MARKERS,
toolTipHandler, navHandler, toolTipHandler, navHandler,
GutterIconRenderer.Alignment.RIGHT GutterIconRenderer.Alignment.RIGHT
@@ -55,10 +58,16 @@ fun collectHighlightingColorsMarkers(
) )
} }
private fun createIcon(styleId: Int): Icon {
val globalScheme = EditorColorsManager.getInstance().globalScheme
val markersColor = globalScheme.getAttributes(DslHighlighterExtension.styleById(styleId)).foregroundColor
val defaultColor = globalScheme.defaultForeground
return JBUI.scale(ColorsIcon(12, markersColor, defaultColor, defaultColor, markersColor))
}
private fun KtClass.styleIdForMarkerAnnotation(): Int? { private fun KtClass.styleIdForMarkerAnnotation(): Int? {
val classDescriptor = toDescriptor() as? ClassDescriptor ?: return null val classDescriptor = toDescriptor() as? ClassDescriptor ?: return null
if (classDescriptor.kind != ClassKind.ANNOTATION_CLASS) return null if (classDescriptor.kind != ClassKind.ANNOTATION_CLASS) return null
if (!classDescriptor.isDslHighlightingMarker()) return null if (!classDescriptor.isDslHighlightingMarker()) return null
return DslHighlighterExtension.styleIdByMarkerAnnotation(classDescriptor) return DslHighlighterExtension.styleIdByMarkerAnnotation(classDescriptor)
} }