feat: store color IDs for color values

This commit is contained in:
Akash Yadav
2022-12-24 07:56:09 +05:30
parent b65146ac84
commit 2eb4603ed0
5 changed files with 79 additions and 19 deletions
@@ -17,11 +17,16 @@
package com.itsaky.androidide.editor.schemes
class IDEColorScheme {
import io.github.rosemoe.sora.widget.schemes.EditorColorScheme
class IDEColorScheme : EditorColorScheme() {
internal val colorIds = mutableMapOf<Int, Int>()
internal val editorScheme = mutableMapOf<Int, Int>()
internal val languages = mutableMapOf<String, LanguageScheme>()
private var colorId = END_COLOR_ID
var isDarkScheme: Boolean = false
internal set
@@ -31,6 +36,18 @@ class IDEColorScheme {
fun getLanguageScheme(type: String): LanguageScheme? {
return this.languages[type]
}
internal fun putColor(color: Int): Int {
this.colorIds[++colorId] = color
return colorId
}
@Suppress("UNNECESSARY_SAFE_CALL")
override fun getColor(type: Int): Int {
// getColor is called in superclass constructor
// in this case, the below properties will be null
return this.editorScheme?.get(type) ?: this.colorIds?.get(type) ?: super.getColor(type)
}
}
/**
@@ -122,27 +122,35 @@ private fun IDEColorScheme.parseEditorScheme(reader: JsonReader) {
while (reader.hasNext()) {
val color = EditorColors.forKey(reader.nextName())
val value = reader.nextString()
editorScheme[color.id] = parseColorValue(value)
editorScheme[color.id] = parseColorValue(value, false)
}
reader.endObject()
}
private fun IDEColorScheme.parseColorValue(value: String?): Int {
/**
* Parses the color and returns the color ID. If [colorId] is `false`, returns the int color value
* instead.
*/
private fun IDEColorScheme.parseColorValue(value: String?, colorId: Boolean = true): Int {
require(!value.isNullOrBlank()) { "Color value is not expected to be null or blank" }
return when (value[0]) {
'@' -> {
val refName = value.substring(1)
definitions[refName] ?: throw ParseException("Referenced color '$value' not found")
}
'#' -> {
if (value[0] == '@') {
val refName = value.substring(1)
val refValue = definitions[refName] ?: throw ParseException("Referenced color '$value' not found")
return if (colorId) refValue else colorIds[refValue]!!
}
if (value[0] == '#') {
val color =
try {
Color.parseColor(value)
} catch (err: Throwable) {
throw ParseException("Invalid hex color code: '$value'")
}
}
else -> throw ParseException("Unsupported color value '$value'")
return if (colorId) putColor(color) else color
}
throw ParseException("Unsupported color value '$value'")
}
private fun IDEColorScheme.parseDefinitions(reader: JsonReader): Map<String, Int> {
@@ -64,24 +64,24 @@
"text.debug": "@f5",
"text.verbose": "#4fc3f7",
"error.priority": {
"priority.error": {
"fg": "@black",
"bg": "#f44336",
"completion": false
},
"warning.priority": {
"priority.warning": {
"fg": "@black",
"bg": "#ffeb3b"
},
"info.priority": {
"priority.info": {
"fg": "@white",
"bg": "#1f65c0"
},
"debug.priority": {
"priority.debug": {
"fg": "@white",
"bg": "#9e9d24"
},
"verbose.priority": {
"priority.verbose": {
"fg": "@black",
"bg": "@white"
}
@@ -38,9 +38,44 @@ class SchemeParserTest {
assertThat(scheme).isNotNull()
assertThat(scheme.isDarkScheme).isFalse()
assertThat(scheme.definitions).hasSize(12)
assertThat(scheme.definitions["black"]).isEqualTo(Color.BLACK)
assertThat(scheme.definitions["white"]).isEqualTo(Color.WHITE)
assertThat(scheme.definitions["9e"]).isEqualTo(Color.parseColor("#9e9e9e"))
assertThat(scheme.colorIds[scheme.definitions["black"]]).isEqualTo(Color.BLACK)
assertThat(scheme.colorIds[scheme.definitions["white"]]).isEqualTo(Color.WHITE)
assertThat(scheme.colorIds[scheme.definitions["9e"]]).isEqualTo(Color.parseColor("#9e9e9e"))
scheme.languages["@log"].apply {
assertThat(this).isNotNull()
assertThat(this!!.styles).isNotEmpty()
this.styles["text.error"].apply {
assertThat(this).isNotNull()
assertThat(scheme.colorIds[this!!.fg]).isEqualTo(Color.parseColor("#f44336"))
}
this.styles["text.warning"].apply {
assertThat(this).isNotNull()
assertThat(scheme.colorIds[this!!.fg]).isEqualTo(Color.parseColor("#ffeb3b"))
}
this.styles["text.debug"].apply {
assertThat(this).isNotNull()
assertThat(scheme.colorIds[this!!.fg]).isEqualTo(Color.parseColor("#f5f5f5"))
assertThat(scheme.colorIds[this.fg]).isEqualTo(scheme.colorIds[scheme.definitions["f5"]])
}
this.styles["priority.debug"].apply {
assertThat(this).isNotNull()
assertThat(scheme.colorIds[this!!.fg]).isEqualTo(Color.WHITE)
assertThat(scheme.colorIds[this.fg]).isEqualTo(scheme.colorIds[scheme.definitions["white"]])
assertThat(scheme.colorIds[this.bg]).isEqualTo(Color.parseColor("#9e9d24"))
assertThat(this.bold || this.italic || this.strikeThrough).isFalse()
assertThat(this.completion).isTrue()
}
this.styles["priority.error"].apply {
assertThat(this).isNotNull()
assertThat(scheme.colorIds[this!!.fg]).isEqualTo(Color.BLACK)
assertThat(scheme.colorIds[this.fg]).isEqualTo(scheme.colorIds[scheme.definitions["black"]])
assertThat(scheme.colorIds[this.bg]).isEqualTo(Color.parseColor("#f44336"))
assertThat(this.bold || this.italic || this.strikeThrough).isFalse()
assertThat(this.completion).isFalse()
}
}
assertThat(scheme.editorScheme).hasSize(27)
assertThat(scheme.editorScheme[EditorColorScheme.WHOLE_BACKGROUND]).isEqualTo(Color.parseColor("#212121"))
Binary file not shown.