idea-plugin / arrow.meta.ide.dsl.editor.syntaxHighlighter / SyntaxHighlighterSyntax / addSyntaxHighlighter

addSyntaxHighlighter

open fun IdeMetaPlugin.addSyntaxHighlighter(highlightingLexer: Lexer, language: Language, tokenHighlights: (tokenType: IElementType) -> Array<TextAttributesKey>): ExtensionPhase

Parameters

tokenHighlights -

specifies the generated tokens from the lexer that need to be highlighted The following example registers a sample amount of the generated Tokens from the KotlinLexer.

import arrow.meta.Plugin
import arrow.meta.ide.IdeMetaPlugin
import arrow.meta.invoke
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.psi.tree.IElementType
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingLexer
import org.jetbrains.kotlin.lexer.KtTokens

//sampleStart
val IdeMetaPlugin.expressionHints: Plugin
 get() = "MiniKotlinHighlighter" {
  meta(
   addSyntaxHighlighter(
    highlightingLexer = KotlinHighlightingLexer(),
    language = KotlinLanguage.INSTANCE,
    tokenHighlights = { token ->
     tokenToHighlighting[token]?.let { arrayOf(it) } ?: emptyArray()
    }
   )
  )
 }
//sampleEnd
val tokenToHighlighting: Map<in IElementType, TextAttributesKey>
  get() = mapOf(
    KtTokens.KEYWORDS to DefaultLanguageHighlighterColors.KEYWORD,
    KtTokens.`AS_SAFE` to DefaultLanguageHighlighterColors.KEYWORD,
    KtTokens.INTEGER_LITERAL to DefaultLanguageHighlighterColors.NUMBER,
    KtTokens.FLOAT_LITERAL to DefaultLanguageHighlighterColors.NUMBER,
    KtTokens.DOC_COMMENT to DefaultLanguageHighlighterColors.DOC_COMMENT,
    KtTokens.ARROW to DefaultLanguageHighlighterColors.PARENTHESES,
    KtTokens.LPAR to DefaultLanguageHighlighterColors.PARENTHESES,
    KtTokens.RPAR to DefaultLanguageHighlighterColors.PARENTHESES
    //... and more tokens
  )

In fact, the latter is a minimal example from the existing KotlinHighlighter.

See Also

SyntaxHighlighterSyntax

syntaxHighlighterFactory

KotlinHighlighter

Do you like Arrow?

Arrow Org
<