f78d7e8ce31b693f246ee71cf5c1ebf89039ac58
[roobuilder] / src / Palete / CompileError.vala
1 /*
2
3 Object to handle compiler errors
4 so they can be passed off to trees.
5
6 */
7
8 namespace Palete {
9
10         public class  CompileError : Object
11         {
12                 
13                 public JsRender.JsRender file = null;
14                 public string title = "";
15                 
16                 public GLib.ListStore lines;
17
18                 public CompileError? parent = null;
19                 public string category;
20                 public string msg;
21                 public  int line { get; set; default = -1; }
22
23                 public CompileError.new_line(CompileError parent, int line, string msg) 
24                 {
25                         this.lines = new GLib.ListStore(typeof(CompileError));
26                         this.parent = parent;
27                         this.line = line;
28                         this.msg = msg;
29                         this.file = parent.file;
30                         this.category = parent.category;
31                          
32                 
33                 }
34                 
35                 
36
37
38                 public CompileError.new_file(JsRender.JsRender file, Json.Object jlines, string category) 
39                 {
40                         this.file = file;
41                         this.category = category;
42                         this.title =  file.relpath + " (" + jlines.get_size().to_string() + ")";
43                         
44             this.lines = new GLib.ListStore(typeof(CompileError));
45             
46             jlines.foreach_member((obja, line, nodea) => {
47                 var msg  = "";
48                 var ar = jlines.get_array_member(line);
49                 
50                 
51                 
52                 for (var i = 0 ; i < ar.get_length(); i++) {
53                                 msg += (msg.length > 0) ? "\n" : "";
54                                 msg += ar.get_string_element(i);
55                     }
56                     this.lines.append(new CompileError.new_line(this, int.parse(line) ,msg));
57          
58             
59             });
60               
61                 }
62                 
63                 public string file_line { // sorting?
64                         set {}
65                         owned get { 
66                                 return this.parent == null ? this.file.relpath : 
67                                         (this.file.relpath + ":" + this.line.to_string("%09d")); 
68                         }
69                 }
70                 public string line_msg {
71                         set {}
72                         owned  get {
73                                 return this.parent == null ? 
74                                          GLib.Markup.escape_text( this.file.relpath + "(" +  this.lines.n_items.to_string() + ")") :                    
75                                          GLib.Markup.escape_text(this.line.to_string() + ": " + this.msg);
76                         }
77                 }
78                 
79                 
80                 public static void parseCompileResults (ValaCompileRequest req, Json.Object tree)
81                 {
82                         req.errorByFile = new Gee.HashMap<string,GLib.ListStore>();
83                         req.errorByType = new Gee.HashMap<string,GLib.ListStore>();
84
85                         req.errorByType.set("ERR",  new GLib.ListStore(typeof(CompileError)));
86                         req.errorByType.set("WARN",  new GLib.ListStore(typeof(CompileError)));
87                         req.errorByType.set("DEPR",  new GLib.ListStore(typeof(CompileError)));                         
88  
89                         jsonToListStoreProp(req, "WARN", tree);
90                         jsonToListStoreProp(req, "ERR", tree);   
91                         jsonToListStoreProp(req, "DEPR", tree);  
92                         
93                          
94                 }
95                 
96                 public static void jsonToListStoreProp(ValaCompileRequest req, string prop, Json.Object tree)
97                 {
98                         var project = req.file.project;
99                         var ls = new GLib.ListStore(typeof(CompileError));
100                         if (!tree.has_member(prop)) {
101                                 GLib.debug("Files with %s : 0", prop);
102                                 req.errorByType.set(prop,ls);
103                                 return;
104                         }
105                         var res = tree.get_object_member(prop);
106                         res.foreach_member((obj, file, node) => {
107                 
108                         var fe = project.getByPath(file);
109                          
110                         if (fe == null) {
111                                 GLib.debug("Warning Can not find file %s", file);
112                                 return;
113                         }
114                         
115  
116                         
117                         
118                         var ce = new CompileError.new_file(fe, res.get_object_member(file), prop);
119                         ls.append(ce);
120                         
121                         if (!req.errorByFile.has_key(fe.targetName())) {
122                                 GLib.debug("add file %s to req.errorByFile", fe.targetName());
123                                 req.errorByFile.set(fe.targetName(), new GLib.ListStore(typeof(CompileError)));
124                         }
125                                 for(var i = 0; i < ce.lines.get_n_items(); i++) {
126                                         var lce = (CompileError) ce.lines.get_item(i);
127                                 GLib.debug("add error %s to %s", lce.msg, fe.targetName());                     
128                                 req.errorByFile.get(fe.targetName()).append(lce);
129                         }
130                                 
131                         
132               
133                     });
134                         GLib.debug("Files with %s : %d", prop, (int) ls.get_n_items());
135                     req.errorByType.set(prop,ls);
136                     
137                 }
138                 
139         // only used by javascript /roo errors..
140                 public static GLib.ListStore jsonToListStore(Project.Project project, Json.Object tree)
141                 {
142                         var ls = new GLib.ListStore(typeof(CompileError));
143                 tree.foreach_member((obj, file, node) => {
144                 
145                         var fe = project.getByPath(file);
146                          
147                         if (fe == null) {
148                                 GLib.debug("Warning Can not find file %s", file);
149                                 return;
150                         }
151                         var ce = new CompileError.new_file(fe, tree.get_object_member(file), "ERR");
152                         ls.append(ce);
153                  
154              
155                     
156                     });
157                     return ls;
158           
159                 
160                 }
161          
162                 
163         }
164         
165 }