Skip to content

Commit cf6a90c

Browse files
committed
updated API of code component.
1 parent 19e7d0c commit cf6a90c

File tree

4 files changed

+262
-135
lines changed

4 files changed

+262
-135
lines changed

src/main/java/com/ss/editor/ui/control/code/BaseCodeArea.java

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.ss.editor.ui.control.code;
22

3+
import static java.util.Collections.singleton;
34
import com.ss.editor.annotation.FXThread;
5+
import com.ss.rlib.util.StringUtils;
46
import org.fxmisc.richtext.CodeArea;
57
import org.fxmisc.richtext.model.StyleSpans;
8+
import org.fxmisc.richtext.model.StyleSpansBuilder;
69
import org.fxmisc.undo.UndoManager;
710
import org.jetbrains.annotations.NotNull;
811

912
import java.util.Collection;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
1015

1116
/**
1217
* The base implementation of code area control for this editor.
@@ -15,11 +20,104 @@
1520
*/
1621
public class BaseCodeArea extends CodeArea {
1722

23+
@NotNull
24+
protected static final String CLASS_KEYWORD = "KEYWORD";
25+
26+
@NotNull
27+
protected static final String CLASS_VALUE_TYPE = "VALUETYPE";
28+
29+
@NotNull
30+
protected static final String CLASS_VALUE_VALUE = "VALUEVALUE";
31+
32+
@NotNull
33+
protected static final String CLASS_PAREN = "PAREN";
34+
35+
@NotNull
36+
protected static final String CLASS_BRACE = "BRACE";
37+
38+
@NotNull
39+
protected static final String CLASS_BRACKET = "BRACKET";
40+
41+
@NotNull
42+
protected static final String CLASS_SEMICOLON = "SEMICOLON";
43+
44+
@NotNull
45+
protected static final String CLASS_STRING = "STRING";
46+
47+
@NotNull
48+
protected static final String CLASS_COMMENT = "COMMENT";
49+
50+
@NotNull
51+
protected static final String[][] AVAILABLE_CLASSES = {
52+
{CLASS_KEYWORD, "keyword"},
53+
{CLASS_VALUE_TYPE, "value-type"},
54+
{CLASS_VALUE_VALUE, "value-value"},
55+
{CLASS_PAREN, "paren"},
56+
{CLASS_BRACE, "brace"},
57+
{CLASS_BRACKET, "bracket"},
58+
{CLASS_SEMICOLON, "semicolon"},
59+
{CLASS_STRING, "string"},
60+
{CLASS_COMMENT, "comment"},
61+
};
62+
63+
protected static final String PAREN_PATTERN = "\\(|\\)";
64+
protected static final String BRACE_PATTERN = "\\{|\\}";
65+
protected static final String BRACKET_PATTERN = "\\[|\\]";
66+
protected static final String SEMICOLON_PATTERN = "\\;";
67+
protected static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
68+
protected static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
69+
1870
public BaseCodeArea() {
1971
richChanges().filter(ch -> !ch.getInserted().equals(ch.getRemoved()))
2072
.subscribe(change -> setStyleSpans(0, calculateStyleSpans(getText())));
2173
}
2274

75+
/**
76+
* Calculate highlighting for the code.
77+
*
78+
* @param pattern the pattern.
79+
* @param text the text.
80+
* @return the highlight styles.
81+
*/
82+
@FXThread
83+
protected @NotNull StyleSpans<Collection<String>> computeHighlighting(@NotNull final Pattern pattern,
84+
@NotNull final String text) {
85+
86+
final Matcher matcher = pattern.matcher(text);
87+
final StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
88+
89+
int lastKwEnd = 0;
90+
91+
while (matcher.find()) {
92+
93+
String styleClass = null;
94+
95+
for (final String[] availableClass : AVAILABLE_CLASSES) {
96+
97+
try {
98+
styleClass = matcher.group(availableClass[0]) != null ? availableClass[1] : null;
99+
} catch (final IllegalArgumentException e) {
100+
continue;
101+
}
102+
103+
if(styleClass != null) {
104+
break;
105+
}
106+
}
107+
108+
assert styleClass != null;
109+
110+
spansBuilder.add(singleton("plain-code"), matcher.start() - lastKwEnd);
111+
spansBuilder.add(singleton(styleClass), matcher.end() - matcher.start());
112+
113+
lastKwEnd = matcher.end();
114+
}
115+
116+
spansBuilder.add(singleton("plain-code"), text.length() - lastKwEnd);
117+
118+
return spansBuilder.create();
119+
}
120+
23121
/**
24122
* Gets style spans.
25123
*
@@ -31,6 +129,11 @@ public BaseCodeArea() {
31129
throw new RuntimeException("unsupported");
32130
}
33131

132+
/**
133+
* Load the content.
134+
*
135+
* @param content the content.
136+
*/
34137
@FXThread
35138
public void loadContent(@NotNull final String content) {
36139
appendText(content);
@@ -39,9 +142,38 @@ public void loadContent(@NotNull final String content) {
39142
undoManager.forgetHistory();
40143
}
41144

145+
/**
146+
* Reload the content.
147+
*
148+
* @param content the content.
149+
*/
42150
@FXThread
43151
public void reloadContent(@NotNull final String content) {
152+
reloadContent(content, false);
153+
}
154+
155+
/**
156+
* Reload the content.
157+
*
158+
* @param content the content.
159+
* @param clearHistory true if need to clear history.
160+
*/
161+
@FXThread
162+
public void reloadContent(@NotNull final String content, final boolean clearHistory) {
163+
44164
final String currentContent = getText();
45-
replaceText(0, currentContent.length(), content);
165+
166+
if (!StringUtils.equals(currentContent, content)) {
167+
if (content.isEmpty()) {
168+
deleteText(0, currentContent.length());
169+
} else {
170+
replaceText(0, currentContent.length(), content);
171+
}
172+
}
173+
174+
if (clearHistory) {
175+
final UndoManager undoManager = getUndoManager();
176+
undoManager.forgetHistory();
177+
}
46178
}
47179
}
Lines changed: 55 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.ss.editor.ui.control.code;
22

3-
import static java.util.Collections.singleton;
43
import com.ss.editor.annotation.FXThread;
4+
import com.ss.editor.util.GLSLType;
55
import org.fxmisc.richtext.model.StyleSpans;
6-
import org.fxmisc.richtext.model.StyleSpansBuilder;
76
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
88

9+
import java.util.ArrayList;
10+
import java.util.Arrays;
911
import java.util.Collection;
10-
import java.util.regex.Matcher;
12+
import java.util.List;
1113
import java.util.regex.Pattern;
1214

1315
/**
@@ -30,87 +32,73 @@ public class GLSLCodeArea extends BaseCodeArea {
3032
};
3133

3234
@NotNull
33-
private static final String[] VALUE_TYPES = {
34-
"float", "double", "int", "bool", "mat2", "mat3", "mat4", "uint", "uvec2", "uvec3", "uvec4",
35-
"sampler1D", "sampler2D", "sampler3D", "samplerCube", "vec2", "vec3", "vec4"
36-
};
35+
private static final String[] VALUE_TYPES;
36+
37+
static {
38+
VALUE_TYPES = Arrays.stream(GLSLType.VALUES)
39+
.map(GLSLType::getRawType)
40+
.toArray(String[]::new);
41+
}
3742

3843
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
3944
private static final String VALUE_TYPE_PATTERN = "\\b(" + String.join("|", VALUE_TYPES) + ")\\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-
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
46-
47-
private static final Pattern PATTERN = Pattern.compile(
48-
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
49-
+ "|(?<VALUETYPE>" + VALUE_TYPE_PATTERN + ")"
50-
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
51-
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
52-
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
53-
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
54-
+ "|(?<STRING>" + STRING_PATTERN + ")"
55-
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
56-
);
57-
58-
@FXThread
59-
private static @NotNull StyleSpans<Collection<String>> computeHighlighting(@NotNull final String text) {
60-
61-
final Matcher matcher = PATTERN.matcher(text);
62-
final StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
63-
64-
int lastKwEnd = 0;
65-
66-
while (matcher.find()) {
67-
68-
String styleClass = matcher.group("KEYWORD") != null ? "keyword" : null;
6945

70-
if (styleClass == null) {
71-
styleClass = matcher.group("VALUETYPE") != null ? "value-type" : null;
72-
}
73-
74-
if (styleClass == null) {
75-
styleClass = matcher.group("PAREN") != null ? "paren" : null;
76-
}
77-
78-
if (styleClass == null) {
79-
styleClass = matcher.group("BRACE") != null ? "brace" : null;
80-
}
81-
82-
if (styleClass == null) {
83-
styleClass = matcher.group("BRACKET") != null ? "bracket" : null;
84-
}
46+
/**
47+
* The available fields.
48+
*/
49+
@NotNull
50+
private final List<String> fields;
8551

86-
if (styleClass == null) {
87-
styleClass = matcher.group("SEMICOLON") != null ? "semicolon" : null;
88-
}
52+
/**
53+
* THe pattern.
54+
*/
55+
@NotNull
56+
private Pattern pattern;
8957

90-
if (styleClass == null) {
91-
styleClass = matcher.group("STRING") != null ? "string" : null;
92-
}
58+
public GLSLCodeArea() {
59+
this.pattern = buildPattern();
60+
this.fields = new ArrayList<>();
61+
}
9362

94-
if (styleClass == null) {
95-
styleClass = matcher.group("COMMENT") != null ? "comment" : null;
96-
}
63+
/**
64+
* Get available fields.
65+
*
66+
* @return available fields.
67+
*/
68+
@FXThread
69+
private @Nullable List<String> getFields() {
70+
return fields;
71+
}
9772

98-
assert styleClass != null;
73+
/**
74+
* Build the pattern to highlight code.
75+
*
76+
* @return the pattern to highlight code.
77+
*/
78+
@FXThread
79+
private @NotNull Pattern buildPattern() {
9980

100-
spansBuilder.add(singleton("plain-code"), matcher.start() - lastKwEnd);
101-
spansBuilder.add(singleton(styleClass), matcher.end() - matcher.start());
81+
String result = "(?<" + CLASS_KEYWORD + ">" + KEYWORD_PATTERN + ")" +
82+
"|(?<" + CLASS_VALUE_TYPE + ">" + VALUE_TYPE_PATTERN + ")";
10283

103-
lastKwEnd = matcher.end();
84+
final List<String> fields = getFields();
85+
if (fields != null && !fields.isEmpty()) {
86+
result += "|(?<" + CLASS_VALUE_VALUE + ">" + "\\b(" + String.join("|", fields) + ")\\b";
10487
}
10588

106-
spansBuilder.add(singleton("plain-code"), text.length() - lastKwEnd);
89+
result += "|(?<" + CLASS_PAREN + ">" + PAREN_PATTERN + ")" +
90+
"|(?<" + CLASS_BRACE + ">" + BRACE_PATTERN + ")" +
91+
"|(?<" + CLASS_BRACKET + ">" + BRACKET_PATTERN + ")" +
92+
"|(?<" + CLASS_SEMICOLON + ">" + SEMICOLON_PATTERN + ")" +
93+
"|(?<" + CLASS_STRING + ">" + STRING_PATTERN + ")" +
94+
"|(?<" + CLASS_COMMENT + ">" + COMMENT_PATTERN + ")";
10795

108-
return spansBuilder.create();
96+
return Pattern.compile(result);
10997
}
11098

11199
@Override
112100
@FXThread
113101
protected @NotNull StyleSpans<? extends Collection<String>> calculateStyleSpans(@NotNull final String text) {
114-
return computeHighlighting(text);
102+
return computeHighlighting(pattern, text);
115103
}
116104
}

0 commit comments

Comments
 (0)