meson.build.o7QLX02
[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 { get; set ; }  // so it triggers updates?
17
18                 //public CompileError? parent = null;
19                 public string category = "";
20                 public string msg = "";
21                 public  int line { get; set; default = -1; }
22
23                 
24                 public CompileError.new_jserror(JsRender.JsRender file, string category, int line, string msg) 
25                 {
26                         this.lines = new GLib.ListStore(typeof(CompileError));
27                         this.line = line;
28                         this.msg = msg;
29                         this.file = file;
30                         this.category = category;
31
32                 
33                 }
34
35                 public CompileError.new_from_diagnostic(JsRender.JsRender file, Lsp.Diagnostic diag) 
36                 {
37                         this.file = file;
38                         this.category = diag.category;
39                         this.line = (int) diag.range.start.line;
40                         this.msg = diag.message;   
41                         this.lines = new GLib.ListStore(typeof(CompileError));
42                         //GLib.debug("new error %s : %d  %s %s", file.path, this.line, this.category, this.msg);
43                         
44                         
45                         
46                 }
47                 
48                 public CompileError.new_from_file(JsRender.JsRender file, string category) 
49                 {
50                         this.file = file;
51                         this.category = category;
52                         this.lines = file.getErrors(category);
53                         this.title =  file.relpath + " (" + lines.get_n_items().to_string() + ")";
54                 }
55
56 /*
57                 public CompileError.new_line(CompileError? parent, int line, string msg) 
58                 {
59                         this.lines = new GLib.ListStore(typeof(CompileError));
60                         this.parent = parent;
61                         this.line = line;
62                         this.msg = msg;
63                         this.file = parent.file;
64                         this.category = parent.category;
65                          
66                 
67                 }
68                 public CompileError.new_file(JsRender.JsRender file, Json.Object jlines, string category) 
69                 {
70                         this.file = file;
71                         this.category = category;
72                         this.title =  file.relpath + " (" + jlines.get_size().to_string() + ")";
73                         
74             this.lines = new GLib.ListStore(typeof(CompileError));
75             
76             jlines.foreach_member((obja, line, nodea) => {
77                 var msg  = "";
78                 var ar = jlines.get_array_member(line);
79                 
80                 
81                 
82                 for (var i = 0 ; i < ar.get_length(); i++) {
83                                 msg += (msg.length > 0) ? "\n" : "";
84                                 msg += ar.get_string_element(i);
85                     }
86                     this.lines.append(new CompileError.new_line(this, int.parse(line) ,msg));
87          
88             
89             });
90               
91                 }
92                 */
93                 public string file_line { // sorting?
94                         set {}
95                         owned get { 
96                                 return this.line == -1 ? this.file.relpath : 
97                                         (this.file.relpath + ":" + this.line.to_string("%09d")); 
98                         }
99                 }
100                 public string linemsg {
101                         set {}
102                         owned  get {
103                                 return this.line == -1 ? 
104                                          GLib.Markup.escape_text( this.file.relpath + "(" +  this.lines.n_items.to_string() + ")") :                    
105                                          GLib.Markup.escape_text(this.line.to_string() + ": " + this.msg);
106                         }
107                 }
108                 
109                 public bool hasErrors() {
110                         return this.lines.get_n_items() > 0;
111                 }
112                 
113                 /*
114                  
115                 public static void parseCompileResults (ValaCompileRequest req, Json.Object tree)
116                 {
117                         //req.errorByFile = new Gee.HashMap<string,GLib.ListStore>();
118                         //req.errorByType = new Gee.HashMap<string,GLib.ListStore>();
119                         
120                         
121
122                         req.errorByType.set("ERR",  new GLib.ListStore(typeof(CompileError)));
123                         req.errorByType.set("WARN",  new GLib.ListStore(typeof(CompileError)));
124                         req.errorByType.set("DEPR",  new GLib.ListStore(typeof(CompileError)));                         
125  
126                         jsonToListStoreProp(req, "WARN", tree);
127                         jsonToListStoreProp(req, "ERR", tree);   
128                         jsonToListStoreProp(req, "DEPR", tree);  
129                         
130                          
131                 }
132         
133                 
134                 public static void jsonToListStoreProp(ValaCompileRequest req, string prop, Json.Object tree)
135                 {
136                         var project = req.file.project;
137                         var ls = new GLib.ListStore(typeof(CompileError));
138                         if (!tree.has_member(prop)) {
139                                 GLib.debug("Files with %s : 0", prop);
140                                 req.errorByType.set(prop,ls);
141                                 return;
142                         }
143                         var res = tree.get_object_member(prop);
144                         res.foreach_member((obj, file, node) => {
145                 
146                         var fe = project.getByPath(file);
147                          
148                         if (fe == null) {
149                                 GLib.debug("Warning Can not find file %s", file);
150                                 return;
151                         }
152                         
153  
154                         
155                         
156                         var ce = new CompileError.new_file(fe, res.get_object_member(file), prop);
157                         ls.append(ce);
158                         
159                         if (!req.errorByFile.has_key(fe.targetName())) {
160                                 GLib.debug("add file %s to req.errorByFile", fe.targetName());
161                                 req.errorByFile.set(fe.targetName(), new GLib.ListStore(typeof(CompileError)));
162                         }
163                                 for(var i = 0; i < ce.lines.get_n_items(); i++) {
164                                         var lce = (CompileError) ce.lines.get_item(i);
165                                 GLib.debug("add error %s to %s", lce.msg, fe.targetName());                     
166                                 req.errorByFile.get(fe.targetName()).append(lce);
167                         }
168                                 
169                         
170               
171                     });
172                         GLib.debug("Files with %s : %d", prop, (int) ls.get_n_items());
173                     req.errorByType.set(prop,ls);
174                     
175                 }
176                         
177         // only used by javascript /roo errors..
178                 public static GLib.ListStore jsonToListStore(Project.Project project, Json.Object tree)
179                 {
180                         var ls = new GLib.ListStore(typeof(CompileError));
181                 tree.foreach_member((obj, file, node) => {
182                 
183                         var fe = project.getByPath(file);
184                          
185                         if (fe == null) {
186                                 GLib.debug("Warning Can not find file %s", file);
187                                 return;
188                         }
189                         var ce = new CompileError.new_file(fe, tree.get_object_member(file), "ERR");
190                         ls.append(ce);
191                  
192              
193                     
194                     });
195                     return ls;
196           
197                 
198                 }
199          */
200                 
201         }
202         
203 }