From d94d3ec925ed1f8b8fcecfdbb0c7ca7c07a9da13 Mon Sep 17 00:00:00 2001 From: thelooter Date: Fri, 6 Dec 2024 20:23:14 +0100 Subject: [PATCH] fix(go): fix go icons when go plugin is installed (#158) --- .../activity/IconPathPatcherActivity.kt | 2 + .../jetbrains_icons/patcher/GoIconPatcher.kt | 41 +++++++++++++++++++ .../settings/PluginSettings.kt | 4 +- .../settings/PluginSettingsState.kt | 1 + .../views/SettingsAdditionalSupportView.kt | 3 ++ 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/github/catppuccin/jetbrains_icons/patcher/GoIconPatcher.kt diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/activity/IconPathPatcherActivity.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/activity/IconPathPatcherActivity.kt index b442afa..f20b9dd 100644 --- a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/activity/IconPathPatcherActivity.kt +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/activity/IconPathPatcherActivity.kt @@ -1,5 +1,6 @@ package com.github.catppuccin.jetbrains_icons.activity +import com.github.catppuccin.jetbrains_icons.patcher.GoIconPatcher import com.github.catppuccin.jetbrains_icons.patcher.PythonIconPatcher import com.intellij.openapi.project.Project import com.intellij.openapi.startup.ProjectActivity @@ -9,5 +10,6 @@ import com.intellij.openapi.util.IconPathPatcher class IconPathPatcherActivity : ProjectActivity { override suspend fun execute(project: Project) { PythonIconPatcher.install() + GoIconPatcher.install() } } diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/patcher/GoIconPatcher.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/patcher/GoIconPatcher.kt new file mode 100644 index 0000000..bf677a5 --- /dev/null +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/patcher/GoIconPatcher.kt @@ -0,0 +1,41 @@ +package com.github.catppuccin.jetbrains_icons.patcher + +import com.github.catppuccin.jetbrains_icons.settings.PluginSettingsState +import com.intellij.openapi.util.IconLoader +import com.intellij.openapi.util.IconPathPatcher +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock + +class GoIconPatcher : IconPathPatcher() { + + private val settingsInstance = PluginSettingsState.instance + + // Only patch the path if Go support is enabled + override fun patchPath(path: String, classLoader: ClassLoader?): String? = + if (settingsInstance.goSupport && overrideMap.containsKey(path)) { + "/jetbrains_icons/icons/${settingsInstance.variant}${overrideMap[path]}" + } else { + null + } + + override fun getContextClassLoader( + path: String, + originalClassLoader: ClassLoader?, + ): ClassLoader? = javaClass.classLoader + + companion object { + private val isInstalledMutex = Mutex() + private var isInstalled = false + + suspend fun install() = + isInstalledMutex.withLock { + if (!isInstalled) { + isInstalled = true + IconLoader.installPathPatcher(GoIconPatcher()) + } + } + + private val overrideMap: Map = + mapOf("/icons/expui/runTest@20x20.svg" to "_go.svg", "/icons/runTest.svg" to "_go.svg") + } +} diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettings.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettings.kt index 5ae7693..90a41b4 100644 --- a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettings.kt +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettings.kt @@ -19,7 +19,8 @@ class PluginSettings : Configurable { val state = PluginSettingsState.instance return packChanged() || component.additionalSupport.python.isSelected != state.pythonSupport || - component.additionalSupport.java.isSelected != state.javaSupport + component.additionalSupport.java.isSelected != state.javaSupport || + component.additionalSupport.go.isSelected != state.goSupport } override fun apply() { @@ -27,6 +28,7 @@ class PluginSettings : Configurable { state.pythonSupport = component.additionalSupport.python.isSelected state.javaSupport = component.additionalSupport.java.isSelected + state.goSupport = component.additionalSupport.go.isSelected if (packChanged()) { state.variant = component.iconPack.variant diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettingsState.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettingsState.kt index 55c2681..b9c20e6 100644 --- a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettingsState.kt +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/PluginSettingsState.kt @@ -17,6 +17,7 @@ class PluginSettingsState : PersistentStateComponent { var pythonSupport = true var javaSupport = isPluginInstalled(getId("com.intellij.java")) + var goSupport = true override fun getState(): PluginSettingsState = this diff --git a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/views/SettingsAdditionalSupportView.kt b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/views/SettingsAdditionalSupportView.kt index ca38152..22cc202 100644 --- a/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/views/SettingsAdditionalSupportView.kt +++ b/src/main/kotlin/com/github/catppuccin/jetbrains_icons/settings/views/SettingsAdditionalSupportView.kt @@ -14,6 +14,7 @@ class SettingsAdditionalSupportView : JPanel() { JBCheckBox("Java Filetypes", PluginSettingsState.instance.javaSupport).apply { isEnabled = isPluginInstalled(findId("com.intellij.java")) } + val go = JBCheckBox("Go", PluginSettingsState.instance.goSupport) init { val form = @@ -24,6 +25,8 @@ class SettingsAdditionalSupportView : JPanel() { .addTooltip( "Use different shapes and colors for Java filetypes (e.g. Class, Interface, Record, etc.)" ) + .addComponent(go) + .addTooltip("Override the Go plugin icons") .panel add(form)