From 2eb4603ed0272a5fc666dc53473fa53d858b7d9b Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Sat, 24 Dec 2022 07:56:09 +0530 Subject: [PATCH] feat: store color IDs for color values --- .../editor/schemes/IDEColorScheme.kt | 19 +++++++- .../schemes/internal/parser/SchemeParser.kt | 28 +++++++----- .../androidide/editor/schemes/syntax.json | 10 ++--- .../language/incremental/SchemeParserTest.kt | 41 ++++++++++++++++-- tests/test-home/.androidide/init/model.jar | Bin 65261 -> 65261 bytes 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/editor/src/main/java/com/itsaky/androidide/editor/schemes/IDEColorScheme.kt b/editor/src/main/java/com/itsaky/androidide/editor/schemes/IDEColorScheme.kt index 0de288e76..bb899cbc2 100644 --- a/editor/src/main/java/com/itsaky/androidide/editor/schemes/IDEColorScheme.kt +++ b/editor/src/main/java/com/itsaky/androidide/editor/schemes/IDEColorScheme.kt @@ -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() internal val editorScheme = mutableMapOf() internal val languages = mutableMapOf() + 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) + } } /** diff --git a/editor/src/main/java/com/itsaky/androidide/editor/schemes/internal/parser/SchemeParser.kt b/editor/src/main/java/com/itsaky/androidide/editor/schemes/internal/parser/SchemeParser.kt index e5687f19d..c109af09e 100644 --- a/editor/src/main/java/com/itsaky/androidide/editor/schemes/internal/parser/SchemeParser.kt +++ b/editor/src/main/java/com/itsaky/androidide/editor/schemes/internal/parser/SchemeParser.kt @@ -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 { diff --git a/editor/src/main/java/com/itsaky/androidide/editor/schemes/syntax.json b/editor/src/main/java/com/itsaky/androidide/editor/schemes/syntax.json index 9f4ec61d8..35233dfff 100644 --- a/editor/src/main/java/com/itsaky/androidide/editor/schemes/syntax.json +++ b/editor/src/main/java/com/itsaky/androidide/editor/schemes/syntax.json @@ -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" } diff --git a/editor/src/test/java/com/itsaky/androidide/language/incremental/SchemeParserTest.kt b/editor/src/test/java/com/itsaky/androidide/language/incremental/SchemeParserTest.kt index 05c0e03d9..efc1c926e 100644 --- a/editor/src/test/java/com/itsaky/androidide/language/incremental/SchemeParserTest.kt +++ b/editor/src/test/java/com/itsaky/androidide/language/incremental/SchemeParserTest.kt @@ -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")) diff --git a/tests/test-home/.androidide/init/model.jar b/tests/test-home/.androidide/init/model.jar index f96dc16c4e263e1012f3bf2166a2d23fab5f6769..b2515948be8da8117d67c6d4af18eac763b96013 100644 GIT binary patch delta 31 ncmaF+m-+2qW}X0VW)?061`ZB}+jbjy{N6L&w%Z)@ej+~rwO9*p delta 31 ncmaF+m-+2qW}X0VW)?061`ZB}3Wkk5e(#wo7&gbepU4jYsGkZg