finishing touches for the toolchain selector

This commit is contained in:
FalsePattern 2025-04-09 22:35:44 +02:00
parent 54cd514249
commit 12e5ffdea1
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
7 changed files with 45 additions and 34 deletions

View file

@ -36,7 +36,7 @@ import java.util.UUID
name = "ZigToolchainList",
storages = [Storage("zigbrains.xml")]
)
class ZigToolchainListService: SerializablePersistentStateComponent<ZigToolchainListService.State>(State()), ZigToolchainListService.IService {
class ZigToolchainListService: SerializablePersistentStateComponent<ZigToolchainListService.State>(State()), IZigToolchainListService {
private val changeListeners = ArrayList<WeakReference<ToolchainListChangeListener>>()
override val toolchains: Sequence<Pair<UUID, ZigToolchain>>
@ -133,22 +133,22 @@ class ZigToolchainListService: SerializablePersistentStateComponent<ZigToolchain
companion object {
@JvmStatic
fun getInstance(): IService = service<ZigToolchainListService>()
}
sealed interface IService {
val toolchains: Sequence<Pair<UUID, ZigToolchain>>
fun setToolchain(uuid: UUID, toolchain: ZigToolchain)
fun registerNewToolchain(toolchain: ZigToolchain): UUID
fun getToolchain(uuid: UUID): ZigToolchain?
fun hasToolchain(uuid: UUID): Boolean
fun removeToolchain(uuid: UUID)
fun addChangeListener(listener: ToolchainListChangeListener)
fun removeChangeListener(listener: ToolchainListChangeListener)
}
@FunctionalInterface
interface ToolchainListChangeListener {
suspend fun toolchainListChanged()
fun getInstance(): IZigToolchainListService = service<ZigToolchainListService>()
}
}
@FunctionalInterface
interface ToolchainListChangeListener {
suspend fun toolchainListChanged()
}
sealed interface IZigToolchainListService {
val toolchains: Sequence<Pair<UUID, ZigToolchain>>
fun setToolchain(uuid: UUID, toolchain: ZigToolchain)
fun registerNewToolchain(toolchain: ZigToolchain): UUID
fun getToolchain(uuid: UUID): ZigToolchain?
fun hasToolchain(uuid: UUID): Boolean
fun removeToolchain(uuid: UUID)
fun addChangeListener(listener: ToolchainListChangeListener)
fun removeChangeListener(listener: ToolchainListChangeListener)
}

View file

@ -37,7 +37,7 @@ import java.util.UUID
name = "ZigToolchain",
storages = [Storage("zigbrains.xml")]
)
class ZigToolchainService: SerializablePersistentStateComponent<ZigToolchainService.State>(State()), ZigToolchainService.IService {
class ZigToolchainService: SerializablePersistentStateComponent<ZigToolchainService.State>(State()), IZigToolchainService {
override var toolchainUUID: UUID?
get() = state.toolchain.ifBlank { null }?.let { UUID.fromString(it) }?.takeIf {
if (ZigToolchainListService.getInstance().hasToolchain(it)) {
@ -66,11 +66,11 @@ class ZigToolchainService: SerializablePersistentStateComponent<ZigToolchainServ
companion object {
@JvmStatic
fun getInstance(project: Project): IService = project.service<ZigToolchainService>()
fun getInstance(project: Project): IZigToolchainService = project.service<ZigToolchainService>()
}
}
sealed interface IService {
var toolchainUUID: UUID?
val toolchain: ZigToolchain?
}
sealed interface IZigToolchainService {
var toolchainUUID: UUID?
val toolchain: ZigToolchain?
}

View file

@ -42,7 +42,7 @@ internal interface ZigToolchainProvider {
fun matchesSuggestion(toolchain: ZigToolchain, suggestion: ZigToolchain): Boolean
fun createConfigurable(uuid: UUID, toolchain: ZigToolchain): ZigToolchainConfigurable<*>
fun suggestToolchains(): List<Pair<SemVer?, ZigToolchain>>
fun render(toolchain: ZigToolchain, component: SimpleColoredComponent, isSuggestion: Boolean)
fun render(toolchain: ZigToolchain, component: SimpleColoredComponent, isSuggestion: Boolean, isSelected: Boolean)
}
fun ZigToolchain.Ref.resolve(): ZigToolchain? {
@ -67,7 +67,7 @@ fun ZigToolchain.createNamedConfigurable(uuid: UUID): ZigToolchainConfigurable<*
}
fun suggestZigToolchains(): List<ZigToolchain> {
val existing = ZigToolchainListService.getInstance().toolchains.map { (uuid, tc) -> tc }.toList()
val existing = ZigToolchainListService.getInstance().toolchains.map { (_, tc) -> tc }.toList()
return EXTENSION_POINT_NAME.extensionList.flatMap { ext ->
val compatibleExisting = existing.filter { ext.isCompatible(it) }
val suggestions = ext.suggestToolchains()
@ -75,7 +75,7 @@ fun suggestZigToolchains(): List<ZigToolchain> {
}.sortedByDescending { it.first }.map { it.second }
}
fun ZigToolchain.render(component: SimpleColoredComponent, isSuggestion: Boolean) {
fun ZigToolchain.render(component: SimpleColoredComponent, isSuggestion: Boolean, isSelected: Boolean) {
val provider = EXTENSION_POINT_NAME.extensionList.find { it.isCompatible(this) } ?: throw IllegalStateException()
return provider.render(this, component, isSuggestion)
return provider.render(this, component, isSuggestion, isSelected)
}

View file

@ -116,24 +116,33 @@ class LocalZigToolchainProvider: ZigToolchainProvider {
return res.mapNotNull { LocalZigToolchain.tryFromPathString(it) }
}
override fun render(toolchain: ZigToolchain, component: SimpleColoredComponent, isSuggestion: Boolean) {
override fun render(toolchain: ZigToolchain, component: SimpleColoredComponent, isSuggestion: Boolean, isSelected: Boolean) {
toolchain as LocalZigToolchain
val path = presentDetectedPath(toolchain.location.pathString)
val name = toolchain.name
val path = presentDetectedPath(toolchain.location.pathString)
val primary: String
val secondary: String?
val tooltip: String?
if (isSuggestion) {
primary = path
secondary = name
tooltip = null
} else {
primary = name ?: "Zig"
secondary = path
if (isSelected) {
secondary = null
tooltip = path
} else {
secondary = path
tooltip = null
}
}
component.append(primary)
if (secondary != null) {
component.append(" ")
component.append(secondary, SimpleTextAttributes.GRAYED_ATTRIBUTES)
}
component.toolTipText = tooltip
}
}

View file

@ -23,6 +23,7 @@
package com.falsepattern.zigbrains.project.toolchain.ui
import com.falsepattern.zigbrains.ZigBrainsBundle
import com.falsepattern.zigbrains.project.toolchain.ToolchainListChangeListener
import com.falsepattern.zigbrains.project.toolchain.ZigToolchainListService
import com.falsepattern.zigbrains.project.toolchain.ZigToolchainService
import com.falsepattern.zigbrains.project.toolchain.base.createNamedConfigurable
@ -48,7 +49,7 @@ import java.util.UUID
import javax.swing.JButton
import kotlin.collections.addAll
class ZigToolchainEditor(private val isForDefaultProject: Boolean = false): SubConfigurable<Project>, ZigToolchainListService.ToolchainListChangeListener {
class ZigToolchainEditor(private val isForDefaultProject: Boolean = false): SubConfigurable<Project>, ToolchainListChangeListener {
private val toolchainBox: TCComboBox
private var selectOnNextReload: UUID? = null
private val model: TCModel

View file

@ -24,6 +24,7 @@ package com.falsepattern.zigbrains.project.toolchain.ui
import com.falsepattern.zigbrains.Icons
import com.falsepattern.zigbrains.ZigBrainsBundle
import com.falsepattern.zigbrains.project.toolchain.ToolchainListChangeListener
import com.falsepattern.zigbrains.project.toolchain.ZigToolchainListService
import com.falsepattern.zigbrains.project.toolchain.base.ZigToolchain
import com.falsepattern.zigbrains.project.toolchain.base.createNamedConfigurable
@ -62,7 +63,7 @@ import javax.swing.JComponent
import javax.swing.event.DocumentEvent
import javax.swing.tree.DefaultTreeModel
class ZigToolchainListEditor : MasterDetailsComponent(), ZigToolchainListService.ToolchainListChangeListener {
class ZigToolchainListEditor : MasterDetailsComponent(), ToolchainListChangeListener {
private var isTreeInitialized = false
private var registered: Boolean = false
private var itemSelectedListeners = ArrayList<Consumer<UUID?>>()

View file

@ -204,7 +204,7 @@ internal class TCCellRenderer(val getModel: () -> TCModel) : ColoredListCellRend
}
this.icon = icon
val toolchain = value.toolchain
toolchain.render(this, isSuggestion)
toolchain.render(this, isSuggestion, index == -1)
}
is TCListElem.Download -> {