Skip to content

Commit

Permalink
Merge pull request #169 from Lootwig/dart-ref-support
Browse files Browse the repository at this point in the history
Support for references to color values in Dart
  • Loading branch information
mallowigi authored Sep 13, 2024
2 parents 668a4b2 + f61960d commit 0ada0f7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ <h1>Changelog</h1>

<h2>18.0.0</h2>


<h2>17.0.1</h2>

<h3>Fixes</h3>
Expand Down
47 changes: 44 additions & 3 deletions src/main/java/com/mallowigi/visitors/DartVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,27 @@ package com.mallowigi.visitors
import com.intellij.codeInsight.daemon.impl.HighlightVisitor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.util.PsiUtilCore
import com.intellij.psi.util.childLeafs
import com.intellij.psi.util.parentOfType
import com.jetbrains.lang.dart.DartTokenTypes
import com.jetbrains.lang.dart.psi.DartRecursiveVisitor
import com.jetbrains.lang.dart.psi.DartReference
import com.jetbrains.lang.dart.psi.DartVarDeclarationList
import com.jetbrains.lang.dart.psi.DartVarInit
import com.mallowigi.search.ColorSearchEngine
import org.rust.lang.core.psi.ext.descendantOfType
import org.toml.lang.psi.ext.elementType
import java.awt.Color

class DartVisitor : ColorVisitor() {
val extensions = setOf(
"dart",
)

private val allowedTypes = setOf("REGULAR_STRING_PART", "NUMBER")
private val allowedTypes = setOf("REGULAR_STRING_PART", "NUMBER", "REFERENCE_EXPRESSION")

override fun clone(): HighlightVisitor = DartVisitor()

Expand All @@ -49,8 +60,38 @@ class DartVisitor : ColorVisitor() {
val type = PsiUtilCore.getElementType(element).toString()
if (type !in allowedTypes) return null

val value = element.text
return ColorSearchEngine.getColor(value, this)
if (element is DartReference) {
if (element.parentOfType<DartReference>()?.resolve() == element.resolve()) return null
return element.readReferencedValue()
} else {
return ColorSearchEngine.getColor(element.text, this)
}
}

private fun createVisitor(): ColorInitializer = ColorInitializer(this)

private fun DartReference.readReferencedValue(): Color? {
if (this.descendantOfType<LeafPsiElement> { it.elementType == DartTokenTypes.IDENTIFIER } != null) {
val visitor = createVisitor()
this.resolve()?.let { visitor.visitElement(it) }
return visitor.result
}
return null
}
}

class ColorInitializer(private val visitor: DartVisitor) : DartRecursiveVisitor() {
var result: Color? = null

override fun visitElement(element: PsiElement) {
val filteredElements =
element.parentOfType<DartVarDeclarationList>()?.descendantOfType<DartVarInit> { true }?.childLeafs()
?.filter { it !is PsiWhiteSpace && it.elementType != DartTokenTypes.EQ }
result = filteredElements?.windowed(4, 1)?.firstOrNull { window ->
Triple(window[0].elementType, window[0].text, window[2].elementType).let {
it.first == DartTokenTypes.IDENTIFIER && it.second == "Color" && it.third == DartTokenTypes.NUMBER
}
}?.elementAt(2)?.let { ColorSearchEngine.getColor(it.text, visitor) }
if (result == null) element.acceptChildren(this) else return
}
}

0 comments on commit 0ada0f7

Please sign in to comment.