Skip to content

Commit

Permalink
fix(go): fix go icons when go plugin is installed (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
thelooter authored Dec 6, 2024
1 parent e8969e6 commit d94d3ec
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,5 +10,6 @@ import com.intellij.openapi.util.IconPathPatcher
class IconPathPatcherActivity : ProjectActivity {
override suspend fun execute(project: Project) {
PythonIconPatcher.install()
GoIconPatcher.install()
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> =
mapOf("/icons/expui/[email protected]" to "_go.svg", "/icons/runTest.svg" to "_go.svg")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ 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() {
val state = PluginSettingsState.instance

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PluginSettingsState : PersistentStateComponent<PluginSettingsState> {

var pythonSupport = true
var javaSupport = isPluginInstalled(getId("com.intellij.java"))
var goSupport = true

override fun getState(): PluginSettingsState = this

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)
Expand Down

0 comments on commit d94d3ec

Please sign in to comment.