Browse Source

GLSL Syntax Highlighting - Round 2 (#179)

* Created project

* Basic file recognition

* Backup before I throw everything out

* Revert "Backup before I throw everything out"

This reverts commit 56c14302c704e7b8d4ebbcbe10802bc26fc7ff4f.

* Added infrastructure - this time non outdated way

* Work on lexer

* Preprocessor now highlighted

* Added block comments

* Fixed preprocessor to work with indents

* Added operators and various brackets

* Added keywords and builtin types

* Fixes to the way keywords are looked up

* Added builtin variables based on glsl 4.4 and JME wiki. Logic behind it would need some tweaking.

* Added builtin functions based on glsl 4.4 and reworked logic behind parsing keywords.

* Added icons by ogli, now just gotta find a way to actually use them

* Fixed the icons, first round ready for PR

* Now with 100% more licenses

* Let autoformat uglify my code

* Added the rest of global uniforms

* Default braces matcher

* Basic indentation working

* Cleanup

* Autoformat
grizeldi 6 years ago
parent
commit
ba7d505505

+ 2 - 2
jme3-glsl-highlighter/nbproject/genfiles.properties

@@ -1,8 +1,8 @@
-build.xml.data.CRC32=9a73419f
+build.xml.data.CRC32=5324ae4f
 build.xml.script.CRC32=0322e3d8
 build.xml.script.CRC32=0322e3d8
 [email protected]
 [email protected]
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=9a73419f
+nbproject/build-impl.xml.data.CRC32=5324ae4f
 nbproject/build-impl.xml.script.CRC32=f767ba38
 nbproject/build-impl.xml.script.CRC32=f767ba38
 nbproject/[email protected]
 nbproject/[email protected]

+ 18 - 0
jme3-glsl-highlighter/nbproject/project.xml

@@ -41,6 +41,15 @@
                         <specification-version>1.87.1.6.3.23.55</specification-version>
                         <specification-version>1.87.1.6.3.23.55</specification-version>
                     </run-dependency>
                     </run-dependency>
                 </dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.editor.bracesmatching</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>0</release-version>
+                        <specification-version>1.39.1.55</specification-version>
+                    </run-dependency>
+                </dependency>
                 <dependency>
                 <dependency>
                     <code-name-base>org.netbeans.modules.editor.lib</code-name-base>
                     <code-name-base>org.netbeans.modules.editor.lib</code-name-base>
                     <build-prerequisite/>
                     <build-prerequisite/>
@@ -50,6 +59,15 @@
                         <specification-version>4.7.1.23.3.55</specification-version>
                         <specification-version>4.7.1.23.3.55</specification-version>
                     </run-dependency>
                     </run-dependency>
                 </dependency>
                 </dependency>
+                <dependency>
+                    <code-name-base>org.netbeans.modules.editor.mimelookup</code-name-base>
+                    <build-prerequisite/>
+                    <compile-dependency/>
+                    <run-dependency>
+                        <release-version>1</release-version>
+                        <specification-version>1.40.1</specification-version>
+                    </run-dependency>
+                </dependency>
                 <dependency>
                 <dependency>
                     <code-name-base>org.netbeans.modules.lexer</code-name-base>
                     <code-name-base>org.netbeans.modules.lexer</code-name-base>
                     <build-prerequisite/>
                     <build-prerequisite/>

+ 48 - 0
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/GlslBracesMatcherFactory.java

@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2003-2018 jMonkeyEngine
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.glsl.highlighter;
+
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.spi.editor.bracesmatching.BracesMatcher;
+import org.netbeans.spi.editor.bracesmatching.BracesMatcherFactory;
+import org.netbeans.spi.editor.bracesmatching.MatcherContext;
+import org.netbeans.spi.editor.bracesmatching.support.BracesMatcherSupport;
+
+@MimeRegistration(mimeType = "text/x-glsl", service = BracesMatcherFactory.class)
+public class GlslBracesMatcherFactory implements BracesMatcherFactory{
+
+    @Override
+    public BracesMatcher createMatcher(MatcherContext mc) {
+        return BracesMatcherSupport.defaultMatcher(mc, -1, -1);
+    }
+    
+}

+ 47 - 0
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/editor/GlslBracesMatcherFactory.java

@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2003-2018 jMonkeyEngine
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.glsl.highlighter.editor;
+
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.spi.editor.bracesmatching.BracesMatcher;
+import org.netbeans.spi.editor.bracesmatching.BracesMatcherFactory;
+import org.netbeans.spi.editor.bracesmatching.MatcherContext;
+import org.netbeans.spi.editor.bracesmatching.support.BracesMatcherSupport;
+
+@MimeRegistration(mimeType = "text/x-glsl", service = BracesMatcherFactory.class)
+public class GlslBracesMatcherFactory implements BracesMatcherFactory {
+
+    @Override
+    public BracesMatcher createMatcher(MatcherContext mc) {
+        return BracesMatcherSupport.defaultMatcher(mc, -1, -1);
+    }
+}

+ 80 - 0
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/editor/GlslIndentTask.java

@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2003-2018 jMonkeyEngine
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.glsl.highlighter.editor;
+
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import org.netbeans.modules.editor.indent.api.IndentUtils;
+import org.netbeans.modules.editor.indent.spi.Context;
+import org.netbeans.modules.editor.indent.spi.ExtraLock;
+import org.netbeans.modules.editor.indent.spi.IndentTask;
+
+/**
+ *
+ * @author grizeldi
+ */
+public class GlslIndentTask implements IndentTask {
+
+    private Context context;
+
+    public GlslIndentTask(Context context) {
+        this.context = context;
+    }
+
+    @Override
+    public void reindent() throws BadLocationException {
+        context.setCaretOffset(1);
+        final Document doc = context.document();
+        int indentModifier = 0;
+
+        //Check if previous line ends with a {
+        int previousLineLength = context.startOffset() - 1 - context.lineStartOffset(context.startOffset() - 1);
+        String previousLine = doc.getText(context.lineStartOffset(context.startOffset() - 1), previousLineLength);
+
+        //Hook other reasons for changes in indentation into this for loop
+        for (int i = previousLineLength - 1; i >= 0; i--) {
+            if (previousLine.charAt(i) == '}') {
+                break;
+            } else if (previousLine.charAt(i) == '{') {
+                indentModifier += IndentUtils.indentLevelSize(doc);
+                break;
+            }
+        }
+        int previousLineIndent = context.lineIndent(context.lineStartOffset(context.startOffset() - 1));
+        context.modifyIndent(context.startOffset(), previousLineIndent + indentModifier);
+    }
+
+    @Override
+    public ExtraLock indentLock() {
+        return null;
+    }
+}

+ 50 - 0
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/editor/GlslIndentTaskFactory.java

@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2003-2018 jMonkeyEngine
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * 
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.jme3.gde.glsl.highlighter.editor;
+
+import org.netbeans.api.editor.mimelookup.MimeRegistration;
+import org.netbeans.modules.editor.indent.spi.Context;
+import org.netbeans.modules.editor.indent.spi.IndentTask;
+
+/**
+ *
+ * @author grizeldi
+ */
+@MimeRegistration(mimeType = "text/x-glsl", service = IndentTask.Factory.class)
+public class GlslIndentTaskFactory implements IndentTask.Factory {
+
+    @Override
+    public IndentTask createTask(Context cntxt) {
+        return new GlslIndentTask(cntxt);
+    }
+
+}

+ 31 - 1
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/lexer/GlslKeywordLibrary.java

@@ -226,6 +226,7 @@ class GlslKeywordLibrary {
         //vertex shaders
         //vertex shaders
         builtinVariables.add("gl_VertexID");
         builtinVariables.add("gl_VertexID");
         builtinVariables.add("gl_InstanceID");
         builtinVariables.add("gl_InstanceID");
+        builtinVariables.add("gl_Position");
         //geometry shaders
         //geometry shaders
         builtinVariables.add("gl_PrimitiveIDIn");
         builtinVariables.add("gl_PrimitiveIDIn");
         builtinVariables.add("gl_Layer");
         builtinVariables.add("gl_Layer");
@@ -245,6 +246,7 @@ class GlslKeywordLibrary {
         builtinVariables.add("gl_SampleMaskIn");
         builtinVariables.add("gl_SampleMaskIn");
         builtinVariables.add("gl_Layer");
         builtinVariables.add("gl_Layer");
         builtinVariables.add("gl_ViewportIndex");
         builtinVariables.add("gl_ViewportIndex");
+        builtinVariables.add("gl_FragColor");
         //general
         //general
         builtinVariables.add("gl_Position");
         builtinVariables.add("gl_Position");
         builtinVariables.add("gl_PointSize");
         builtinVariables.add("gl_PointSize");
@@ -256,10 +258,38 @@ class GlslKeywordLibrary {
         builtinVariables.add("inNormal");
         builtinVariables.add("inNormal");
         builtinVariables.add("inColor");
         builtinVariables.add("inColor");
         builtinVariables.add("inTextCoord");
         builtinVariables.add("inTextCoord");
-        builtinVariables.add("g_WorldViewMatrix");
+        builtinVariables.add("g_WorldMatrix");
+        builtinVariables.add("g_ViewMatrix");
         builtinVariables.add("g_ProjectionMatrix");
         builtinVariables.add("g_ProjectionMatrix");
+        builtinVariables.add("g_WorldViewMatrix");
         builtinVariables.add("g_WorldViewProjectionMatrix");
         builtinVariables.add("g_WorldViewProjectionMatrix");
+        builtinVariables.add("g_WorldNormalMatrix");
         builtinVariables.add("g_NormalMatrix");
         builtinVariables.add("g_NormalMatrix");
+        builtinVariables.add("g_ViewProjectionMatrix");
+        builtinVariables.add("g_WorldMatrixInverseTranspose");
+        builtinVariables.add("g_WorldMatrixInverse");
+        builtinVariables.add("g_ViewMatrixInverse");
+        builtinVariables.add("g_ProjectionMatrixInverse");
+        builtinVariables.add("g_ViewProjectionMatrixInverse");
+        builtinVariables.add("g_WorldViewMatrixInverse");
+        builtinVariables.add("g_NormalMatrixInverse");
+        builtinVariables.add("g_WorldViewProjectionMatrixInverse");
+        builtinVariables.add("g_ViewPort");
+        builtinVariables.add("g_FrustumNearFar");
+        builtinVariables.add("g_Resolution");
+        builtinVariables.add("g_ResolutionInverse");
+        builtinVariables.add("g_Aspect");
+        builtinVariables.add("g_CameraPosition");
+        builtinVariables.add("g_CameraDirection");
+        builtinVariables.add("g_CameraLeft");
+        builtinVariables.add("g_CameraUp");
+        builtinVariables.add("g_Time");
+        builtinVariables.add("g_Tpf");
+        builtinVariables.add("g_FrameRate");
+        builtinVariables.add("g_LightDirection");
+        builtinVariables.add("g_LightPosition");
+        builtinVariables.add("g_LightColor");
+        builtinVariables.add("g_AmbientLightColor");
         //builtin functions
         //builtin functions
         builtinFunctions.add("radians");
         builtinFunctions.add("radians");
         builtinFunctions.add("degrees");
         builtinFunctions.add("degrees");

+ 1 - 1
jme3-glsl-highlighter/src/com/jme3/gde/glsl/highlighter/lexer/GlslTokenID.java

@@ -51,7 +51,7 @@ public enum GlslTokenID implements TokenId {
     PRIMITIVE("basictype"),
     PRIMITIVE("basictype"),
     BUILTIN_VARIABLE("builtinvar"),
     BUILTIN_VARIABLE("builtinvar"),
     BUILTIN_FUNCTION("builtinfunc"),
     BUILTIN_FUNCTION("builtinfunc"),
-    
+
     //Stuff that has to be recognized
     //Stuff that has to be recognized
     LBRACKET("operator"), RBRACKET("operator"),
     LBRACKET("operator"), RBRACKET("operator"),
     LPARENTHESIS("operator"), RPARENTHESIS("operator"),
     LPARENTHESIS("operator"), RPARENTHESIS("operator"),