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
179     public class Xcls_key_edit : Object
180     {
181         public Gtk.Entry el;
182         private Editor  _this;
183
184
185             // my vars (def)
186
187         // ctor
188         public Xcls_key_edit(Editor _owner )
189         {
190             _this = _owner;
191             _this.key_edit = this;
192             this.el = new Gtk.Entry();
193
194             // my vars (dec)
195
196             // set gobject values
197         }
198
199         // user defined functions
200     }
201
202
203     public class Xcls_RightEditor : Object
204     {
205         public Gtk.ScrolledWindow el;
206         private Editor  _this;
207
208
209             // my vars (def)
210
211         // ctor
212         public Xcls_RightEditor(Editor _owner )
213         {
214             _this = _owner;
215             _this.RightEditor = this;
216             this.el = new Gtk.ScrolledWindow( null, null );
217
218             // my vars (dec)
219
220             // set gobject values
221             var child_0 = new Xcls_view( _this );
222             child_0.ref();
223             this.el.add (  child_0.el  );
224
225             // init method
226
227             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
228         }
229
230         // user defined functions
231     }
232     public class Xcls_view : Object
233     {
234         public Gtk.SourceView el;
235         private Editor  _this;
236
237
238             // my vars (def)
239
240         // ctor
241         public Xcls_view(Editor _owner )
242         {
243             _this = _owner;
244             _this.view = this;
245             this.el = new Gtk.SourceView();
246
247             // my vars (dec)
248
249             // set gobject values
250             this.el.auto_indent = true;
251             this.el.indent_width = 4;
252             this.el.show_line_marks = true;
253             this.el.insert_spaces_instead_of_tabs = true;
254             this.el.show_line_numbers = true;
255             this.el.highlight_current_line = true;
256             var child_0 = new Xcls_buffer( _this );
257             child_0.ref();
258             this.el.set_buffer (  child_0.el  );
259
260             // init method
261
262             var description =   Pango.FontDescription.from_string("monospace");
263                 description.set_size(8000);
264                 this.el.override_font(description);
265             
266             
267               
268                 var attrs = new Gtk.SourceMarkAttributes();
269                 var  pink = new Gdk.RGBA();
270                 pink.parse ( "pink");
271                 attrs.set_background ( pink);
272                 attrs.set_icon_name ( "process-stop");    
273                 attrs.query_tooltip_text.connect(( mark) => {
274                     //print("tooltip query? %s\n", mark.name);
275                     return mark.name;
276                 });
277                 
278                 this.el.set_mark_attributes ("ERR", attrs, 1);
279                 
280                  var wattrs = new Gtk.SourceMarkAttributes();
281                 var  blue = new Gdk.RGBA();
282                 blue.parse ( "blue");
283                 wattrs.set_background ( blue);
284                 wattrs.set_icon_name ( "process-stop");    
285                 wattrs.query_tooltip_text.connect(( mark) => {
286                     //print("tooltip query? %s\n", mark.name);
287                     return mark.name;
288                 });
289                 
290                 this.el.set_mark_attributes ("WARN", wattrs, 1);
291                 
292              
293                 
294                  var dattrs = new Gtk.SourceMarkAttributes();
295                 var  purple = new Gdk.RGBA();
296                 purple.parse ( "purple");
297                 dattrs.set_background ( purple);
298                 dattrs.set_icon_name ( "process-stop");    
299                 dattrs.query_tooltip_text.connect(( mark) => {
300                     //print("tooltip query? %s\n", mark.name);
301                     return mark.name;
302                 });
303                 
304                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
305
306             //listeners
307             this.el.key_release_event.connect( (event) => {
308                 
309                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
310                     print("SAVE: ctrl-S  pressed");
311                     _this.saveContents();
312                     return false;
313                 }
314                // print(event.key.keyval)
315                 
316                 return false;
317             
318             });
319         }
320
321         // user defined functions
322         public   void load (string str) {
323         
324         // show the help page for the active node..
325            //this.get('/Help').show();
326         
327         
328           // this.get('/BottomPane').el.set_current_page(0);
329             this.el.get_buffer().set_text(str, str.length);
330             var lm = Gtk.SourceLanguageManager.get_default();
331             
332             var lang = _this.file.language;
333             //?? is javascript going to work as js?
334             
335             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
336             var buf = this.el.get_buffer();
337              
338             _this.dirty = false;
339             this.el.grab_focus();
340             _this.save_button.el.sensitive = false;
341         }
342     }
343     public class Xcls_buffer : Object
344     {
345         public Gtk.SourceBuffer el;
346         private Editor  _this;
347
348
349             // my vars (def)
350         public bool check_queued;
351         public int error_line;
352         public bool check_running;
353
354         // ctor
355         public Xcls_buffer(Editor _owner )
356         {
357             _this = _owner;
358             _this.buffer = this;
359             this.el = new Gtk.SourceBuffer( null );
360
361             // my vars (dec)
362             this.check_queued = false;
363             this.error_line = -1;
364             this.check_running = false;
365
366             // set gobject values
367
368             //listeners
369             this.el.changed.connect( () => {
370                 // check syntax??
371                 // ??needed..??
372                 _this.save_button.el.sensitive = true;
373                 print("EDITOR CHANGED");
374                 this.checkSyntax();
375                
376                 _this.dirty = true;
377             
378                 // this.get('/LeftPanel.model').changed(  str , false);
379                 return ;
380             });
381         }
382
383         // user defined functions
384         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
385                 this.error_line = validate_res.size;
386         
387                 if (this.error_line < 1) {
388                       return true;
389                 }
390                 var tlines = this.el.get_line_count ();
391                 Gtk.TextIter iter;
392                 var valiter = validate_res.map_iterator();
393                 while (valiter.next()) {
394                 
395             //        print("get inter\n");
396                     var eline = valiter.get_key();
397                     if (eline > tlines) {
398                         continue;
399                     }
400                     this.el.get_iter_at_line( out iter, eline);
401                     //print("mark line\n");
402                     this.el.create_source_mark(valiter.get_value(), "error", iter);
403                 }   
404                 return false;
405             }
406         public   string toString () {
407             
408             Gtk.TextIter s;
409             Gtk.TextIter e;
410             this.el.get_start_iter(out s);
411             this.el.get_end_iter(out e);
412             var ret = this.el.get_text(s,e,true);
413             //print("TO STRING? " + ret);
414             return ret;
415         }
416         public   bool checkSyntax () {
417          
418             if (this.check_running) {
419                 print("Check is running\n");
420                 if (this.check_queued) { 
421                     print("Check is already queued");
422                     return true;
423                 }
424                 this.check_queued = true;
425                 print("Adding queued Check ");
426                 GLib.Timeout.add_seconds(1, () => {
427                     this.check_queued = false;
428                     
429                     this.checkSyntax();
430                     return false;
431                 });
432             
433         
434                 return true;
435             }
436            
437            
438             var p = Palete.factory(_this.file.xtype);   
439             
440             var str = this.toString();
441             
442             string res = "";
443             
444             if (this.error_line > 0) {
445                  Gtk.TextIter start;
446                  Gtk.TextIter end;     
447                 this.el.get_bounds (out start, out end);
448         
449                 this.el.remove_source_marks (start, end, null);
450             }
451             
452             if (str.length < 1) {
453                 print("checkSyntax - empty string?\n");
454                 return true;
455             }
456             this.check_running = true;
457             
458             
459             if (_this.file.language == "js") {
460                 this.check_running = false;
461                 print("calling validate javascript\n"); 
462                 return this.highlightErrors(p.validateJavascript(
463                     str, 
464                      _this.key, 
465                     _this.ptype,
466                     _this.file,
467                     _this.node
468                 ));    
469                 
470             }
471                 
472                 
473             print("calling validate vala\n");    
474             // clear the buttons.
475             _this.window.statusbar_errors.el.hide();
476             _this.window.statusbar_warnings.el.hide();
477             _this.window.statusbar_depricated.el.hide();
478             
479             
480             
481             p.validateVala(
482                 str, 
483                  _this.key, 
484                 _this.ptype,
485                 _this.file,
486                 _this.node,
487                 (obj) => {
488                     this.check_running = false;
489                     this.highlightErrorsJson(obj);            
490                     
491                     //var validate_res = p.validateVala.end(res);
492                     //this.highlightErrors(validate_res);
493         
494                 }
495                 
496             );
497              
498             
499             
500             //print("done mark line\n");
501              
502             return true; // at present allow saving - even if it's invalid..
503         }
504         public bool highlightErrorsJson (string type, Json.Object obj) {
505               Gtk.TextIter start;
506              Gtk.TextIter end;     
507                 this.el.get_bounds (out start, out end);
508                 
509                 this.el.remove_source_marks (start, end, type);
510                          
511              
512              // we should highlight other types of errors..
513             
514             if (!obj.has_member(type)) {
515                 print("Return has no errors\n");
516                 return true;
517             }
518             var err = obj.get_object_member(type);
519             
520             
521             
522             if (!err.has_member(_this.file.path)) {
523                 print("File path has no errors\n");
524                 return true;
525             }
526         
527             var lines = err.get_object_member(_this.file.path);
528             
529             var offset = 1;
530             if (obj.has_member("line_offset")) {
531                 offset = (int)obj.get_int_member("line_offset") + 1;
532             }
533              
534             
535             var tlines = this.el.get_line_count () +1;
536             
537             lines.foreach_member((obj, line, node) => {
538                 
539                      Gtk.TextIter iter;
540             //        print("get inter\n");
541                     var eline = int.parse(line) - offset;
542                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
543                     
544                     
545                     if (eline > tlines || eline < 0) {
546                         return;
547                     }
548                     this.el.get_iter_at_line( out iter, eline);
549                     //print("mark line\n");
550                     var msg  = "Line: %d".printf(eline+1);
551                     var ar = lines.get_array_member(line);
552                     for (var i = 0 ; i < ar.get_length(); i++) {
553                             msg += (msg.length > 0) ? "\n" : "";
554                             msg += ar.get_string_element(i);
555                     }
556                     
557                     
558                     this.el.create_source_mark(msg, type, iter);
559                 } );
560                 return false;
561             
562         
563         
564         
565         
566         }
567     }
568
569
570
571 }