Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.jetbrains.kotlin.doc.highlighter
import java.util.HashMap
import kotlin.template.HtmlFormatter
import com.intellij.psi.*
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import org.jetbrains.jet.lexer.*
fun main(args: Array) {
val tool = SyntaxHighligher()
val answer = tool.highlight(""" val x = arrayList(1, 2, 3)
println("hello")""")
println(answer)
}
/**
* Syntax highlights Kotlin code
*/
class SyntaxHighligher() {
var formatter: HtmlFormatter = HtmlFormatter()
val styleMap = createStyleMap()
/** Highlights the given kotlin code as HTML */
fun highlight(code: String): String {
try {
val builder = StringBuilder()
builder.append(
"
" +
"
" +
"
"
)
// lets add the leading whitespace first
var idx = 0
while (Character.isWhitespace(code[idx])) {
idx++
}
if (idx > 0) {
val space = code.substring(0, idx)
builder.append("""$space""")
}
val lexer = JetLexer()
lexer.start(code)
val end = lexer.getTokenEnd()
while (true) {
lexer.advance()
val token = lexer.getTokenType()
if (token == null) break
val tokenText = lexer.getTokenSequence().toString().replaceAll("\n", "\r\n")
var style: String? = null
if (token is JetKeywordToken) {
style = "keyword"
} else if (token == JetTokens.IDENTIFIER) {
val types = JetTokens.SOFT_KEYWORDS.getTypes()
if (types != null) {
for (softKeyword in types) {
if (softKeyword is JetKeywordToken) {
if (softKeyword.getValue().equals(tokenText)) {
style = "softkeyword"
break
}
}
}
}
style = if (style == null) "plain" else style
} else if (styleMap.containsKey(token)) {
style = styleMap.get(token)
if (style == null) {
println("Warning: No style for token $token")
}
} else {
style = "plain"
}
builder.append("""""")
formatter.format(builder, tokenText)
builder.append("")
}
builder.append("