Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
feat: add coloring support for kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Barfurth authored and Sebastian Barfurth committed Dec 6, 2021
1 parent 2e35430 commit 7cfc536
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 133 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
## [Unreleased]

### Added
- keyword highlighting adapted from https://github.com/clutcher/comments_highlighter
- keyword highlighting for Java and Kotlin
- initial port of the theme
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<!-- Plugin description -->
This is a port of the [Plastic theme](https://plastictheme.com/) by [Will Stone](https://wstone.io/) adapted for
Jetbrains IDEs. In order to set some colors a few components from the
[comments highlighter](https://plugins.jetbrains.com/plugin/12895-comments-highlighter) plugin by
[clutcher](https://github.com/clutcher) were copied and modified. This allows separate highlighting of some things
Jetbrains Platform IDEs do not usually allow highlighting of.
Jetbrains IDEs. In order to set some colors a custom annotator was implemented. This allows separate highlighting of
some things Jetbrains Platform IDEs do not usually allow highlighting of. The custom annotator requires a specific
implementation per language in order to define which keywords are to be highlighted in what colors. The goal is to
be closer to the semantic highlighting used by VS Code.
<!-- Plugin description end -->

## Installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,30 @@ import com.intellij.openapi.util.Pair
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement

abstract class AbstractPlasticColoringAnnotator : Annotator {
abstract class AbstractSemanticColoringAnnotator : Annotator {

override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if(!isColorable(element)) return
if(!isKeyword(element)) return

val highlight = getHighlight(element.text, element.textRange.startOffset) ?: return

if(element.textMatches("abstract")) {
println(highlight)
println(getSemanticKeywords())
}

holder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(highlight.first)
.textAttributes(highlight.second)
.create()
}

private fun getHighlight(word: String, startOffset: Int): Pair<TextRange, TextAttributesKey>? {
val type = getTypeByWord(word) ?: return null
val type = getSemanticKeywords()[word] ?: return null
return Pair(TextRange(startOffset, startOffset + word.length), type.getAttributeKey())
}

private fun isColorable(element: PsiElement): Boolean {
return getColorableClasses().any { clazz -> clazz.isAssignableFrom(element.javaClass) }
}

private fun getTypeByWord(word: String): PlasticTokenType? {
if(getConstantKeywords().contains(word)) return PlasticTokenType.CONSTANT
if(getStorageKeywords().contains(word)) return PlasticTokenType.STORAGE
if(getTypeKeywords().contains(word)) return PlasticTokenType.TYPE
return null
}

protected abstract fun getColorableClasses(): Collection<Class<*>>

protected abstract fun getConstantKeywords(): Collection<String>
protected abstract fun getStorageKeywords(): Collection<String>
protected abstract fun getTypeKeywords(): Collection<String>
protected abstract fun isKeyword(element: PsiElement): Boolean
protected abstract fun getSemanticKeywords(): Map<String, SemanticTokenType>

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.barfurth.jb.plastic.annotator

import com.intellij.psi.PsiElement
import com.intellij.psi.PsiKeyword
import com.intellij.psi.PsiLiteralExpression

class JavaSemanticColoringAnnotator : AbstractSemanticColoringAnnotator() {

override fun isKeyword(element: PsiElement): Boolean {
return element is PsiKeyword || element is PsiLiteralExpression
}

override fun getSemanticKeywords(): Map<String, SemanticTokenType> {
return mapOf(
PsiKeyword.TRUE to SemanticTokenType.CONSTANT,
PsiKeyword.FALSE to SemanticTokenType.CONSTANT,
PsiKeyword.NULL to SemanticTokenType.CONSTANT,

PsiKeyword.CLASS to SemanticTokenType.STORAGE,
PsiKeyword.RECORD to SemanticTokenType.STORAGE,
PsiKeyword.INTERFACE to SemanticTokenType.STORAGE,
PsiKeyword.ENUM to SemanticTokenType.STORAGE,
PsiKeyword.VAR to SemanticTokenType.STORAGE,
PsiKeyword.CONST to SemanticTokenType.STORAGE,

PsiKeyword.CHAR to SemanticTokenType.TYPE,
PsiKeyword.BOOLEAN to SemanticTokenType.TYPE,
PsiKeyword.LONG to SemanticTokenType.TYPE,
PsiKeyword.FLOAT to SemanticTokenType.TYPE,
PsiKeyword.INT to SemanticTokenType.TYPE,
PsiKeyword.BYTE to SemanticTokenType.TYPE,
PsiKeyword.SHORT to SemanticTokenType.TYPE,
PsiKeyword.DOUBLE to SemanticTokenType.TYPE,
PsiKeyword.VOID to SemanticTokenType.TYPE,
)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.barfurth.jb.plastic.annotator

import com.intellij.psi.PsiElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.lexer.KtKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtConstantExpression

class KotlinSemanticColoringAnnotator : AbstractSemanticColoringAnnotator() {

override fun isKeyword(element: PsiElement): Boolean {
if (element is LeafPsiElement) {
val type = element.elementType
return type is KtKeywordToken
}
return element is KtConstantExpression
}

override fun getSemanticKeywords(): Map<String, SemanticTokenType> {
return mapOf(
KtTokens.NULL_KEYWORD.value to SemanticTokenType.CONSTANT,
KtTokens.FALSE_KEYWORD.value to SemanticTokenType.CONSTANT,
KtTokens.TRUE_KEYWORD.value to SemanticTokenType.CONSTANT,

KtTokens.VAL_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.VAR_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.CLASS_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.OBJECT_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.ENUM_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.FUN_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.INTERFACE_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.ANNOTATION_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.DATA_KEYWORD.value to SemanticTokenType.STORAGE,
KtTokens.CONST_KEYWORD.value to SemanticTokenType.STORAGE,

KtTokens.GET_KEYWORD.value to SemanticTokenType.FUNCTION,
KtTokens.SET_KEYWORD.value to SemanticTokenType.FUNCTION
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.github.barfurth.jb.plastic.annotator
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey

enum class PlasticTokenType {
CONSTANT, STORAGE, TYPE;
enum class SemanticTokenType {
CONSTANT, STORAGE, TYPE, FUNCTION;

fun getAttributeKey(): TextAttributesKey {
val name = "PLASTIC_${this.name}"
val name = "SEMANTIC_${this.name}"
return TextAttributesKey.createTextAttributesKey(name, DefaultLanguageHighlighterColors.KEYWORD)
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<vendor>barfurth</vendor>

<depends>com.intellij.modules.platform</depends>
<depends optional="true" config-file="coloring-java.xml">com.intellij.java</depends>
<depends optional="true" config-file="coloring-kotlin.xml">org.jetbrains.kotlin</depends>
<depends optional="true" config-file="semantic-java.xml">com.intellij.java</depends>
<depends optional="true" config-file="semantic-kotlin.xml">org.jetbrains.kotlin</depends>

<extensions defaultExtensionNs="com.intellij">
<themeProvider id="com.github.barfurth.jb.plastic" path="/themes/plastic.theme.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<annotator language="JAVA" implementationClass="com.github.barfurth.jb.plastic.annotator.JavaPlasticColoringAnnotator"/>
<annotator language="JAVA" implementationClass="com.github.barfurth.jb.plastic.annotator.JavaSemanticColoringAnnotator"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<annotator language="kotlin" implementationClass="com.github.barfurth.jb.plastic.annotator.KotlinPlasticColoringAnnotator"/>
<annotator language="kotlin" implementationClass="com.github.barfurth.jb.plastic.annotator.KotlinSemanticColoringAnnotator"/>
</extensions>
</idea-plugin>
13 changes: 9 additions & 4 deletions src/main/resources/themes/plastic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,27 @@
<option name="WHITESPACES_MODIFIED_LINES_COLOR" value="d19a66" />
</colors>
<attributes>
<!-- PLASTIC SPECIFIC CONFIG-->
<option name="PLASTIC_CONSTANT">
<!-- SEMANTIC COLORING CONFIG-->
<option name="SEMANTIC_CONSTANT">
<value>
<option name="FOREGROUND" value="56b6c2" />
</value>
</option>
<option name="PLASTIC_STORAGE">
<option name="SEMANTIC_STORAGE">
<value>
<option name="FOREGROUND" value="61afef" />
</value>
</option>
<option name="PLASTIC_TYPE">
<option name="SEMANTIC_TYPE">
<value>
<option name="FOREGROUND" value="e5c07b" />
</value>
</option>
<option name="SEMANTIC_FUNCTION">
<value>
<option name="FOREGROUND" value="b57edc" />
</value>
</option>
<option name="ABSTRACT_CLASS_NAME_ATTRIBUTES">
<value>
<option name="FOREGROUND" value="e5c07b" />
Expand Down

0 comments on commit 7cfc536

Please sign in to comment.