Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 263ce48

Browse files
committed
fix(editor): fall back to default color scheme if custom scheme doesn't support a file type
1 parent 23f4f52 commit 263ce48

File tree

6 files changed

+56
-29
lines changed

6 files changed

+56
-29
lines changed

app/src/main/java/com/itsaky/androidide/fragments/LogViewFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ abstract class LogViewFragment : Fragment(), ShareableOutputFragment {
240240
}
241241
}
242242

243-
IDEColorSchemeProvider.readScheme(requireContext()) { scheme ->
243+
IDEColorSchemeProvider.readScheme(requireContext(), LogLanguage.TS_TYPE) { scheme ->
244244
editor.applyTreeSitterLang(LogLanguage(requireContext()), LogLanguage.TS_TYPE, scheme)
245245
}
246246
}

editor/src/main/java/com/itsaky/androidide/editor/language/treesitter/TreeSitterLanguage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ abstract class TreeSitterLanguage(context: Context, lang: TSLanguage, type: Stri
5757
init {
5858
this.languageSpec = getLanguageSpec(context, type, lang, newLocalCaptureSpec(type))
5959
this.tsTheme = TsTheme(languageSpec.spec.tsQuery)
60-
IDEColorSchemeProvider.readScheme(context) { scheme ->
60+
IDEColorSchemeProvider.readScheme(context, type) { scheme ->
6161
if (scheme == null) {
6262
log.error("Failed to read color scheme")
6363
return@readScheme

editor/src/main/java/com/itsaky/androidide/editor/schemes/IDEColorSchemeProvider.kt

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.itsaky.androidide.editor.schemes
1919

2020
import android.content.Context
2121
import com.itsaky.androidide.eventbus.events.editor.ColorSchemeInvalidatedEvent
22+
import com.itsaky.androidide.preferences.internal.DEFAULT_COLOR_SCHEME
2223
import com.itsaky.androidide.preferences.internal.colorScheme
2324
import com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE
2425
import com.itsaky.androidide.tasks.executeAsyncProvideError
@@ -45,25 +46,32 @@ object IDEColorSchemeProvider {
4546
private const val SCHEME_IS_DARK = "scheme.isDark"
4647
private const val SCHEME_FILE = "scheme.file"
4748

49+
var defaultScheme: IDEColorScheme? = null
50+
get() {
51+
return field ?: getColorScheme(DEFAULT_COLOR_SCHEME).also { field = it }
52+
}
53+
private set
54+
4855
var currentScheme: IDEColorScheme? = null
4956
get() {
50-
if (field != null) {
51-
return field
52-
}
57+
return field ?: getColorScheme(colorScheme).also { field = it }
58+
}
59+
private set
5360

54-
val scheme = this.schemes[colorScheme] ?: return null
55-
field = try {
56-
scheme.load()
57-
scheme.darkVariant?.load()
58-
scheme
59-
} catch (err: Exception) {
60-
log.error("An error occurred while loading color scheme '$colorScheme'", err)
61-
null
62-
}
61+
private fun getColorScheme(name: String): IDEColorScheme? {
62+
return schemes[name]?.also(this::loadColorScheme)
63+
}
6364

64-
return field
65+
private fun loadColorScheme(scheme: IDEColorScheme): IDEColorScheme? {
66+
return try {
67+
scheme.load()
68+
scheme.darkVariant?.load()
69+
scheme
70+
} catch (err: Exception) {
71+
log.error("An error occurred while loading color scheme '$colorScheme'", err)
72+
null
6573
}
66-
private set
74+
}
6775

6876
@JvmStatic
6977
fun init() {
@@ -97,11 +105,11 @@ object IDEColorSchemeProvider {
97105
)
98106
""
99107
}
100-
108+
101109
if (version <= 0) {
102110
log.warn("Version code of color scheme '$schemeDir' must be set to >= 1")
103111
}
104-
112+
105113
if (file.isBlank()) {
106114
continue
107115
}
@@ -112,32 +120,34 @@ object IDEColorSchemeProvider {
112120
scheme.isDarkScheme = isDark
113121
schemes[schemeDir.name] = scheme
114122
}
115-
123+
116124
schemes.values.forEach {
117125
it.darkVariant = schemes["${it.key}-dark"]
118126
}
119127
}
120-
128+
121129
@JvmStatic
122130
fun initIfNeeded() {
123131
if (this.schemes.isEmpty()) {
124132
init()
125133
}
126134
}
127135

128-
fun readScheme(context: Context, schemeConsumer: Consumer<SchemeAndroidIDE?>) {
129-
readScheme(context) {
136+
@JvmOverloads
137+
fun readScheme(context: Context, type: String? = null, schemeConsumer: Consumer<SchemeAndroidIDE?>) {
138+
readScheme(context, type) {
130139
schemeConsumer.accept(it)
131140
}
132141
}
133142

134-
fun readScheme(context: Context, consume: (SchemeAndroidIDE?) -> Unit) {
135-
executeAsyncProvideError({ this.currentScheme }) { scheme, error ->
143+
@JvmOverloads
144+
fun readScheme(context: Context, type: String? = null, consume: (SchemeAndroidIDE?) -> Unit) {
145+
executeAsyncProvideError({ getColorSchemeForType(type) }) { scheme, error ->
136146
if (scheme == null || error != null) {
137147
log.error("Failed to read color scheme", error)
138148
return@executeAsyncProvideError
139149
}
140-
150+
141151
val dark = scheme.darkVariant
142152
if (context.isSystemInDarkMode() && dark != null) {
143153
consume(dark)
@@ -147,15 +157,32 @@ object IDEColorSchemeProvider {
147157
}
148158
}
149159

160+
fun getColorSchemeForType(type: String?) : IDEColorScheme? {
161+
if (type == null) {
162+
return currentScheme
163+
}
164+
165+
return currentScheme?.let { scheme ->
166+
return@let if (scheme.getLanguageScheme(type) == null) {
167+
log.warn("Color scheme '${scheme.name}' does not support '$type'")
168+
log.warn("Falling back to default color scheme")
169+
null
170+
} else {
171+
scheme
172+
}
173+
} ?: defaultScheme
174+
}
175+
150176
fun list(): List<IDEColorScheme> {
151177
// filter out schemes that are dark variants of other schemes
152178
// schemes with both light and dark variant will be used according to system's dark mode
153179
return this.schemes.values.filter { !it.key.endsWith("-dark") }.toList()
154180
}
155-
181+
156182
fun destroy() {
157183
this.schemes.clear()
158184
this.currentScheme = null
185+
this.defaultScheme = null
159186
}
160187

161188
fun reload() {

editor/src/main/java/com/itsaky/androidide/editor/schemes/LocalCaptureSpecProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object LocalCaptureSpecProvider {
3232
@JvmStatic
3333
fun newLocalCaptureSpec(type: String): LocalsCaptureSpec {
3434
val lang =
35-
IDEColorSchemeProvider.currentScheme?.languages?.get(type)
35+
IDEColorSchemeProvider.getColorSchemeForType(type)?.languages?.get(type)
3636
?: run {
3737
log.error(
3838
"Cannot create LocalsCaptureSpec",

editor/src/main/java/com/itsaky/androidide/editor/ui/IDEEditor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ public void setupLanguage(File file) {
958958
final var language = createLanguage(file);
959959
final var extension = FilesKt.getExtension(file);
960960
if (language instanceof TreeSitterLanguage) {
961-
IDEColorSchemeProvider.INSTANCE.readScheme(getContext(), scheme -> {
961+
IDEColorSchemeProvider.INSTANCE.readScheme(getContext(), extension, scheme -> {
962962
applyTreeSitterLang(language, extension, scheme);
963963
});
964964
} else {

uidesigner/src/main/java/com/itsaky/androidide/uidesigner/ShowXmlActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ShowXmlActivity : BaseIDEActivity() {
6868
editor.setText(intent?.getStringExtra(KEY_XML) ?: "")
6969
editor.setTextSize(fontSize)
7070

71-
IDEColorSchemeProvider.readScheme(this) {
71+
IDEColorSchemeProvider.readScheme(this, XMLLanguage.TS_TYPE) {
7272
if (it == null) {
7373
log.error("Unable to load color sheme")
7474
return@readScheme

0 commit comments

Comments
 (0)