src/Palete/Palete.vala
[app.Builder.js] / src / Palete / Palete.vala
index cc3e4c3..88003c1 100644 (file)
@@ -1,4 +1,5 @@
 
+using Gtk;
 namespace Palete 
 {
 
@@ -73,12 +74,15 @@ namespace Palete
        
         public string name;
 
-       public Gee.ArrayList<Usage> map;
-               
+               public Gee.ArrayList<Usage> map;
+
+               public Gee.HashMap<string,GirObject> classes; // used in roo.. 
+       
         public Palete()
         {
-            // nothing?
-               this.map = null;
+                               // nothing?
+                       this.map = null;
+                       this.classes = null;
         }
         
         
@@ -99,86 +103,86 @@ namespace Palete
          
 
            
-       public string[] getChildList(string in_rval)
+               public string[] getChildList(string in_rval)
         {
 
-               if (this.map == null) {
-                       this.load();
-               }
-               // empty == *top
-               
-               var rval = in_rval == "" ? "*top" : in_rval; 
-                               
-                               // should be a bit more than this..
-                       // -> it should look for all elements that inherit 
-               string[] ret = {};
-               var rt = new Gee.ArrayList<string>();
-               for (var i = 0; i < this.map.size; i++) {
-                       var m = this.map.get(i);
-                               
-                       if (!m.leftHas(rval)) {
-                               continue;
+                       if (this.map == null) {
+                               this.load();
                        }
-                       print("found LEFT, adding right\n");
-               
-                       for(var ii =0; ii < m.right.size; ii++) {
-                               var l = m.right.get(ii);
+                       // empty == *top
+                       
+                       var rval = in_rval == "" ? "*top" : in_rval; 
                                        
-                               if (rt.index_of(l) > -1) {
-                                   continue;
-                               }
-                               //print("ADD " + string.joinv(", ", ret) + "\n");
-                               ret += l;
-                               rt.add(l);
-                       }
-                               
-                
-               }
-               print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
-               //console.log("DROP LIST:");
-               //console.dump(ret);
-               return ret;
-            
+                                       // should be a bit more than this..
+                               // -> it should look for all elements that inherit 
+                               string[] ret = {};
+                       var rt = new Gee.ArrayList<string>();
+                       for (var i = 0; i < this.map.size; i++) {
+                               var m = this.map.get(i);
+                                       
+                                       if (!m.leftHas(rval)) {
+                                       continue;
+                               }
+                               print("found LEFT, adding right\n");
+                       
+                               for(var ii =0; ii < m.right.size; ii++) {
+                                               var l = m.right.get(ii);
+                                               
+                                               if (rt.index_of(l) > -1) {
+                                                       continue;
+                                               }
+                                       //print("ADD " + string.joinv(", ", ret) + "\n");
+                                               ret += l;
+                                       rt.add(l);
+                                       }
+                                       
+                                       
+                               }
+                       print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
+                       //console.log("DROP LIST:");
+                       //console.dump(ret);
+                       return ret;
+                               
         }
 
            
         public string[] getDropList(string rval)
         {
 
-               if (this.map == null) {
-                       this.load();
-               }
-
-                               
-                               // should be a bit more than this..
-                       // -> it should look for all elements that inherit 
-               string[] ret = {};
-               var rt = new Gee.ArrayList<string>();
-               for (var i = 0; i < this.map.size; i++) {
-                       var m = this.map.get(i);
-                               
-                       if (m.right.index_of(rval) < 0) {
-                               continue;
+                       if (this.map == null) {
+                               this.load();
                        }
-                       //print("found RIGHT, adding left\n");
-               
-                       for(var ii =0; ii < m.left.size; ii++) {
-                               var l = m.left.get(ii);
+
                                        
-                               if (rt.index_of(l) > -1) {
-                                   continue;
-                               }
-                               //print("ADD " + string.joinv(", ", ret) + "\n");
-                               ret += l;
-                               rt.add(l);
-                       }
-                               
-                
-               }
-                print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
-               //console.log("DROP LIST:");
-               //console.dump(ret);
-               return ret;
+                                       // should be a bit more than this..
+                               // -> it should look for all elements that inherit 
+                               string[] ret = {};
+                       var rt = new Gee.ArrayList<string>();
+                       for (var i = 0; i < this.map.size; i++) {
+                               var m = this.map.get(i);
+                                       
+                                       if (m.right.index_of(rval) < 0) {
+                                       continue;
+                               }
+                               //print("found RIGHT, adding left\n");
+                       
+                               for(var ii =0; ii < m.left.size; ii++) {
+                                               var l = m.left.get(ii);
+                                               
+                                               if (rt.index_of(l) > -1) {
+                                                       continue;
+                                               }
+                                       //print("ADD " + string.joinv(", ", ret) + "\n");
+                                               ret += l;
+                                       rt.add(l);
+                                       }
+                                       
+                                       
+                               }
+                        print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
+                       //console.log("DROP LIST:");
+                       //console.dump(ret);
+                       return ret;
             
         }
       
@@ -207,173 +211,259 @@ namespace Palete
         public  GLib.List<string> listTemplates (JsRender.Node node)
         {
             
-               var gn = node.fqn();
-            
-               var ret = new GLib.List<string>();
-               var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
-               if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
-                       return ret;
-               }
+                       var gn = node.fqn();
+                               
+                       var ret = new GLib.List<string>();
+                       var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
+                       if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
+                               return ret;
+                       }
                        
 
 
                                    
-               var f = File.new_for_path(dir);
-        
-            var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
-             
-            FileInfo next_file; 
-            while ((next_file = file_enum.next_file(null)) != null) {
-                var n = next_file.get_display_name();
-                       if (!Regex.match_simple ("\\.json$", n)) {
-                                       continue;
+                       var f = File.new_for_path(dir);
+                       
+                               var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
+                                
+                               FileInfo next_file; 
+                               while ((next_file = file_enum.next_file(null)) != null) {
+                                       var n = next_file.get_display_name();
+                                       if (!Regex.match_simple ("\\.json$", n)) {
+                                               continue;
+                                       }
+                                       ret.append( dir + "/" + n);
                                }
-                               ret.append( dir + "/" + n);
-            }
-            return ret;
+                               return ret;
             
-        }
+               }
  
         public JsRender.Node? loadTemplate(string path)
         {
 
-               var pa = new Json.Parser();
-               pa.load_from_file(path);
-               var node = pa.get_root();
+                       var pa = new Json.Parser();
+                       pa.load_from_file(path);
+                       var node = pa.get_root();
 
-               if (node.get_node_type () != Json.NodeType.OBJECT) {
-                       return null;
-               }
-               var obj = node.get_object ();
+                       if (node.get_node_type () != Json.NodeType.OBJECT) {
+                               return null;
+                       }
+                       var obj = node.get_object ();
 
-               var ret = new JsRender.Node();
+                       var ret = new JsRender.Node();
 
 
-               ret.loadFromJson(obj, 1);
-               ret.ref(); // not sure if needed -- but we had a case where ret became uninitialized?
+                       ret.loadFromJson(obj, 1);
+                       ret.ref(); // not sure if needed -- but we had a case where ret became uninitialized?
                
-               return ret;
-        }
+                       return ret;
+               }
 
 
 
-    public   void  loadUsageFile (string fname) {
+               public   void  loadUsageFile (string fname) {
 
 
 
                        
-               print("Palete Load called\n");
-               string raw;
-               if (!FileUtils.test (fname, FileTest.EXISTS)) {
-                       throw new Error.MISSING_FILE(fname + " not found");
-               }
+                               print("Palete Load called\n");
+                       string raw;
+                       if (!FileUtils.test (fname, FileTest.EXISTS)) {
+                               throw new Error.MISSING_FILE(fname + " not found");
+                       }
        
-               FileUtils.get_contents(fname, out raw);
-             // print(data);
-               var data  = raw.split("\n");
-               var state = 0;
-               var cfg = new Gee.ArrayList<Usage>();
-               var left = new Gee.ArrayList<string>();
-               var right = new Gee.ArrayList<string>();
+                       FileUtils.get_contents(fname, out raw);
+                     // print(data);
+                       var data  = raw.split("\n");
+                       var state = 0;
+                       var cfg = new Gee.ArrayList<Usage>();
+                       var left = new Gee.ArrayList<string>();
+                       var right = new Gee.ArrayList<string>();
        
-               for (var i = 0; i < data.length; i++) {
-                       var d = data[i].strip();
-                       //print("READ " + d + "\n");
-                       if (
-                               d.length < 1
-                           ||
-                            Regex.match_simple ("^\\s+$", d)
-                           ||
-                               Regex.match_simple ("^\\s*/", d)
-                    ){
-                           continue;
-                       }
+                       for (var i = 0; i < data.length; i++) {
+                               var d = data[i].strip();
+                               //print("READ " + d + "\n");
+                               if (
+                                       d.length < 1
+                                       ||
+                                        Regex.match_simple ("^\\s+$", d)
+                                       ||
+                                       Regex.match_simple ("^\\s*/", d)
+                                ){
+                                       continue;
+                               }
                                
-                       if (Regex.match_simple ("^left:", d)) { 
-                           state = 1;
-                           if (left.size > 0 ){
-                               cfg.add(new Usage( left, right));
-                                               }
-                           left = new Gee.ArrayList<string>();
-                                       right = new Gee.ArrayList<string>();
-                           continue;
-                       }
-                        if (Regex.match_simple ("^right:", d)) { 
-                           state = 2;
-                           continue;
-                       }
-                       if (state == 1) {
-                                               //print("add left: " + d + "\n");
-                           left.add(d);
-                           continue;
-                       }
-                                       //print("add Right: " + d + "\n");
-                       right.add(d);
-                       //Seed.quit();
-                      
-               }
-               if (left.size > 0 ){
-                       cfg.add(new Usage( left, right));
-               }
-               this.map = cfg;
-
-        }
+                                   if (Regex.match_simple ("^left:", d)) { 
+                                       state = 1;
+                                       if (left.size > 0 ){
+                                           cfg.add(new Usage( left, right));
+                                                       }
+                                       left = new Gee.ArrayList<string>();
+                                               right = new Gee.ArrayList<string>();
+                                       continue;
+                                   }
+                                    if (Regex.match_simple ("^right:", d)) { 
+                                       state = 2;
+                                       continue;
+                                   }
+                                   if (state == 1) {
+                                                       //print("add left: " + d + "\n");
+                                       left.add(d);
+                                       continue;
+                                   }
+                                               //print("add Right: " + d + "\n");
+                                   right.add(d);
+                                   //Seed.quit();
+                                  
+                       }
+                       if (left.size > 0 ){
+                               cfg.add(new Usage( left, right));
+                       }
+                       this.map = cfg;
+
+          }
+
+        
+               public   void validateVala(
+                               WindowState state,
+                               string code, 
+                               string property, 
+                               string ptype,
+                               JsRender.JsRender file,
+                               JsRender.Node node
+                ) 
+               {   
+
+                       print("validate code (%s) %s\n", file.language, code);
+                        
+               
+                        
+                       if (file.language != "vala" ) { // not sure if we need to validate property
+                               return;
+                       }
+                       // file.project , file.path, file.build_module, ""
+                       
+               
+                        
+                       //var cd = new JSCore.ClassDefinitionEmpty();
+                       state.valasource.checkFileWithNodePropChange(
+                                       file,
+                                       node, 
+                                       property, 
+                                       ptype,
+                                       code
+                        );
+                        
 
-       
-         
-       public Gee.HashMap<int,string>  validateCode(
-                       string code, 
-                       string property, 
-                       string ptype,
-                        JsRender.JsRender file,
-                       JsRender.Node node
-                     ) 
-       {   
-
-               //print("validate code (%s) %s\n", file.language, code);
+               }
                 
+       
+       
+       
+               public   bool  javascriptHasErrors(
+                                       WindowState state,
+                                       string code, 
+                                       string property, 
+                                       string ptype,
+                                       JsRender.JsRender file,
+                                       JsRender.Node? node, 
+                                       out Gee.HashMap<int,string> errors
+                                ) 
+               {   
+
+                        print("validate code (%s) ptype=%s property=%s\n", file.language, ptype, property);
+                          errors = new Gee.HashMap<int,string>();
                
-               if (file.language == "js" && (ptype == "listener" || property[0] == '|')) {
-                       var ret = new Gee.HashMap<int,string>();
+                       if (file.language != "js") {
+                               return false;
+                        }
+                        if (ptype != "listener" && property.length > 0 && property[0] == '|') {
+                               return false;
+                        }
                        
                        //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);
+                                                                 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.path, lines_obj);
+                                        
+                                       err.set_object_member("ERR", files_obj);
+                                       state.showCompileResult(err);
+                                       // do not set errors.. that's not done here..
+                                       return true;
+                               }
+                               errors.set(line, errmsg); // depricated - this is used by the editor currently -- but we are planning to switch from that..
+                               print("got  errors\n");
+                               return true;
 
-                       if (line < 0) {
-                               return ret;
                        }
-                       ret.set(line, errmsg);
-                       return ret;
-               }
-               if (file.language == "vala" ) { // not sure if we need to validate property
+                       // now syntax is OK.. try the 
                        
-                       var vs = new ValaSource(file);
-                       //var cd = new JSCore.ClassDefinitionEmpty();
-                       var ret = vs.checkFileWithNodePropChange(
-                               node, 
-                               property, 
-                               ptype,
-                                       code
-                         );
-                       return ret;
-               }
-               var ret = new Gee.HashMap<int,string>();
+                       
+                       
+                       if (ptype == "file") {
+                                return this.javascriptHasCompressionErrors(file, state, code);
+                       }
+                       print("no errors\n");
+                       return false;
+                         
+               } 
+               
+               public bool  javascriptHasCompressionErrors(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 
 
-               //print ("not javascript\n");
-               return ret;
+                       var p = new JSDOC.Packer();
+                       p.keepWhite = false;
+                       p.skipScope = false;
+                       p.dumpTokens = false;
+                       p.cleanup = false; 
+                
+                        
+                       p.packFile(code, file.path,"");
+                       state.showCompileResult(p.result);
+                       if (p.hasErrors()) {
+                               return true;
+                       }
+                       return false;
+                       
 
+               
+               }
+               
+               
+                     
+               public abstract void fillPack(JsRender.Node node,JsRender.Node parent);
+               public abstract void load();
+               public abstract Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type);
+               public abstract GirObject? getClass(string ename);
+       
+               public abstract bool typeOptions(string fqn, string key, string type, out string[] opts);
+               public abstract  List<SourceCompletionItem> suggestComplete(
+                               JsRender.JsRender file,
+                               JsRender.Node? node,
+                               string proptype, 
+                               string key,
+                               string complete_string
+               );
        }
 
-           
-          
-        public abstract void fillPack(JsRender.Node node,JsRender.Node parent);
-       public abstract void load();
-       public abstract Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type);
-       public abstract GirObject? getClass(string ename);
-    }
-
 
 }