Skip to content

Commit 35b24b4

Browse files
committed
Update version and some fixes in the color provider.
1 parent a963624 commit 35b24b4

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
## [Unreleased]
66

7+
## [0.1.2] - 2025-04-04
8+
9+
- Line marker for main.
10+
- Color selector for #ff00aa.
11+
712
## [0.1.1] - 2025-04-04
813

914
- Added new file action for c3 projects

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.github.c3lang.c3intellij
44
pluginName = c3intellij
55
pluginRepositoryUrl = https://github.com/c3lang/c3intellij
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.1.1
7+
pluginVersion = 0.1.2
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 241

src/main/java/org/c3lang/intellij/C3ElementColorProvider.kt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,46 @@ package org.c3lang.intellij
22

33
import com.intellij.openapi.editor.ElementColorProvider
44
import com.intellij.psi.PsiElement
5+
import com.intellij.psi.PsiFileFactory
56
import com.intellij.ui.JBColor
6-
import org.c3lang.intellij.psi.C3LiteralExpr
7+
import org.c3lang.intellij.psi.C3Types
78
import java.awt.Color
89

910
class 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

Comments
 (0)