Skip to content

Commit bc70d48

Browse files
committed
separated code area components.
1 parent 20c1e70 commit bc70d48

File tree

6 files changed

+325
-232
lines changed

6 files changed

+325
-232
lines changed

src/main/java/com/ss/editor/ui/component/editor/impl/CodeAreaFileEditor.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33
import static com.ss.rlib.util.ObjectUtils.notNull;
44
import com.ss.editor.annotation.BackgroundThread;
55
import com.ss.editor.annotation.FXThread;
6+
import com.ss.editor.ui.control.code.BaseCodeArea;
67
import com.ss.editor.ui.css.CSSClasses;
78
import com.ss.rlib.ui.util.FXUtils;
89
import com.ss.rlib.util.FileUtils;
910
import javafx.scene.layout.HBox;
1011
import javafx.scene.layout.VBox;
1112
import org.fxmisc.richtext.CodeArea;
12-
import org.fxmisc.richtext.model.StyleSpans;
13-
import org.fxmisc.undo.UndoManager;
1413
import org.jetbrains.annotations.NotNull;
1514
import org.jetbrains.annotations.Nullable;
1615

1716
import java.io.IOException;
1817
import java.io.PrintWriter;
1918
import java.nio.file.Files;
2019
import java.nio.file.Path;
21-
import java.util.Collection;
2220

2321
/**
2422
* The implementation of editor to edit files with code.
@@ -37,7 +35,7 @@ public abstract class CodeAreaFileEditor extends AbstractFileEditor<VBox> {
3735
* The code area.
3836
*/
3937
@Nullable
40-
private CodeArea codeArea;
38+
private BaseCodeArea codeArea;
4139

4240
@Override
4341
@FXThread
@@ -49,9 +47,7 @@ public abstract class CodeAreaFileEditor extends AbstractFileEditor<VBox> {
4947
@FXThread
5048
protected void createContent(@NotNull final VBox root) {
5149

52-
codeArea = new CodeArea();
53-
codeArea.richChanges().filter(ch -> !ch.getInserted().equals(ch.getRemoved()))
54-
.subscribe(change -> codeArea.setStyleSpans(0, getStyleSpans(codeArea.getText())));
50+
codeArea = createCodeArea();
5551
codeArea.textProperty().addListener((observable, oldValue, newValue) -> updateDirty(newValue));
5652
codeArea.prefHeightProperty().bind(root.heightProperty());
5753
codeArea.prefWidthProperty().bind(root.widthProperty());
@@ -61,14 +57,13 @@ protected void createContent(@NotNull final VBox root) {
6157
}
6258

6359
/**
64-
* Gets style spans.
60+
* Create the code area.
6561
*
66-
* @param text the text
67-
* @return the style spans
62+
* @return the code area.
6863
*/
6964
@FXThread
70-
protected @NotNull StyleSpans<? extends Collection<String>> getStyleSpans(@NotNull final String text) {
71-
throw new RuntimeException("unsupported");
65+
protected @NotNull BaseCodeArea createCodeArea() {
66+
throw new RuntimeException();
7267
}
7368

7469
/**
@@ -96,7 +91,7 @@ protected void createToolbar(@NotNull final HBox container) {
9691
* @return the code area.
9792
*/
9893
@FXThread
99-
private @NotNull CodeArea getCodeArea() {
94+
private @NotNull BaseCodeArea getCodeArea() {
10095
return notNull(codeArea);
10196
}
10297

@@ -107,11 +102,8 @@ public void openFile(@NotNull final Path file) {
107102

108103
setOriginalContent(FileUtils.read(file));
109104

110-
final CodeArea codeArea = getCodeArea();
111-
codeArea.appendText(getOriginalContent());
112-
113-
final UndoManager undoManager = codeArea.getUndoManager();
114-
undoManager.forgetHistory();
105+
final BaseCodeArea codeArea = getCodeArea();
106+
codeArea.loadContent(getOriginalContent());
115107

116108
setOriginalContent(codeArea.getText());
117109
updateDirty(getOriginalContent());
@@ -165,9 +157,9 @@ protected void handleExternalChanges() {
165157

166158
final String newContent = FileUtils.read(getEditFile());
167159

168-
final CodeArea codeArea = getCodeArea();
160+
final BaseCodeArea codeArea = getCodeArea();
169161
final String currentContent = codeArea.getText();
170-
codeArea.replaceText(0, currentContent.length(), newContent);
162+
codeArea.reloadContent(newContent);
171163

172164
setOriginalContent(currentContent);
173165
updateDirty(newContent);

src/main/java/com/ss/editor/ui/component/editor/impl/GLSLFileEditor.java

Lines changed: 5 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package com.ss.editor.ui.component.editor.impl;
22

3-
import static java.util.Collections.singleton;
43
import com.ss.editor.FileExtensions;
54
import com.ss.editor.Messages;
65
import com.ss.editor.annotation.FXThread;
76
import com.ss.editor.annotation.FromAnyThread;
87
import com.ss.editor.ui.component.editor.EditorDescription;
9-
import org.fxmisc.richtext.model.StyleSpans;
10-
import org.fxmisc.richtext.model.StyleSpansBuilder;
8+
import com.ss.editor.ui.control.code.BaseCodeArea;
9+
import com.ss.editor.ui.control.code.GLSLCodeArea;
1110
import org.jetbrains.annotations.NotNull;
1211

13-
import java.util.Collection;
14-
import java.util.regex.Matcher;
15-
import java.util.regex.Pattern;
16-
1712
/**
1813
* The implementation of editor to edit GLSL files.
1914
*
@@ -34,101 +29,10 @@ public class GLSLFileEditor extends CodeAreaFileEditor {
3429
DESCRIPTION.setExtensions(FileExtensions.SHADER_EXTENSIONS);
3530
}
3631

37-
@NotNull
38-
private static final String[] KEYWORDS = {
39-
"define", "undef", "if", "ifdef", "ifndef",
40-
"else", "elif", "endif", "error", "pragma",
41-
"extension", "version", "line", "attribute", "const",
42-
"uniform", "varying", "layout", "centroid", "flat",
43-
"smooth", "noperspective", "patch", "sample", "break",
44-
"continue", "do", "for", "while", "switch",
45-
"case", "default", "if", "subroutine", "in", "out", "inout",
46-
"void", "true", "false", "invariant", "discard", "return", "struct"
47-
};
48-
49-
@NotNull
50-
private static final String[] VALUE_TYPES = {
51-
"float", "double", "int", "bool", "mat2", "mat3", "mat4", "uint", "uvec2", "uvec3", "uvec4",
52-
"sampler1D", "sampler2D", "sampler3D", "samplerCube", "vec2", "vec3", "vec4"
53-
};
54-
55-
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
56-
private static final String VALUE_TYPE_PATTERN = "\\b(" + String.join("|", VALUE_TYPES) + ")\\b";
57-
private static final String PAREN_PATTERN = "\\(|\\)";
58-
private static final String BRACE_PATTERN = "\\{|\\}";
59-
private static final String BRACKET_PATTERN = "\\[|\\]";
60-
private static final String SEMICOLON_PATTERN = "\\;";
61-
private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
62-
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
63-
64-
private static final Pattern PATTERN = Pattern.compile(
65-
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
66-
+ "|(?<VALUETYPE>" + VALUE_TYPE_PATTERN + ")"
67-
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
68-
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
69-
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
70-
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
71-
+ "|(?<STRING>" + STRING_PATTERN + ")"
72-
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
73-
);
74-
75-
@NotNull
76-
private static StyleSpans<Collection<String>> computeHighlighting(@NotNull final String text) {
77-
78-
final Matcher matcher = PATTERN.matcher(text);
79-
final StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
80-
81-
int lastKwEnd = 0;
82-
83-
while (matcher.find()) {
84-
85-
String styleClass = matcher.group("KEYWORD") != null ? "keyword" : null;
86-
87-
if (styleClass == null) {
88-
styleClass = matcher.group("VALUETYPE") != null ? "value-type" : null;
89-
}
90-
91-
if (styleClass == null) {
92-
styleClass = matcher.group("PAREN") != null ? "paren" : null;
93-
}
94-
95-
if (styleClass == null) {
96-
styleClass = matcher.group("BRACE") != null ? "brace" : null;
97-
}
98-
99-
if (styleClass == null) {
100-
styleClass = matcher.group("BRACKET") != null ? "bracket" : null;
101-
}
102-
103-
if (styleClass == null) {
104-
styleClass = matcher.group("SEMICOLON") != null ? "semicolon" : null;
105-
}
106-
107-
if (styleClass == null) {
108-
styleClass = matcher.group("STRING") != null ? "string" : null;
109-
}
110-
111-
if (styleClass == null) {
112-
styleClass = matcher.group("COMMENT") != null ? "comment" : null;
113-
}
114-
115-
assert styleClass != null;
116-
117-
spansBuilder.add(singleton("plain-code"), matcher.start() - lastKwEnd);
118-
spansBuilder.add(singleton(styleClass), matcher.end() - matcher.start());
119-
120-
lastKwEnd = matcher.end();
121-
}
122-
123-
spansBuilder.add(singleton("plain-code"), text.length() - lastKwEnd);
124-
125-
return spansBuilder.create();
126-
}
127-
128-
@FXThread
12932
@Override
130-
protected @NotNull StyleSpans<? extends Collection<String>> getStyleSpans(@NotNull final String text) {
131-
return computeHighlighting(text);
33+
@FXThread
34+
protected @NotNull BaseCodeArea createCodeArea() {
35+
return new GLSLCodeArea();
13236
}
13337

13438
@Override

src/main/java/com/ss/editor/ui/component/editor/impl/MaterialDefinitionFileEditor.java

Lines changed: 4 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
package com.ss.editor.ui.component.editor.impl;
22

3-
import static java.util.Collections.singleton;
43
import com.ss.editor.FileExtensions;
54
import com.ss.editor.Messages;
65
import com.ss.editor.annotation.FXThread;
76
import com.ss.editor.annotation.FromAnyThread;
87
import com.ss.editor.ui.component.editor.EditorDescription;
9-
import org.fxmisc.richtext.model.StyleSpans;
10-
import org.fxmisc.richtext.model.StyleSpansBuilder;
8+
import com.ss.editor.ui.control.code.BaseCodeArea;
9+
import com.ss.editor.ui.control.code.MaterialDefinitionCodeArea;
1110
import org.jetbrains.annotations.NotNull;
1211

13-
import java.util.Collection;
14-
import java.util.regex.Matcher;
15-
import java.util.regex.Pattern;
16-
1712
/**
1813
* The implementation of editor to edit material definition files.
1914
*
@@ -34,112 +29,10 @@ public class MaterialDefinitionFileEditor extends CodeAreaFileEditor {
3429
DESCRIPTION.addExtension(FileExtensions.JME_MATERIAL_DEFINITION);
3530
}
3631

37-
@NotNull
38-
private static final String[] KEYWORDS = {
39-
"MaterialDef", "MaterialParameters", "Technique", "WorldParameters", "Defines", "ForcedRenderState"
40-
};
41-
42-
@NotNull
43-
private static final String[] VALUE_TYPES = {
44-
"Texture2D", "Float", "Boolean", "Int", "Color", "Vector3", "TextureCubeMap", "Matrix4", "Vector4", "Vector2",
45-
"VertexShader", "TessellationEvaluationShader ", "TessellationControlShader", "FragmentShader", "LightMode",
46-
"WorldViewProjectionMatrix", "Time", "NormalMatrix", "WorldViewMatrix",
47-
"ViewMatrix", "CameraPosition", "WorldMatrix", "FaceCull", "DepthTest", "DepthWrite", "PolyOffset",
48-
"ColorWrite", "Blend", "Resolution", "FragmentShader", "ViewProjectionMatrix",
49-
"IntArray", "FloatArray", "Vector2Array", "Vector3Array", "Vector4Array", "Matrix3",
50-
"Matrix3Array", "Matrix4Array", "TextureBuffer", "Texture3D", "TextureArray", "GeometryShader"
51-
};
52-
53-
@NotNull
54-
private static final String[] VALUE_VALUES = {
55-
"true", "false", "Off", "On", "True", "False", "Disable", "SinglePass", "MultiPass",
56-
"SinglePassAndImageBased", "FixedPipeline", "StaticPass", "InPass", "PostPass", "World", "View",
57-
"Legacy", "GLSL100", "GLSL110", "GLSL120", "GLSL130", "GLSL140", "GLSL150", "GLSL400", "GLSL330",
58-
"GLSL410", "GLSL420", "GLSL430", "GLSL440", "GLSL450",
59-
};
60-
61-
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
62-
private static final String VALUE_TYPE_PATTERN = "\\b(" + String.join("|", VALUE_TYPES) + ")\\b";
63-
private static final String VALUE_VALUE_PATTERN = "\\b(" + String.join("|", VALUE_VALUES) + ")\\b";
64-
private static final String PAREN_PATTERN = "\\(|\\)";
65-
private static final String BRACE_PATTERN = "\\{|\\}";
66-
private static final String BRACKET_PATTERN = "\\[|\\]";
67-
private static final String SEMICOLON_PATTERN = "\\;";
68-
private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
69-
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
70-
71-
private static final Pattern PATTERN = Pattern.compile(
72-
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
73-
+ "|(?<VALUETYPE>" + VALUE_TYPE_PATTERN + ")"
74-
+ "|(?<VALUEVALUE>" + VALUE_VALUE_PATTERN + ")"
75-
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
76-
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
77-
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
78-
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
79-
+ "|(?<STRING>" + STRING_PATTERN + ")"
80-
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
81-
);
82-
83-
private static StyleSpans<Collection<String>> computeHighlighting(final String text) {
84-
85-
final Matcher matcher = PATTERN.matcher(text);
86-
final StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
87-
88-
int lastKwEnd = 0;
89-
90-
while (matcher.find()) {
91-
92-
String styleClass = matcher.group("KEYWORD") != null ? "keyword" : null;
93-
94-
if (styleClass == null) {
95-
styleClass = matcher.group("VALUETYPE") != null ? "value-type" : null;
96-
}
97-
98-
if (styleClass == null) {
99-
styleClass = matcher.group("VALUEVALUE") != null ? "value-value" : null;
100-
}
101-
102-
if (styleClass == null) {
103-
styleClass = matcher.group("PAREN") != null ? "paren" : null;
104-
}
105-
106-
if (styleClass == null) {
107-
styleClass = matcher.group("BRACE") != null ? "brace" : null;
108-
}
109-
110-
if (styleClass == null) {
111-
styleClass = matcher.group("BRACKET") != null ? "bracket" : null;
112-
}
113-
114-
if (styleClass == null) {
115-
styleClass = matcher.group("SEMICOLON") != null ? "semicolon" : null;
116-
}
117-
118-
if (styleClass == null) {
119-
styleClass = matcher.group("STRING") != null ? "string" : null;
120-
}
121-
122-
if (styleClass == null) {
123-
styleClass = matcher.group("COMMENT") != null ? "comment" : null;
124-
}
125-
126-
assert styleClass != null;
127-
128-
spansBuilder.add(singleton("plain-code"), matcher.start() - lastKwEnd);
129-
spansBuilder.add(singleton(styleClass), matcher.end() - matcher.start());
130-
131-
lastKwEnd = matcher.end();
132-
}
133-
134-
spansBuilder.add(singleton("plain-code"), text.length() - lastKwEnd);
135-
136-
return spansBuilder.create();
137-
}
138-
13932
@Override
14033
@FXThread
141-
protected @NotNull StyleSpans<? extends Collection<String>> getStyleSpans(@NotNull final String text) {
142-
return computeHighlighting(text);
34+
protected @NotNull BaseCodeArea createCodeArea() {
35+
return new MaterialDefinitionCodeArea();
14336
}
14437

14538
@Override

0 commit comments

Comments
 (0)