commonMain.piacenti.dslmaker.dsl.FormatterAndHighlighter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-maker-js Show documentation
Show all versions of dsl-maker-js Show documentation
Kotlin multiplatform library to facilitate creation of DSLs with ANTLR or a simple built in parser
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