Created
November 7, 2019 13:55
-
-
Save DVDAndroid/342668a9c14c80cb409482ae089c5a98 to your computer and use it in GitHub Desktop.
AttributesDescriptorsBuilder DSL for Intellij Plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.editor.colors.TextAttributesKey | |
import com.intellij.openapi.options.colors.AttributesDescriptor | |
import java.util.* | |
fun attributesDescriptors(block: AttributesDescriptorsBuilder.() -> Unit): Array<AttributesDescriptor> = | |
AttributesDescriptorsBuilder().apply(block).build() | |
@DslMarker | |
annotation class DescriptorDsl | |
@DescriptorDsl | |
class AttributesDescriptorsBuilder { | |
private val attrs = mutableListOf<AttributesDescriptor>() | |
fun descriptor(displayName: String, attrKey: TextAttributesKey) = | |
attrs.add(AttributesDescriptor(displayName, attrKey)) | |
fun descriptor(block: DescriptorBuilder.() -> Unit) = attrs.add(DescriptorBuilder().apply(block).build()) | |
fun descriptors(displayName: String, block: DESCRIPTORS.() -> Unit) = | |
attrs.addAll(DESCRIPTORS(displayName).apply(block)) | |
fun build(): Array<AttributesDescriptor> = attrs.toTypedArray() | |
} | |
@DescriptorDsl | |
class DESCRIPTORS(private val parentDisplayName: String) : ArrayList<AttributesDescriptor>() { | |
fun descriptor(displayName: String, attrKey: TextAttributesKey) = add(DescriptorBuilder(parentDisplayName).apply { | |
name = displayName | |
key = attrKey | |
}.build()) | |
fun descriptor(block: DescriptorBuilder.() -> Unit) = add(DescriptorBuilder(parentDisplayName).apply(block).build()) | |
} | |
@DescriptorDsl | |
class DescriptorBuilder(private val parentDisplayName: String = "") { | |
lateinit var name: String | |
lateinit var key: TextAttributesKey | |
fun build() = AttributesDescriptor( | |
if (parentDisplayName.isEmpty()) name | |
else xBundle["settings.color.attribute.descriptor.separator", parentDisplayName, name], key | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment