Fix #8003 - undo code
[roobuilder] / src / JsRender / NodeWriter.vala
1
2 namespace JsRender {
3         public abstract class NodeWriter : Object {
4         
5         
6                 protected JsRender file;
7                 protected Node node;
8
9                 protected int depth;
10                 protected string inpad;  // the pad for the class outer
11                 protected string pad;   // the padd for the methods / properties.
12                 protected string ipad;
13                  
14                 protected Gee.ArrayList<Node> top_level_items; // top level items (was vitems)
15                 protected int cur_line;
16                 protected NodeWriter top; 
17                 
18                 public string ret {
19                         get {
20                                 return this.output;
21                         }
22                         private set {
23                                 GLib.error("set called on nodewriter ret");
24                         }
25                 }
26          
27                 string output = "";  // the result of outputing..
28                 
29                 Gee.ArrayList<string> ignoreList;
30                 static  Gee.ArrayList<string>? globalIgnoreList = null; 
31                 static construct {
32                         globalIgnoreList = new Gee.ArrayList<string>();
33                 }
34                  
35                 /* 
36                  * ctor - just initializes things
37                  * - wraps a render node 
38                  */
39                 protected NodeWriter( JsRender file,  Node node,  int depth, NodeWriter? parent) 
40                 {
41                         this.file = file;
42                         this.node = node;
43                         this.depth = depth;
44                          
45                         this.top_level_items = new Gee.ArrayList<Node>();
46                         this.cur_line = parent == null ? 0 : parent.cur_line;
47                         this.top = parent == null ? this : parent.top;
48                          
49                 // initialize line data..
50                         node.line_start = this.cur_line;
51                         node.line_end  = this.cur_line;
52                         node.lines = new Gee.ArrayList<int>();
53                         node.line_map = new Gee.HashMap<int,string>();
54                         if (parent == null) {
55                                 node.node_lines = new Gee.ArrayList<int>();
56                                 node.node_lines_map = new Gee.HashMap<int,Node>();
57                          }
58                          
59                         this.ignoreList = new Gee.ArrayList<string>();
60                         
61                 }
62                 
63                 //int var_name_count = 0; // was vcnt
64
65                 string toValaNS(Node item)
66                 {
67                         return item.get("xns") + ".";
68                 }
69                 /**
70                         fills in all the xvala_cls names into the nodes
71                         
72                 */
73                 
74                 
75                 public void initPadding(char pad, int len) 
76                 {
77                 
78                         var has_ns = this.file.xtype == "Gtk" &&  this.file.file_namespace.length > 0;
79                         
80                         if (has_ns) { // namespaced..
81                                 this.inpad = string.nfill((depth > 0 ? 2 : 1)* len, pad);
82                         } else {
83                                 this.inpad = string.nfill((depth > 0 ? 1 : 0) * len , pad);
84                         }
85                         this.pad = this.inpad + string.nfill(len, pad);
86                         this.node.node_pad = this.inpad;
87                         this.ipad = this.inpad +  string.nfill(2* len, pad);;
88                 
89                 
90                 }
91                 
92                 public void  toValaName(Node item, int depth =0) 
93                 {
94  
95
96                         var ns =  this.toValaNS(item) ;
97                         var cls = ns + item.get("xtype");
98                         
99                         item.xvala_cls = cls;
100                         
101                         string id = item.get("id").length > 0 ?
102                                 item.get("id") :  "%s%d".printf(item.get("xtype"), item.oid);
103
104                         
105                         
106                         
107                         if (id[0] == '*' || id[0] == '+') {
108                                 item.xvala_xcls = "Xcls_" + id.substring(1);
109                         } else {
110                                 item.xvala_xcls = "Xcls_" + id;
111                         }
112                                 
113                         
114                         item.xvala_id =  id;
115                         if (depth > 0) {                        
116                                 this.top_level_items.add(item);
117                                 
118                         // setting id on top level class changes it classname..                 
119                         // oddly enough we havent really thought about namespacing here.
120                         
121                         } else if (!item.props.has_key("id")) { 
122                                 // use the file name..
123                                 item.xvala_xcls =  this.file.file_without_namespace;
124                                 // is id used?
125                                 item.xvala_id = this.file.file_without_namespace;
126
127                         }
128                                 // loop children..
129                                                                                                                                    
130                         if (item.readItems().size < 1) {
131                                 return;
132                         }
133                         for(var i =0;i<item.readItems().size;i++) {
134                                 this.toValaName(item.readItems().get(i), depth+1);
135                         }
136                                                   
137                 }
138                 
139                 protected void addLine(string str= "")
140                 {
141                         
142                         if (str.contains("\n")) {
143                                 this.addMultiLine(str);
144                                 return;
145                         }
146                         //GLib.debug(str);
147                         this.cur_line++;
148                         //if (BuilderApplication.opt_bjs_compile != null) {
149                         //      this.output += "/*%d*/ ".printf(this.cur_line) + str + "\n";
150                         //} else {
151                                 this.output += str + "\n";
152                         //}
153                 }
154                 protected void addMultiLine(string str= "")
155                 {
156                          
157                         this.cur_line += str.split("\n").length;
158                         //this.ret +=  "/*%d*/ ".printf(l) + str + "\n";
159                         //GLib.debug(str);
160                         this.output +=   str + "\n";
161                 }
162                 protected string padMultiline(string pad, string str)
163                 {
164                         var ar = str.strip().split("\n");
165                         return string.joinv("\n" + pad , ar);
166                 }
167                 protected static void globalIgnore(string i) {
168                         if (globalIgnoreList == null) {
169                                 globalIgnoreList =  new Gee.ArrayList<string>(); // should not happend.. but dont know about ctor process..
170                         }
171                         globalIgnoreList.add(i);
172                 }
173                 
174                 protected void ignore(string i) {
175                         this.ignoreList.add(i);
176                         
177                 }
178                 
179                 protected bool shouldIgnore(string i)
180                 {
181                         return globalIgnoreList.contains(i) || ignoreList.contains(i);
182                 }
183                 
184                 
185                 // interface
186                 public abstract string munge();
187                 
188                 
189                 
190         }
191 }