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