src/Palete/Palete.vala
[app.Builder.js] / src / Palete / Palete.vala
index f70ca08..44ed2e3 100644 (file)
@@ -361,39 +361,104 @@ namespace Palete
        
        
                public   Gee.HashMap<int,string>  validateJavascript(
+                                       WindowState state,
                                        string code, 
                                        string property, 
                                        string ptype,
                                        JsRender.JsRender file,
-                                       JsRender.Node node
+                                       JsRender.Node? node
                                 ) 
                {   
 
-                        print("validate code (%s) %s\n", file.language, code);
+                        print("validate code (%s) ptype=%s property=%s\n", file.language, ptype, property);
                         var ret = new Gee.HashMap<int,string>();
                
                        if (file.language != "js") {
                                return ret;
                         }
-                        if (ptype != "listener" && property[0] == '|') {
+                        if (ptype != "listener" && property.length > 0 && property[0] == '|') {
                                return ret;
                         }
                        
                        //var cd = new JSCore.ClassDefinitionEmpty();
                        //print("TESTING CODE %s\n", code);
                        string errmsg;
+                       var testcode = ptype == "file" ? code : "var __aaa___ = " + code;
                        var line = Javascript.singleton().validate(
-                                                                 "var __aaa___ = " + code, out errmsg);
-
-                       if (line < 0) {
-                               print("no errors\n");
+                                                                 testcode, out errmsg);
+
+                       if (line > -1) {
+                               if (ptype == "file") {
+                                       var err = new Json.Object();
+                                       err.set_int_member("ERR-TOTAL", 1);
+                                       var files_obj = new Json.Object();
+                                       var lines_obj = new Json.Object();
+                                       var lines_ar = new Json.Array();
+                                       lines_ar.add_string_element(errmsg);
+                                       lines_obj.set_array_member(line.to_string(), lines_ar);
+                                       files_obj.set_object_member(file.filename, lines_obj);
+                                        
+                                       err.set_object_member("ERR", files_obj);
+                                       state.showCompileResult(err);
+                                       return ret;
+                               }
+                               ret.set(line, errmsg); // depricated - this is used by the editor currently -- but we are planning to switch from that..
+                               print("got  errors\n");
                                return ret;
+
+                       }
+                       // now syntax is OK.. try the 
+                       
+                       
+                       
+                       if (ptype == "file") {
+                                this.validateJavascriptCompression(file, state, code);
                        }
-                       ret.set(line, errmsg);
-                       print("got  errors\n");
+                       print("no errors\n");
                        return ret;
                          
                } 
+               
+               public void  validateJavascriptCompression(JsRender.JsRender file, WindowState state, string code)
+               {
+                       // this uses the roojspacker code to try and compress the code.
+                       // it should highlight errors before we actually push live the code.
+                       
+                       // standard error format:  file %s, line %s, Error 
+
+                       var p = new JSDOC.Packer();
+                       p.keepWhite = false;
+                       p.skipScope = false;
+                       p.dumpTokens = false;
+                       p.cleanup = false; 
+                       var estr = "";
+                       var ret = new Gee.HashMap<int,string>();
+                        
+                       p.packFile(code, file.filename,"");
+                       if (!p.hasErrors("")) {
+                               state.showCompileResult(err);
+                       }
+                         
+                       if (estr.length > 0 ) {
+
+                               var bits = estr.split(",");
+                               if (bits.length > 2) {
+                                       var linebits = bits[1].strip().split(" ");
+                                       if (linebits.length > 1) {
+                                               var line = int.parse(linebits[1]);
+                                               // line is correct - but our standard for error messages is to do it start at '0'
+                                               ret.set(line -1 , bits[2]);
+                                       }
+                                       
+                               }
+                       }
+                       
+                       return ret;
+               
+               }
+               
+               
                      
                public abstract void fillPack(JsRender.Node node,JsRender.Node parent);
                public abstract void load();
@@ -403,7 +468,7 @@ namespace Palete
                public abstract bool typeOptions(string fqn, string key, string type, out string[] opts);
                public abstract  List<SourceCompletionItem> suggestComplete(
                                JsRender.JsRender file,
-                               JsRender.Node node,
+                               JsRender.Node? node,
                                string proptype, 
                                string key,
                                string complete_string