[Editor] Improve Syntax Highlighting

This commit is contained in:
2021-02-23 19:56:16 +01:00
parent a9c3aa0c1a
commit 953ca10676
2 changed files with 38 additions and 12 deletions

View File

@@ -16,6 +16,9 @@ class JavaSyntaxHighlighter : SyntaxHighlighter {
result.groups["BRACKET"] != null -> "bracket" result.groups["BRACKET"] != null -> "bracket"
result.groups["SEMICOLON"] != null -> "semicolon" result.groups["SEMICOLON"] != null -> "semicolon"
result.groups["STRING"] != null -> "string" result.groups["STRING"] != null -> "string"
result.groups["NUMBER"] != null -> "number"
result.groups["ANNOTATION"] != null -> "annotation"
result.groups["OPERATOR"] != null -> "operator"
result.groups["COMMENT"] != null -> "comment" result.groups["COMMENT"] != null -> "comment"
else -> throw IllegalStateException("Unsupported regex group") else -> throw IllegalStateException("Unsupported regex group")
} }
@@ -44,28 +47,34 @@ class JavaSyntaxHighlighter : SyntaxHighlighter {
"new", "package", "private", "protected", "public", "new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while" "transient", "try", "void", "volatile", "while", "null"
) )
private val KEYWORD_PATTERN = "\\b(" + KEYWORDS.joinToString("|") + ")\\b" private val KEYWORD_PATTERN = "\\b(" + KEYWORDS.joinToString("|") + ")\\b"
private val ANNOTATION_PATTERN = "@\\w+"
private val PAREN_PATTERN = "\\(|\\)" private val PAREN_PATTERN = "\\(|\\)"
private val BRACE_PATTERN = "\\{|\\}" private val BRACE_PATTERN = "\\{|\\}"
private val BRACKET_PATTERN = "\\[|\\]" private val BRACKET_PATTERN = "\\[|\\]"
private val SEMICOLON_PATTERN = "\\;" private val SEMICOLON_PATTERN = "\\;"
private val STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"" private val STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\""
private val NUMBER_PATTERN = "\\b-?([0-9]*\\.[0-9]+|[0-9]+)\\w?\\b"
private val OPERATOR_PATTERN = "[+-/*:|&?<>=!]"
private val COMMENT_PATTERN = """ private val COMMENT_PATTERN = """
//[^ //[^
]*|/\*(.|\R)*?\*/ ]*|/\*(.|\R)*?\*/
""".trimIndent() """.trimIndent()
private val PATTERN = ( private val PATTERN = (
"(?<KEYWORD>" + KEYWORD_PATTERN + ")" "(?<KEYWORD>$KEYWORD_PATTERN)"
+ "|(?<PAREN>" + PAREN_PATTERN + ")" + "|(?<ANNOTATION>$ANNOTATION_PATTERN)"
+ "|(?<BRACE>" + BRACE_PATTERN + ")" + "|(?<PAREN>$PAREN_PATTERN)"
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")" + "|(?<BRACE>$BRACE_PATTERN)"
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")" + "|(?<BRACKET>$BRACKET_PATTERN)"
+ "|(?<STRING>" + STRING_PATTERN + ")" + "|(?<SEMICOLON>$SEMICOLON_PATTERN)"
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")" + "|(?<STRING>$STRING_PATTERN)"
+ "|(?<NUMBER>$NUMBER_PATTERN)"
+ "|(?<OPERATOR>$OPERATOR_PATTERN)"
+ "|(?<COMMENT>$COMMENT_PATTERN)"
).toRegex() ).toRegex()
} }
} }

View File

@@ -11,6 +11,10 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
val bracket by cssclass() val bracket by cssclass()
val brace by cssclass() val brace by cssclass()
val string by cssclass() val string by cssclass()
val number by cssclass()
val annotation by cssclass()
val operator by cssclass()
val field by cssclass()
val comment by cssclass() val comment by cssclass()
val paragraphBox by cssclass() val paragraphBox by cssclass()
val paragraphText by cssclass() val paragraphText by cssclass()
@@ -22,7 +26,7 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
init { init {
keyword { keyword {
fill = c("purple") fill = c("#000080")
fontWeight = FontWeight.BOLD fontWeight = FontWeight.BOLD
} }
@@ -31,7 +35,7 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
} }
paren { paren {
fill = c("firebrick") fill = c("cadetblue")
fontWeight = FontWeight.BOLD fontWeight = FontWeight.BOLD
} }
@@ -46,11 +50,24 @@ class JavaSyntaxHighlightingStylesheet : Stylesheet() {
} }
string { string {
fill = c("blue") fill = c("#008000")
}
number {
fill = c("#0000FF")
}
operator {
fill = c("#CC7832")
}
annotation {
fill = c("#BBB529")
fontWeight = FontWeight.BOLD
} }
comment { comment {
fill = c("cadetblue") fill = c("#808080")
} }
paragraphBox and hasCaret { paragraphBox and hasCaret {