src/Builder4/Editor.bjs
[app.Builder.js] / src / Builder4 / Editor.vala
1 static Editor  _Editor;
2
3 public class Editor : Object 
4 {
5     public Gtk.VBox el;
6     private Editor  _this;
7
8     public static Editor singleton()
9     {
10         if (_Editor == null) {
11             _Editor= new Editor();
12         }
13         return _Editor;
14     }
15     public Xcls_save_button save_button;
16     public Xcls_key_edit key_edit;
17     public Xcls_RightEditor RightEditor;
18     public Xcls_view view;
19     public Xcls_buffer buffer;
20
21         // my vars (def)
22     public Xcls_MainWindow window;
23     public string activeEditor;
24     public int pos_root_x;
25     public int pos_root_y;
26     public string ptype;
27     public string key;
28     public JsRender.JsRender file;
29     public bool pos;
30     public bool dirty;
31     public signal void save ();
32     public JsRender.Node node;
33
34     // ctor 
35     public Editor()
36     {
37         _this = this;
38         this.el = new Gtk.VBox( false, 0 );
39
40         // my vars (dec)
41         this.window = null;
42         this.activeEditor = "";
43         this.ptype = "";
44         this.key = "";
45         this.file = null;
46         this.pos = false;
47         this.dirty = false;
48         this.node = null;
49
50         // set gobject values
51         var child_0 = new Xcls_HBox2( _this );
52         child_0.ref();
53         this.el.pack_start (  child_0.el , false,true );
54         var child_1 = new Xcls_RightEditor( _this );
55         child_1.ref();
56         this.el.add (  child_1.el  );
57     }
58
59     // user defined functions 
60     public   bool saveContents ()  {
61         
62         
63         
64         
65         
66         
67         
68        
69          
70          var str = _this.buffer.toString();
71          
72           
73          
74          
75          if (!_this.buffer.checkSyntax()) {
76              print("check syntax failed");
77              //this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
78              //return false;
79          }
80          
81          // LeftPanel.model.changed(  str , false);
82          _this.dirty = false;
83          _this.save_button.el.sensitive = false;
84          
85          
86             
87          
88         // find the text for the node..
89         if (ptype == "listener") {
90             this.node.listeners.set(key,str);
91         
92         } else {
93              this.node.props.set(key,str);
94         }
95     
96          
97         
98         // call the signal..
99         this.save();
100         
101         return true;
102     
103     }
104     public   void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
105     {
106         this.ptype = ptype;
107         this.key  = key;
108         this.node = node;
109         this.file = file;
110         
111        string val = "";
112         // find the text for the node..
113         if (ptype == "listener") {
114             val = node.listeners.get(key);
115         
116         } else {
117             val = node.props.get(key);
118         }
119         this.view.load(val);
120         this.key_edit.el.text = key;    
121     
122     }
123     public class Xcls_HBox2 : Object 
124     {
125         public Gtk.HBox el;
126         private Editor  _this;
127
128
129             // my vars (def)
130
131         // ctor 
132         public Xcls_HBox2(Editor _owner )
133         {
134             _this = _owner;
135             this.el = new Gtk.HBox( false, 0 );
136
137             // my vars (dec)
138
139             // set gobject values
140             var child_0 = new Xcls_save_button( _this );
141             child_0.ref();
142             this.el.pack_start (  child_0.el , false,false );
143             var child_1 = new Xcls_key_edit( _this );
144             child_1.ref();
145             this.el.pack_end (  child_1.el , true,true );
146         }
147
148         // user defined functions 
149     }
150     public class Xcls_save_button : Object 
151     {
152         public Gtk.Button el;
153         private Editor  _this;
154
155
156             // my vars (def)
157
158         // ctor 
159         public Xcls_save_button(Editor _owner )
160         {
161             _this = _owner;
162             _this.save_button = this;
163             this.el = new Gtk.Button();
164
165             // my vars (dec)
166
167             // set gobject values
168             this.el.label = "Save";
169
170             // listeners 
171             this.el.clicked.connect( () => { 
172                 _this.saveContents();
173             });
174         }
175
176         // user defined functions 
177     }
178     public class Xcls_key_edit : Object 
179     {
180         public Gtk.Entry el;
181         private Editor  _this;
182
183
184             // my vars (def)
185
186         // ctor 
187         public Xcls_key_edit(Editor _owner )
188         {
189             _this = _owner;
190             _this.key_edit = this;
191             this.el = new Gtk.Entry();
192
193             // my vars (dec)
194
195             // set gobject values
196         }
197
198         // user defined functions 
199     }
200     public class Xcls_RightEditor : Object 
201     {
202         public Gtk.ScrolledWindow el;
203         private Editor  _this;
204
205
206             // my vars (def)
207
208         // ctor 
209         public Xcls_RightEditor(Editor _owner )
210         {
211             _this = _owner;
212             _this.RightEditor = this;
213             this.el = new Gtk.ScrolledWindow( null, null );
214
215             // my vars (dec)
216
217             // set gobject values
218             var child_0 = new Xcls_view( _this );
219             child_0.ref();
220             this.el.add (  child_0.el  );
221
222             // init method 
223
224             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
225         }
226
227         // user defined functions 
228     }
229     public class Xcls_view : Object 
230     {
231         public Gtk.SourceView el;
232         private Editor  _this;
233
234
235             // my vars (def)
236
237         // ctor 
238         public Xcls_view(Editor _owner )
239         {
240             _this = _owner;
241             _this.view = this;
242             this.el = new Gtk.SourceView();
243
244             // my vars (dec)
245
246             // set gobject values
247             this.el.auto_indent = true;
248             this.el.indent_width = 4;
249             this.el.show_line_marks = true;
250             this.el.insert_spaces_instead_of_tabs = true;
251             this.el.show_line_numbers = true;
252             this.el.highlight_current_line = true;
253             var child_0 = new Xcls_buffer( _this );
254             child_0.ref();
255             this.el.set_buffer (  child_0.el  );
256
257             // init method 
258
259             var description =   Pango.FontDescription.from_string("monospace");
260                 description.set_size(8000);
261                 this.el.override_font(description);
262             
263                 var attrs = new Gtk.SourceMarkAttributes();
264                 var  pink = new Gdk.RGBA();
265                 pink.parse ( "pink");
266                 attrs.set_background ( pink);
267                 attrs.set_icon_name ( "process-stop");    
268                 attrs.query_tooltip_text.connect(( mark) => {
269                     //print("tooltip query? %s\n", mark.name);
270                     return mark.name;
271                 });
272                 
273                 this.el.set_mark_attributes ("error", attrs, 1);
274
275             // listeners 
276             this.el.key_release_event.connect( (event) => {
277                 
278                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
279                     print("SAVE: ctrl-S  pressed");
280                     _this.saveContents();
281                     return false;
282                 }
283                // print(event.key.keyval)
284                 
285                 return false;
286             
287             });
288         }
289
290         // user defined functions 
291         public   void load (string str) {
292         
293         // show the help page for the active node..
294            //this.get('/Help').show();
295         
296         
297           // this.get('/BottomPane').el.set_current_page(0);
298             this.el.get_buffer().set_text(str, str.length);
299             var lm = Gtk.SourceLanguageManager.get_default();
300             
301             var lang = _this.file.language;
302             //?? is javascript going to work as js?
303             
304             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
305             var buf = this.el.get_buffer();
306              
307             _this.dirty = false;
308             this.el.grab_focus();
309             _this.save_button.el.sensitive = false;
310         }
311     }
312     public class Xcls_buffer : Object 
313     {
314         public Gtk.SourceBuffer el;
315         private Editor  _this;
316
317
318             // my vars (def)
319         public bool check_queued;
320         public int error_line;
321         public bool check_running;
322
323         // ctor 
324         public Xcls_buffer(Editor _owner )
325         {
326             _this = _owner;
327             _this.buffer = this;
328             this.el = new Gtk.SourceBuffer( null );
329
330             // my vars (dec)
331             this.check_queued = false;
332             this.error_line = -1;
333             this.check_running = false;
334
335             // set gobject values
336
337             // listeners 
338             this.el.changed.connect( () => {
339                 // check syntax??
340                 // ??needed..??
341                 _this.save_button.el.sensitive = true;
342                 print("EDITOR CHANGED");
343                 this.checkSyntax();
344                
345                 _this.dirty = true;
346             
347                 // this.get('/LeftPanel.model').changed(  str , false);
348                 return ;
349             });
350         }
351
352         // user defined functions 
353         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
354                 this.error_line = validate_res.size;
355         
356                 if (this.error_line < 1) {
357                       return true;
358                 }
359                 var tlines = this.el.get_line_count ();
360                 Gtk.TextIter iter;
361                 var valiter = validate_res.map_iterator();
362                 while (valiter.next()) {
363                 
364             //        print("get inter\n");
365                     var eline = valiter.get_key();
366                     if (eline > tlines) {
367                         continue;
368                     }
369                     this.el.get_iter_at_line( out iter, eline);
370                     //print("mark line\n");
371                     this.el.create_source_mark(valiter.get_value(), "error", iter);
372                 }   
373                 return false;
374             }
375         public   string toString () {
376             
377             Gtk.TextIter s;
378             Gtk.TextIter e;
379             this.el.get_start_iter(out s);
380             this.el.get_end_iter(out e);
381             var ret = this.el.get_text(s,e,true);
382             //print("TO STRING? " + ret);
383             return ret;
384         }
385         public   bool checkSyntax () {
386          
387             if (this.check_running) {
388                 print("Check is running\n");
389                 if (this.check_queued) { 
390                     print("Check is already queued");
391                     return true;
392                 }
393                 this.check_queued = true;
394                 print("Adding queued Check ");
395                 GLib.Timeout.add_seconds(1, () => {
396                     this.check_queued = false;
397                     
398                     this.checkSyntax();
399                     return false;
400                 });
401             
402         
403                 return true;
404             }
405            
406            
407             var p = Palete.factory(_this.file.xtype);   
408             
409             var str = this.toString();
410             
411             string res = "";
412             
413             if (this.error_line > 0) {
414                  Gtk.TextIter start;
415                  Gtk.TextIter end;     
416                 this.el.get_bounds (out start, out end);
417         
418                 this.el.remove_source_marks (start, end, null);
419             }
420             
421             if (str.length < 1) {
422                 print("checkSyntax - empty string?\n");
423                 return true;
424             }
425             this.check_running = true;
426             
427             
428             if (_this.file.language == "js") {
429                 this.check_running = false;
430                 print("calling validate javascript\n"); 
431                 return this.highlightErrors(p.validateJavascript(
432                     str, 
433                      _this.key, 
434                     _this.ptype,
435                     _this.file,
436                     _this.node
437                 ));    
438                 
439             }
440                 
441                 
442             print("calling validate vala\n");    
443             // clear the buttons.
444             _this.window.statusbar_errors.el.hide();
445             _this.window.statusbar_warnings.el.hide();
446             _this.window.statusbar_depricated.el.hide();
447             
448             
449             
450             p.validateVala(
451                 str, 
452                  _this.key, 
453                 _this.ptype,
454                 _this.file,
455                 _this.node,
456                 (obj) => {
457                     this.check_running = false;
458                     this.highlightErrorsJson(obj);            
459                     
460                     //var validate_res = p.validateVala.end(res);
461                     //this.highlightErrors(validate_res);
462         
463                 }
464                 
465             );
466              
467             
468             
469             //print("done mark line\n");
470              
471             return true; // at present allow saving - even if it's invalid..
472         }
473         public bool highlightErrorsJson (Json.Object obj) {
474               Gtk.TextIter start;
475              Gtk.TextIter end;     
476                 this.el.get_bounds (out start, out end);
477                 
478                 this.el.remove_source_marks (start, end, null);
479                         
480                         
481              _this.window.statusbar_errors.el.hide();
482              _this.window.statusbar_warnings.el.hide();
483              _this.window.statusbar_depricated.el.hide();       
484                         
485             
486             if (obj.has_member("ERR-TOTAL")) {
487                 _this.window.statusbar_errors.el.show();
488                  _this.window.statusbar_errors.notices = obj.get_object_member("ERR");
489                 _this.window.statusbar_errors.el.label = obj.get_int_member("ERR-TOTAL").to_string() + " Errors";
490             }
491             
492             if (obj.has_member("WARN-TOTAL")) {
493                 _this.window.statusbar_warnings.el.show();
494                  _this.window.statusbar_warnings.notices = obj.get_object_member("WARN");
495                 _this.window.statusbar_warnings.el.label = obj.get_int_member("WARN-TOTAL").to_string() + " Warnings";
496             }
497             if (obj.has_member("DEPR-TOTAL")) {
498                 _this.window.statusbar_depricated.el.show();    
499                 _this.window.statusbar_depricated.notices = obj.get_object_member("DEPR");
500                 _this.window.statusbar_depricated.el.label = obj.get_int_member("DEPR-TOTAL").to_string() + " Depricated";
501             }
502             
503             if (!obj.has_member("ERR")) {
504                 print("Return has no errors\n");
505                 return true;
506             }
507             var err = obj.get_object_member("ERR");
508             
509             
510             
511             if (!err.has_member(_this.file.path)) {
512                 print("File path has no errors\n");
513                 return true;
514             }
515         
516             var lines = err.get_object_member(_this.file.path);
517             
518             var offset = 1;
519             if (obj.has_member("line_offset")) {
520                 offset = (int)obj.get_int_member("line_offset") + 1;
521             }
522              
523             
524             var tlines = this.el.get_line_count ();
525             
526             lines.foreach_member((obj, line, node) => {
527                 
528                      Gtk.TextIter iter;
529             //        print("get inter\n");
530                     var eline = int.parse(line) - offset;
531                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
532                     
533                     
534                     if (eline > tlines || eline < 0) {
535                         return;
536                     }
537                     this.el.get_iter_at_line( out iter, eline);
538                     //print("mark line\n");
539                     var msg  = "Line: %d".printf(eline+1);
540                     var ar = lines.get_array_member(line);
541                     for (var i = 0 ; i < ar.get_length(); i++) {
542                             msg += (msg.length > 0) ? "\n" : "";
543                             msg += ar.get_string_element(i);
544                     }
545                     
546                     
547                     this.el.create_source_mark(msg, "error", iter);
548                 } );
549                 return false;
550             
551         
552         
553         
554         
555         }
556     }
557 }