Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add specialized MCP color support #459

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/kotlin/com/demonwav/mcdev/insight/ColorUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ fun PsiElement.setColor(color: String) {
}
}

fun PsiLiteralExpression.setColor(value: Int) {
fun PsiLiteralExpression.setColor(value: Int, hasAlpha: Boolean = false) {
this.containingFile.runWriteAction {
val node = this.node

val padLength = if (hasAlpha) 8 else 6
val literalExpression = JavaPsiFacade.getElementFactory(this.project)
.createExpressionFromText("0x" + Integer.toHexString(value).toUpperCase(), null) as PsiLiteralExpression
.createExpressionFromText("0x" + Integer.toHexString(value).toUpperCase().padStart(padLength, '0'), null) as PsiLiteralExpression

node.psi.replace(literalExpression)
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/demonwav/mcdev/platform/mcp/McpModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.demonwav.mcdev.facet.MinecraftFacet
import com.demonwav.mcdev.i18n.I18nFileListener
import com.demonwav.mcdev.platform.AbstractModule
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.mcp.color.McpColorMethods
import com.demonwav.mcdev.platform.mcp.srg.SrgManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
Expand All @@ -31,6 +32,7 @@ class McpModule(facet: MinecraftFacet) : AbstractModule(facet) {

var srgManager: SrgManager? = null
private set
val colorMethods = McpColorMethods[settings.state.minecraftVersion ?: "1.12"]

override fun init() {
val files = getSettings().mappingFiles
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Minecraft Dev for IntelliJ
*
* https://minecraftdev.org
*
* Copyright (c) 2018 minecraft-dev
*
* MIT License
*/

package com.demonwav.mcdev.platform.mcp.color

import com.demonwav.mcdev.MinecraftSettings
import com.demonwav.mcdev.insight.ColorAnnotator
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.psi.PsiElement

class McpColorAnnotator : Annotator {

override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if (!MinecraftSettings.instance.isShowChatColorUnderlines) {
return
}

for (call in element.findColors()) {
ColorAnnotator.setColorAnnotator(call.arg, element, holder)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Minecraft Dev for IntelliJ
*
* https://minecraftdev.org
*
* Copyright (c) 2018 minecraft-dev
*
* MIT License
*/

package com.demonwav.mcdev.platform.mcp.color

import com.intellij.codeHighlighting.Pass
import com.intellij.codeInsight.daemon.GutterIconNavigationHandler
import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.LineMarkerProvider
import com.intellij.codeInsight.daemon.MergeableLineMarkerInfo
import com.intellij.codeInsight.daemon.NavigateAction
import com.intellij.icons.AllIcons
import com.intellij.openapi.editor.markup.GutterIconRenderer
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiUtilBase
import com.intellij.ui.ColorChooser
import com.intellij.util.Function
import com.intellij.util.ui.ColorIcon
import com.intellij.util.ui.TwoColorsIcon
import java.awt.Color
import javax.swing.Icon

class McpColorLineMarkerProvider : LineMarkerProvider {
override fun getLineMarkerInfo(element: PsiElement) = null

override fun collectSlowLineMarkers(elements: List<PsiElement>, result: MutableCollection<LineMarkerInfo<PsiElement>>) {
for (element in elements) {
val calls = element.findColors()

for (call in calls) {
val info = McpColorInfo(element, call)
NavigateAction.setNavigateAction(info, "Change color", null)
result.add(info)
}
}
}

private class McpColorInfo(private val parent: PsiElement, private val result: McpColorResult<Color>) : MergeableLineMarkerInfo<PsiElement>(
result.expression,
result.argRange,
ColorIcon(12, result.arg),
Pass.UPDATE_ALL,
Function { result.param.description },
GutterIconNavigationHandler handler@{ _, _ ->
if (!result.expression.isWritable) {
return@handler
}

val editor = PsiUtilBase.findEditor(result.expression) ?: return@handler

val c = ColorChooser.chooseColor(editor.component, "Choose ${result.param.description}", result.arg, result.param.hasAlpha)
if (c != null) {
result.param.setColor(result.withArg(c))
}
},
GutterIconRenderer.Alignment.RIGHT
) {
override fun canMergeWith(info: MergeableLineMarkerInfo<*>) = info is McpColorInfo && info.parent == parent
override fun getCommonIconAlignment(infos: List<MergeableLineMarkerInfo<*>>) = GutterIconRenderer.Alignment.RIGHT

override fun getCommonIcon(infos: List<MergeableLineMarkerInfo<*>>): Icon {
if (infos.size == 2 && infos[0] is McpColorInfo && infos[1] is McpColorInfo) {
return TwoColorsIcon(12, (infos[0] as McpColorInfo).result.arg, (infos[1] as McpColorInfo).result.arg)
}
return AllIcons.Gutter.Colors
}

override fun getElementPresentation(element: PsiElement): String {
return result.param.description
}
}
}
Loading