Fix #8004 - try and fix start line / end line cached node output
[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                 protected 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                 public void addLine(string str= "")
140                 {
141                         if (str.contains("\n")) {
142                                 this.addMultiLine(str);
143                                 return;
144                         }
145                         //GLib.debug(str);
146                         this.cur_line++;
147                         //if (BuilderApplication.opt_bjs_compile != null) {
148                         //      this.output += "/*%d*/ ".printf(this.cur_line) + str + "\n";
149                         //} else {
150                                 this.output += str + "\n";
151                         //}
152                 }
153                 public void addMultiLine(string str= "", bool addLastLine = true)
154                 {
155                          
156                         this.cur_line += str.split("\n").length;
157                         this.cur_line -= (addLastLine? 0 : 1);
158                         
159                         //this.ret +=  "/*%d*/ ".printf(l) + str + "\n";
160                         //GLib.debug(str);
161                         this.output +=   str + (addLastLine ?  "\n" : "");
162                 }
163                 protected string padMultiline(string pad, string str)
164                 {
165                         var ar = str.strip().split("\n");
166                         return string.joinv("\n" + pad , ar);
167                 }
168                 protected static void globalIgnore(string i) {
169                         if (globalIgnoreList == null) {
170                                 globalIgnoreList =  new Gee.ArrayList<string>(); // should not happend.. but dont know about ctor process..
171                         }
172                         globalIgnoreList.add(i);
173                 }
174                 
175                 protected void ignore(string i) {
176                         this.ignoreList.add(i);
177                         
178                 }
179                 
180                 protected bool shouldIgnore(string i)
181                 {
182                         return globalIgnoreList.contains(i) || ignoreList.contains(i);
183                 }
184                 
185                 
186                 // interface
187                 public abstract string munge();
188                 
189                 
190                 
191         }
192 }