@@ -2,23 +2,46 @@ package org.c3lang.intellij
22
33import com.intellij.openapi.editor.ElementColorProvider
44import com.intellij.psi.PsiElement
5+ import com.intellij.psi.PsiFileFactory
56import com.intellij.ui.JBColor
6- import org.c3lang.intellij.psi.C3LiteralExpr
7+ import org.c3lang.intellij.psi.C3Types
78import java.awt.Color
89
910class C3ElementColorProvider : ElementColorProvider
1011{
12+ private var lastElement : PsiElement ? = null
13+
1114 override fun getColorFrom (element : PsiElement ): Color ?
12- {
13- if (element !is C3LiteralExpr ) return null
15+ { if (element.node.elementType != C3Types .INT_LITERAL ) return null
1416 if (! element.text.matches(Regex (" 0x([\\ dA-Fa-f]{8}|[\\ dA-Fa-f]{6})" ))) return null
15-
16- val rgb = element.text.substring(2 ).toLong(16 )
17-
18- return JBColor (rgb.toInt(), rgb.toInt())
17+ val rgba = element.text.substring(2 )
18+ var intval = rgba.toLong(16 )
19+ if (rgba.length == 6 ) {
20+ return JBColor (intval.toInt(), intval.toInt())
21+ }
22+ intval = ((intval and 0xFFFFFF00 ) shr 8 ) + ((intval and 0xFF ) shl 24 )
23+ return JBColor (Color (intval.toInt(), true ), Color (intval.toInt(), true ));
1924 }
2025
2126 override fun setColorTo (element : PsiElement , color : Color )
2227 {
28+ if (lastElement == null || element.parent.parent != null ) {
29+ lastElement = element
30+ }
31+ val upper = element.text.contains(Regex (" [A-F]" ))
32+ val str = if (element.text.length == 8 ) {
33+ String .format(if (upper) " 0x%02X%02X%02X" else " 0x%02x%02x%02x" , color.red, color.green, color.blue)
34+ } else {
35+ String .format(if (upper) " 0x%02X%02X%02X%02X" else " 0x%02x%02x%02x%02x" , color.red, color.green, color.blue, color.alpha)
36+ }
37+ val file = PsiFileFactory .getInstance(element.project).createFileFromText(
38+ " color_replace.dummy.c3" ,
39+ C3SourceFileType .INSTANCE ,
40+ " int a = $str ;" ,
41+ )
42+ val newElement = file.firstChild.firstChild.firstChild.children[1 ].children[0 ].firstChild
43+ val parent = lastElement!! .parent
44+ lastElement!! .replace(newElement)
45+ lastElement = parent.firstChild
2346 }
2447}
0 commit comments