X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=src%2FJsRender%2FNodeToVala.vala;h=1350c0dc62488d255f644f711f0900df62e6a7c1;hb=HEAD;hp=4f8a7af3dfaf89118c980ed7b7a1729e1875ef5c;hpb=bb8b289f76753af8654f0df1210739e69d0ab86a;p=app.Builder.js diff --git a/src/JsRender/NodeToVala.vala b/src/JsRender/NodeToVala.vala index 4f8a7af3d..1350c0dc6 100644 --- a/src/JsRender/NodeToVala.vala +++ b/src/JsRender/NodeToVala.vala @@ -1,8 +1,8 @@ /** * - * Code to convert node tree to Javascript... + * Code to convert node tree to Vala... * - * usage : x = (new JsRender.NodeToJs(node)).munge(); + * usage : x = (new JsRender.NodeToVala(node)).munge(); * * * @@ -25,7 +25,8 @@ public class JsRender.NodeToVala : Object { string xcls; string ret; - int line; + + int cur_line; Gee.ArrayList ignoreList; Gee.ArrayList ignoreWrappedList; @@ -50,13 +51,26 @@ public class JsRender.NodeToVala : Object { this.cls = node.xvala_cls; this.xcls = node.xvala_xcls; this.ret = ""; - this.line = parent.line; + this.cur_line = parent == null ? 0 : parent.cur_line; + + this.top = parent == null ? this : parent.top; this.ignoreList = new Gee.ArrayList(); this.ignoreWrappedList = new Gee.ArrayList(); this.myvars = new Gee.ArrayList(); this.vitems = new Gee.ArrayList(); this.file = null; + + // initialize line data.. + node.line_start = this.cur_line; + node.line_end = this.cur_line; + node.lines = new Gee.ArrayList(); + node.line_map = new Gee.HashMap(); + if (parent == null) { + node.node_lines = new Gee.ArrayList(); + node.node_lines_map = new Gee.HashMap(); + } + } public int vcnt = 0; @@ -174,31 +188,51 @@ public class JsRender.NodeToVala : Object { var x = new NodeToVala(cnode, this.depth+1, this); return x.munge(); } - + public void addLine(string str= "") + { + this.cur_line++; + //this.ret += "/*%d*/ ".printf(this.cur_line-1) + str + "\n"; + this.ret += str + "\n"; + } + public void addMultiLine(string str= "") + { + + this.cur_line += str.split("\n").length; + //this.ret += "/*%d*/ ".printf(l) + str + "\n"; + this.ret += str + "\n"; + } + + public void globalVars() { if (this.depth > 0) { return; } - // Global Vars.. - //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n"; + // Global Vars..??? when did this get removed..? + //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n"; - - this.ret += this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";\n\n"; - - + this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";"); + this.addLine(); + } void classHeader() { - // class header.. - // class xxx { WrappedGtk el; } - this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n"; - this.ret += this.pad + "public " + this.cls + " el;\n"; - - - this.ret += this.pad + "private " + this.top.xcls + " _this;\n\n"; + // class header.. + // class xxx { WrappedGtk el; } + this.node.line_start = this.cur_line; + + this.top.node.setNodeLine(this.cur_line, this.node); + + this.addLine(inpad + "public class " + this.xcls + " : Object"); + this.addLine(this.inpad + "{"); + + + this.addLine(this.pad + "public " + this.cls + " el;"); + + this.addLine(this.pad + "private " + this.top.xcls + " _this;"); + this.addLine(); @@ -206,19 +240,23 @@ public class JsRender.NodeToVala : Object { } void addSingleton() { - if (depth > 0) { + if (depth > 0) { return; } - this.ret += pad + "public static " + xcls + " singleton()\n" + - this.pad + "{\n" + - this.ipad + "if (_" + this.node.xvala_id + " == null) {\n" + - this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args? - this.ipad + "}\n" + - this.ipad + "return _" + this.node.xvala_id +";\n" + - this.pad + "}\n"; + this.addLine(pad + "public static " + xcls + " singleton()"); + this.addLine(this.pad + "{"); + this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {"); + this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args? + this.addLine(this.ipad + "}"); + this.addLine(this.ipad + "return _" + this.node.xvala_id +";"); + this.addLine(this.pad + "}"); } - + /** + * when ID is used... on an element, it registeres a property on the top level... + * so that _this.ID always works.. + * + */ void addTopProperties() { if (this.depth > 0) { @@ -231,24 +269,31 @@ public class JsRender.NodeToVala : Object { var n = iter.get(); - if (!n.props.has_key("id") || n.xvala_id.length < 0) { - continue; - - } - if (n.xvala_id[0] == '*') { - continue; - } - if (n.xvala_id[0] == '+') { - continue; - } - this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n"; - } + if (!n.props.has_key("id") || n.xvala_id.length < 0) { + continue; + + } + if (n.xvala_id[0] == '*') { + continue; + } + if (n.xvala_id[0] == '+') { + continue; + } + this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";"); + + } } - - void addMyVars() + /** + * create properties that are not 'part of the wrapped element. + * + * + */ + + void addMyVars() { - this.ret += "\n" + this.ipad + "// my vars (def)\n"; + this.addLine(); + this.addLine(this.ipad + "// my vars (def)"); @@ -262,7 +307,7 @@ public class JsRender.NodeToVala : Object { // Key = TYPE:name var iter = this.node.props.map_iterator(); while (iter.next()) { - var k = iter.get_key(); + var k = iter.get_key(); if (this.shouldIgnore(k)) { continue; } @@ -275,11 +320,13 @@ public class JsRender.NodeToVala : Object { continue; } - if (vv[0] == "@") { - this.ret += this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";\n"; + if (vv[0] == "@") { + this.node.setLine(this.cur_line, "p", k); + this.addLine(this.pad + "public signal" + k.substring(1) + " " + iter.get_value() + ";"); + this.ignore(k); continue; - } + } var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2; if (vv.length < min) { // skip 'old js style properties without a type' @@ -298,10 +345,10 @@ public class JsRender.NodeToVala : Object { } this.myvars.add(k); - - - this.ret += this.pad + "public " + - (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n"; + this.node.setLine(this.cur_line, "p", k); + + this.addLine(this.pad + "public " + + (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";"); this.ignore(k); @@ -309,67 +356,78 @@ public class JsRender.NodeToVala : Object { } } - // if id of child is '+' then it's a property of this.. - void addPlusProperties() + // if id of child is '+' then it's a property of this.. + void addPlusProperties() { - if (this.node.items.size < 1) { - return; + if (this.node.items.size < 1) { + return; } var iter = this.node.items.list_iterator(); while (iter.next()) { var ci = iter.get(); - - if (ci.xvala_id[0] != '+') { - continue; // skip generation of children? - - } - this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n"; - - - } + + if (ci.xvala_id[0] != '+') { + continue; // skip generation of children? + + } + + this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";"); + + + } } - + /** + * add the constructor definition.. + */ void addValaCtor() { - - // .vala props.. - - string[] cargs = {}; - var cargs_str = ""; - // ctor.. - this.ret += "\n" + this.pad + "// ctor \n"; - if (this.node.has("* args")) { - // not sure what this is supposed to be ding.. - - cargs_str = ", " + this.node.get("* args"); - //var ar = this.node.get("* args");.split(","); - //for (var ari =0; ari < ar.length; ari++) { - // cargs += (ar[ari].trim().split(" ").pop(); - // } - } - if (this.depth < 1) { - this.ret += this.pad + "public " + this.xcls + "(" + - cargs_str +")\n" + this.pad + "{\n"; - } else { - - //code - - this.ret+= this.pad + "public " + this.xcls + "(" + - this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n"; + // .vala props.. + + string[] cargs = {}; + var cargs_str = ""; + // ctor.. + this.addLine(); + this.addLine(this.pad + "// ctor"); + + if (this.node.has("* args")) { + // not sure what this is supposed to be ding.. + + cargs_str = ", " + this.node.get("* args"); + //var ar = this.node.get("* args");.split(","); + //for (var ari =0; ari < ar.length; ari++) { + // cargs += (ar[ari].trim().split(" ").pop(); + // } } - + + if (this.depth < 1) { + + // top level - does not pass the top level element.. + this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")"); + this.addLine(this.pad + "{"); + } else { + + // for sub classes = we passs the top level as _owner + this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")"); + this.addLine(this.pad + "{"); + } + } + /** + * make sure _this is defined.. + */ void addUnderThis() { - // public static? - if (depth < 1) { - this.ret += this.ipad + "_this = this;\n"; + // public static? + if (depth < 1) { + this.addLine( this.ipad + "_this = this;"); return; } - this.ret+= this.ipad + "_this = _owner;\n"; + // for non top level = _this point to owner, and _this.ID is set + + this.addLine( this.ipad + "_this = _owner;"); if (this.node.props.has_key("id") && @@ -379,13 +437,16 @@ public class JsRender.NodeToVala : Object { && this.node.xvala_id[0] != '+' ) { - this.ret+= this.ipad + "_this." + node.xvala_id + " = this;\n"; + this.addLine( this.ipad + "_this." + node.xvala_id + " = this;"); } - - - + } + /** + * Initialize this.el to point to the wrapped element. + * + * + */ void addWrappedCtor() { @@ -402,19 +463,14 @@ public class JsRender.NodeToVala : Object { } */ if (this.node.has("* ctor")) { - - - this.ret += this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n"; + this.node.setLine(this.cur_line, "p", "* ctor"); + this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";"); return; } - // the ctor arguments... - - // see what the - //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv"); - //if (default_ctor == null) { - var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new"); + + var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new"); - //} + if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) { string[] args = {}; var iter =default_ctor.paramset.params.list_iterator(); @@ -455,13 +511,15 @@ public class JsRender.NodeToVala : Object { args += v; } - this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ; + this.node.setLine(this.cur_line, "p", "* xtype"); + + this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ; return; } + this.node.setLine(this.cur_line, "p", "* xtype");; - - this.ret += this.ipad + "this.el = new " + this.cls + "();\n"; + this.addLine(this.ipad + "this.el = new " + this.cls + "();"); } @@ -474,30 +532,32 @@ public class JsRender.NodeToVala : Object { // initialize.. my vars.. - this.ret += "\n" + this.ipad + "// my vars (dec)\n"; + this.addLine(); + this.addLine( this.ipad + "// my vars (dec)"); var iter = this.myvars.list_iterator(); while(iter.next()) { - var k = iter.get(); + var k = iter.get(); - var ar = k.strip().split(" "); + var ar = k.strip().split(" "); var kname = ar[ar.length-1]; - var v = this.node.props.get(k); + var v = this.node.props.get(k); // ignore signals.. - if (v.length < 1) { - continue; - } + if (v.length < 1) { + continue; + } if (v == "FALSE" || v == "TRUE") { v = v.down(); } -//FIXME -- check for raw string.. "string XXXX" + //FIXME -- check for raw string.. "string XXXX" // if it's a string... - this.ret += this.ipad + "this." + kname + " = " + v +";\n"; - } + + this.addLine(this.ipad + "this." + kname + " = " + v +";"); + } } @@ -511,12 +571,14 @@ public class JsRender.NodeToVala : Object { return; } // what are the properties of this class??? - this.ret += "\n" + this.ipad + "// set gobject values\n"; + this.addLine(); + this.addLine(this.ipad + "// set gobject values"); + var iter = cls.props.map_iterator(); while (iter.next()) { var p = iter.get_key(); - print("Check Write %s\n", p); + //print("Check Write %s\n", p); if (!this.node.has(p)) { continue; } @@ -550,7 +612,7 @@ public class JsRender.NodeToVala : Object { } - this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s, iter.get_value().type); + this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type); // got a property.. @@ -558,6 +620,12 @@ public class JsRender.NodeToVala : Object { } } + /** + * pack the children into the parent. + * + * if the child's id starts with '*' then it is not packed... + * - this allows you to define children and add them manually.. + */ void addChildren() { @@ -566,62 +634,65 @@ public class JsRender.NodeToVala : Object { return; } - var iter = this.node.items.list_iterator(); + var iter = this.node.items.list_iterator(); var i = -1; while (iter.next()) { i++; - var ci = iter.get(); + var ci = iter.get(); if (ci.xvala_id[0] == '*') { - continue; // skip generation of children? - } + continue; // skip generation of children? + } - var xargs = ""; - if (ci.has("* args")) { - - var ar = ci.get("* args").split(","); - for (var ari = 0 ; ari < ar.length; ari++ ) { + var xargs = ""; + if (ci.has("* args")) { + + var ar = ci.get("* args").split(","); + for (var ari = 0 ; ari < ar.length; ari++ ) { var arg = ar[ari].split(" "); - xargs += "," + arg[arg.length -1]; - } - } - - this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls + - "( _this " + xargs + ");\n" ; - - this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children... - - if (ci.has("* prop")) { - this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n"; - continue; - } + xargs += "," + arg[arg.length -1]; + } + } + // create the element.. + this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls + + "( _this " + xargs + ");" ); + + // this is only needed if it does not have an ID??? + this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children... + + if (ci.has("* prop")) { + this.addLine(ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;"); + continue; + } + - // not sure why we have 'true' in pack?!? - if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") { - continue; - } - - string[] packing = { "add" }; + // not sure why we have 'true' in pack?!? + if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") { + continue; + } + + string[] packing = { "add" }; if (ci.has("pack")) { packing = ci.get("pack").split(","); } - - var pack = packing[0]; - this.ret += this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " + - (packing.length > 1 ? - (", " + string.joinv(",", packing).substring(pack.length+1)) - : - "" - ) + " );\n"; - - - if (ci.xvala_id[0] != '+') { - continue; // skip generation of children? - - } - this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";\n"; - + + var pack = packing[0]; + this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " + + (packing.length > 1 ? + (", " + string.joinv(",", packing).substring(pack.length+1)) + : + "" + ) + " );"); + + + if (ci.xvala_id[0] != '+') { + continue; // skip generation of children? + + } + // this.{id - without the '+'} = the element... + this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";"); + } } @@ -632,9 +703,12 @@ public class JsRender.NodeToVala : Object { if (!this.node.has("init")) { return; } - this.ret+= "\n" + ipad + "// init method \n"; - - this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n"; + this.addLine(); + this.addLine(ipad + "// init method"); + this.addLine(); + this.node.setLine(this.cur_line, "p", "init"); + + this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) ); } void addListeners() @@ -643,26 +717,27 @@ public class JsRender.NodeToVala : Object { return; } + this.addLine(); + this.addLine(ipad + "//listeners"); - - this.ret+= "\n" + ipad + "// listeners \n"; + var iter = this.node.listeners.map_iterator(); while (iter.next()) { var k = iter.get_key(); var v = iter.get_value(); - this.ret+= this.ipad + "this.el." + k + ".connect( " + - this.padMultiline(this.ipad,v) +");\n"; - - } + + this.node.setLine(this.cur_line, "l", k); + this.addMultiLine(this.ipad + "this.el." + k + ".connect( " + + this.padMultiline(this.ipad,v) +");"); + + } } - void addEndCtor() + void addEndCtor() { - - - + // end ctor.. - this.ret+= this.pad + "}\n"; + this.addLine(this.pad + "}"); } @@ -710,48 +785,53 @@ public class JsRender.NodeToVala : Object { void addUserMethods() { - - this.ret+= "\n" + pad + "// user defined functions \n"; + this.addLine(); + this.addLine(this.pad + "// user defined functions"); // user defined functions... var iter = this.node.props.map_iterator(); while(iter.next()) { - var k = iter.get_key(); + var k = iter.get_key(); if (this.shouldIgnore(k)) { continue; } // HOW TO DETERIME if its a method? - if (k[0] != '|') { - //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); - continue; - } - // function in the format of {type} (args) { .... } - var kk = k.substring(2); - var vv = iter.get_value(); - this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n"; + if (k[0] != '|') { + //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); + continue; + } + + // function in the format of {type} (args) { .... } + var kk = k.substring(2); + var vv = iter.get_value(); + this.node.setLine(this.cur_line, "p", k); + this.addMultiLine(this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv));; - } + } } void iterChildren() { + this.node.line_end = this.cur_line; + this.node.sortLines(); + - if (this.depth > 0) { - this.ret+= this.inpad + "}\n"; - } + if (this.depth > 0) { + this.addLine(this.inpad + "}"); + } var iter = this.node.items.list_iterator(); - var i = -1; + while (iter.next()) { - this.ret += this.mungeChild(iter.get()); + this.addMultiLine(this.mungeChild(iter.get())); } - if (this.depth < 1) { - this.ret+= this.inpad + "}\n"; - } - + if (this.depth < 1) { + this.addLine(this.inpad + "}"); } + + } string padMultiline(string pad, string str) {