All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.piacenti.dslmaker.dsl.FormatterAndHighlighter.kt Maven / Gradle / Ivy

Go to download

Kotlin multiplatform library to facilitate creation of DSLs with ANTLR or a simple built in parser

There is a newer version: 1.1.55
Show newest version
package piacenti.dslmaker.dsl

import kotlinx.serialization.Serializable
import piacenti.dslmaker.GenericParser2
import piacenti.dslmaker.combine

@Serializable
data class FormatterAndHighlighterResult(val combined: String, val formatted: String)
class GrammarBasedFormatterAndHighlighter {
    fun applyAll(text: String, formatter: Formatter, highlighter: Highlighter,
                 providedParser: GenericParser2<*>? = null): FormatterAndHighlighterResult {
        val preClean = formatter.preClean(text)
        val astNode = providedParser?.parse(preClean, true)
        val tabText = formatter.getTabText()
        val formatIndexes = formatter.getFormatIndexes(astNode, preClean, tabText)
        val formatted = formatter.postClean(TextModifier.applyModificationsMap(preClean, formatIndexes))
        val computeHighlighting = highlighter.computeHighlighting(preClean, astNode, providedParser)
        val combined = formatIndexes.combine(computeHighlighting)
        val appliedChanges = TextModifier.applyModificationsMap(preClean, combined)
        val result = formatter.postClean(appliedChanges)
        return FormatterAndHighlighterResult(combined = result, formatted = formatted)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy