Skip to content

Commit a098da6

Browse files
committed
Update JavaKeywords examples to use multiple colors.
1 parent fab35ff commit a098da6

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,21 @@ public class JavaKeywords extends Application {
3131
"transient", "try", "void", "volatile", "while"
3232
};
3333

34-
private static final Pattern KEYWORD_PATTERN = Pattern.compile("\\b(" + String.join("|", KEYWORDS) + ")\\b");
34+
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
35+
private static final String PAREN_PATTERN = "\\(|\\)";
36+
private static final String BRACE_PATTERN = "\\{|\\}";
37+
private static final String BRACKET_PATTERN = "\\[|\\]";
38+
private static final String SEMICOLON_PATTERN = "\\;";
39+
private static final String STRING_PATTERN = "\"([^\"]|\\\")*\"";
40+
41+
private static final Pattern PATTERN = Pattern.compile(
42+
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
43+
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
44+
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
45+
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
46+
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
47+
+ "|(?<STRING>" + STRING_PATTERN + ")"
48+
);
3549

3650
private static final String sampleCode = String.join("\n", new String[] {
3751
"package com.example;",
@@ -77,13 +91,21 @@ public void start(Stage primaryStage) {
7791
}
7892

7993
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
80-
Matcher matcher = KEYWORD_PATTERN.matcher(text);
94+
Matcher matcher = PATTERN.matcher(text);
8195
int lastKwEnd = 0;
8296
StyleSpansBuilder<Collection<String>> spansBuilder
8397
= new StyleSpansBuilder<>();
8498
while(matcher.find()) {
99+
String styleClass =
100+
matcher.group("KEYWORD") != null ? "keyword" :
101+
matcher.group("PAREN") != null ? "paren" :
102+
matcher.group("BRACE") != null ? "brace" :
103+
matcher.group("BRACKET") != null ? "bracket" :
104+
matcher.group("SEMICOLON") != null ? "semicolon" :
105+
matcher.group("STRING") != null ? "string" :
106+
null; /* never happens */ assert styleClass != null;
85107
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
86-
spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start());
108+
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
87109
lastKwEnd = matcher.end();
88110
}
89111
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);

richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsAsync.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,21 @@ public class JavaKeywordsAsync extends Application {
3636
"transient", "try", "void", "volatile", "while"
3737
};
3838

39-
private static final Pattern KEYWORD_PATTERN = Pattern.compile("\\b(" + String.join("|", KEYWORDS) + ")\\b");
39+
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
40+
private static final String PAREN_PATTERN = "\\(|\\)";
41+
private static final String BRACE_PATTERN = "\\{|\\}";
42+
private static final String BRACKET_PATTERN = "\\[|\\]";
43+
private static final String SEMICOLON_PATTERN = "\\;";
44+
private static final String STRING_PATTERN = "\"([^\"]|\\\")*\"";
45+
46+
private static final Pattern PATTERN = Pattern.compile(
47+
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
48+
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
49+
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
50+
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
51+
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
52+
+ "|(?<STRING>" + STRING_PATTERN + ")"
53+
);
4054

4155
private static final String sampleCode = String.join("\n", new String[] {
4256
"package com.example;",
@@ -107,13 +121,21 @@ private void applyHighlighting(StyleSpans<Collection<String>> highlighting) {
107121
}
108122

109123
private static StyleSpans<Collection<String>> computeHighlighting(String text) {
110-
Matcher matcher = KEYWORD_PATTERN.matcher(text);
124+
Matcher matcher = PATTERN.matcher(text);
111125
int lastKwEnd = 0;
112126
StyleSpansBuilder<Collection<String>> spansBuilder
113127
= new StyleSpansBuilder<>();
114128
while(matcher.find()) {
129+
String styleClass =
130+
matcher.group("KEYWORD") != null ? "keyword" :
131+
matcher.group("PAREN") != null ? "paren" :
132+
matcher.group("BRACE") != null ? "brace" :
133+
matcher.group("BRACKET") != null ? "bracket" :
134+
matcher.group("SEMICOLON") != null ? "semicolon" :
135+
matcher.group("STRING") != null ? "string" :
136+
null; /* never happens */ assert styleClass != null;
115137
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
116-
spansBuilder.add(Collections.singleton("keyword"), matcher.end() - matcher.start());
138+
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
117139
lastKwEnd = matcher.end();
118140
}
119141
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);

richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/java-keywords.css

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@
22
-fx-fill: #8a6;
33
-fx-font-weight: bold;
44
}
5-
.default {
6-
-fx-fill: #666;
5+
.semicolon {
6+
-fx-font-weight: bold;
7+
}
8+
.paren {
9+
-fx-fill: darkblue;
10+
-fx-font-weight: bold;
11+
}
12+
.bracket {
13+
-fx-fill: darkgreen;
14+
-fx-font-weight: bold;
715
}
16+
.brace {
17+
-fx-fill: darkviolet;
18+
-fx-font-weight: bold;
19+
}
20+
.string {
21+
-fx-fill: blue;
22+
}
23+
824
.lineno {
925
-fx-font-family: monospace;
1026
-fx-background-color: #eee;
1127
-fx-text-fill: #666;
1228
-fx-padding: 0 5px;
1329
}
1430
.code-area {
15-
-fx-font-family: monospace;
1631
-fx-background-color: #eee;
1732
}

0 commit comments

Comments
 (0)