11package com .ss .editor .ui .control .code ;
22
3- import static java .util .Collections .singleton ;
43import com .ss .editor .annotation .FXThread ;
4+ import com .ss .editor .util .GLSLType ;
55import org .fxmisc .richtext .model .StyleSpans ;
6- import org .fxmisc .richtext .model .StyleSpansBuilder ;
76import org .jetbrains .annotations .NotNull ;
7+ import org .jetbrains .annotations .Nullable ;
88
9+ import java .util .ArrayList ;
10+ import java .util .Arrays ;
911import java .util .Collection ;
10- import java .util .regex . Matcher ;
12+ import java .util .List ;
1113import 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