Skip to content

Commit 4ca6215

Browse files
authored
Port some features shared by all frameworks to UAST (#811)
Includes colors, plugins and listeners gutter marks
1 parent 2beb908 commit 4ca6215

19 files changed

+294
-290
lines changed

src/main/kotlin/insight/ColorAnnotator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import com.intellij.openapi.editor.markup.TextAttributes
1818
import com.intellij.psi.PsiElement
1919
import java.awt.Color
2020
import java.awt.Font
21+
import org.jetbrains.uast.UIdentifier
22+
import org.jetbrains.uast.toUElementOfType
2123

2224
class ColorAnnotator : Annotator {
2325

@@ -26,7 +28,7 @@ class ColorAnnotator : Annotator {
2628
return
2729
}
2830

29-
val color = element.findColor { _, chosenEntry -> chosenEntry.value } ?: return
31+
val color = element.toUElementOfType<UIdentifier>()?.findColor { _, chosenEntry -> chosenEntry.value } ?: return
3032

3133
setColorAnnotator(color, element, holder)
3234
}

src/main/kotlin/insight/ColorLineMarkerProvider.kt

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@ import com.intellij.codeInsight.daemon.LineMarkerInfo
1616
import com.intellij.codeInsight.daemon.LineMarkerProvider
1717
import com.intellij.codeInsight.daemon.MergeableLineMarkerInfo
1818
import com.intellij.codeInsight.daemon.NavigateAction
19+
import com.intellij.codeInsight.hint.HintManager
1920
import com.intellij.icons.AllIcons
2021
import com.intellij.openapi.editor.markup.GutterIconRenderer
22+
import com.intellij.psi.JVMElementFactories
2123
import com.intellij.psi.PsiElement
22-
import com.intellij.psi.PsiExpressionList
23-
import com.intellij.psi.PsiLiteralExpression
24-
import com.intellij.psi.PsiNewExpression
25-
import com.intellij.psi.impl.source.tree.JavaElementType
2624
import com.intellij.psi.util.PsiEditorUtil
2725
import com.intellij.ui.ColorChooser
2826
import com.intellij.util.FunctionUtil
2927
import com.intellij.util.ui.ColorIcon
3028
import com.intellij.util.ui.ColorsIcon
3129
import java.awt.Color
3230
import javax.swing.Icon
31+
import org.jetbrains.uast.UCallExpression
32+
import org.jetbrains.uast.UElement
33+
import org.jetbrains.uast.UIdentifier
34+
import org.jetbrains.uast.ULiteralExpression
35+
import org.jetbrains.uast.toUElementOfType
3336

3437
class ColorLineMarkerProvider : LineMarkerProvider {
3538

@@ -38,7 +41,8 @@ class ColorLineMarkerProvider : LineMarkerProvider {
3841
return null
3942
}
4043

41-
val info = element.findColor { map, chosen -> ColorInfo(element, chosen.value, map, chosen.key) }
44+
val identifier = element.toUElementOfType<UIdentifier>() ?: return null
45+
val info = identifier.findColor { map, chosen -> ColorInfo(element, chosen.value, map, chosen.key, identifier) }
4246
if (info != null) {
4347
NavigateAction.setNavigateAction(info, "Change Color", null)
4448
}
@@ -49,7 +53,13 @@ class ColorLineMarkerProvider : LineMarkerProvider {
4953
open class ColorInfo : MergeableLineMarkerInfo<PsiElement> {
5054
protected val color: Color
5155

52-
constructor(element: PsiElement, color: Color, map: Map<String, Color>, colorName: String) : super(
56+
constructor(
57+
element: PsiElement,
58+
color: Color,
59+
map: Map<String, Color>,
60+
colorName: String,
61+
workElement: UElement
62+
) : super(
5363
element,
5464
element.textRange,
5565
ColorIcon(12, color),
@@ -63,8 +73,8 @@ class ColorLineMarkerProvider : LineMarkerProvider {
6373

6474
val picker = ColorPicker(map, editor.component)
6575
val newColor = picker.showDialog()
66-
if (newColor != null) {
67-
element.setColor(newColor)
76+
if (newColor != null && map[newColor] != color) {
77+
workElement.setColor(newColor)
6878
}
6979
},
7080
GutterIconRenderer.Alignment.RIGHT,
@@ -103,7 +113,7 @@ class ColorLineMarkerProvider : LineMarkerProvider {
103113
class CommonColorInfo(
104114
element: PsiElement,
105115
color: Color,
106-
workElement: PsiElement
116+
workElement: UElement
107117
) : ColorLineMarkerProvider.ColorInfo(
108118
element,
109119
color,
@@ -113,18 +123,20 @@ class ColorLineMarkerProvider : LineMarkerProvider {
113123
}
114124

115125
val editor = PsiEditorUtil.findEditor(element) ?: return@handler
126+
if (JVMElementFactories.getFactory(element.language, element.project) == null) {
127+
// The setColor methods used here require a JVMElementFactory. Unfortunately the Kotlin plugin does not
128+
// implement it yet. It is better to not display the color chooser at all than deceiving users after
129+
// after they chose a color
130+
HintManager.getInstance()
131+
.showErrorHint(editor, "Can't change colors in " + element.language.displayName)
132+
return@handler
133+
}
116134

117135
val c = ColorChooser.chooseColor(editor.component, "Choose Color", color, false)
118-
if (c != null) {
119-
when (workElement) {
120-
is PsiLiteralExpression -> workElement.setColor(c.rgb and 0xFFFFFF)
121-
is PsiExpressionList -> workElement.setColor(c.red, c.green, c.blue)
122-
is PsiNewExpression -> {
123-
val list = workElement.getNode().findChildByType(JavaElementType.EXPRESSION_LIST)
124-
as PsiExpressionList?
125-
list?.setColor(c.red, c.green, c.blue)
126-
}
127-
}
136+
?: return@handler
137+
when (workElement) {
138+
is ULiteralExpression -> workElement.setColor(c.rgb and 0xFFFFFF)
139+
is UCallExpression -> workElement.setColor(c.red, c.green, c.blue)
128140
}
129141
}
130142
)
@@ -139,6 +151,6 @@ class ColorLineMarkerProvider : LineMarkerProvider {
139151
return info
140152
}
141153

142-
abstract fun findColor(element: PsiElement): Pair<Color, PsiElement>?
154+
abstract fun findColor(element: PsiElement): Pair<Color, UElement>?
143155
}
144156
}

0 commit comments

Comments
 (0)