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 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.Box( Gtk.Orientation.VERTICAL, 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         this.el.homogeneous = false;
52         var child_0 = new Xcls_Box2( _this );
53         child_0.ref();
54         this.el.pack_start (  child_0.el , false,true );
55         var child_1 = new Xcls_RightEditor( _this );
56         child_1.ref();
57         this.el.pack_end (  child_1.el , true,true );
58     }
59
60     // user defined functions
61     public   bool saveContents ()  {
62         
63         
64         if (_this.file == null) {
65             return true;
66         }
67         
68         
69         
70         
71        
72          
73          var str = _this.buffer.toString();
74          
75          _this.buffer.checkSyntax();
76          
77          
78          
79          // LeftPanel.model.changed(  str , false);
80          _this.dirty = false;
81          _this.save_button.el.sensitive = false;
82          
83         // find the text for the node..
84         if (_this.file.xtype != "PlainFile") {
85             if (ptype == "listener") {
86                 this.node.listeners.set(key,str);
87             
88             } else {
89                  this.node.props.set(key,str);
90             }
91         } else {
92             _this.file.setSource(  str );
93          }
94         
95         // call the signal..
96         this.save();
97         
98         return true;
99     
100     }
101     public   void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
102     {
103         this.file = file;    
104         this.ptype = "";
105         this.key  = "";
106         this.node = null;
107     
108         
109         if (file.xtype != "PlainFile") {
110         
111             this.ptype = ptype;
112             this.key  = key;
113             this.node = node;
114              string val = "";
115             // find the text for the node..
116             if (ptype == "listener") {
117                 val = node.listeners.get(key);
118             
119             } else {
120                 val = node.props.get(key);
121             }
122             this.view.load(val);
123             this.key_edit.el.show();
124             this.key_edit.el.text = key;  
125         
126         } else {
127             this.view.load(        file.toSource() );
128             this.key_edit.el.hide();
129         }
130     
131            
132     }
133     public class Xcls_Box2 : Object
134     {
135         public Gtk.Box el;
136         private Editor  _this;
137
138
139             // my vars (def)
140
141         // ctor
142         public Xcls_Box2(Editor _owner )
143         {
144             _this = _owner;
145             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
146
147             // my vars (dec)
148
149             // set gobject values
150             this.el.homogeneous = false;
151             var child_0 = new Xcls_save_button( _this );
152             child_0.ref();
153             this.el.pack_start (  child_0.el , false,false );
154             var child_1 = new Xcls_key_edit( _this );
155             child_1.ref();
156             this.el.pack_end (  child_1.el , true,true );
157         }
158
159         // user defined functions
160     }
161     public class Xcls_save_button : Object
162     {
163         public Gtk.Button el;
164         private Editor  _this;
165
166
167             // my vars (def)
168
169         // ctor
170         public Xcls_save_button(Editor _owner )
171         {
172             _this = _owner;
173             _this.save_button = this;
174             this.el = new Gtk.Button();
175
176             // my vars (dec)
177
178             // set gobject values
179             this.el.label = "Save";
180
181             //listeners
182             this.el.clicked.connect( () => { 
183                 _this.saveContents();
184             });
185         }
186
187         // user defined functions
188     }
189
190     public class Xcls_key_edit : Object
191     {
192         public Gtk.Entry el;
193         private Editor  _this;
194
195
196             // my vars (def)
197
198         // ctor
199         public Xcls_key_edit(Editor _owner )
200         {
201             _this = _owner;
202             _this.key_edit = this;
203             this.el = new Gtk.Entry();
204
205             // my vars (dec)
206
207             // set gobject values
208         }
209
210         // user defined functions
211     }
212
213
214     public class Xcls_RightEditor : Object
215     {
216         public Gtk.ScrolledWindow el;
217         private Editor  _this;
218
219
220             // my vars (def)
221
222         // ctor
223         public Xcls_RightEditor(Editor _owner )
224         {
225             _this = _owner;
226             _this.RightEditor = this;
227             this.el = new Gtk.ScrolledWindow( null, null );
228
229             // my vars (dec)
230
231             // set gobject values
232             var child_0 = new Xcls_view( _this );
233             child_0.ref();
234             this.el.add (  child_0.el  );
235
236             // init method
237
238             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
239         }
240
241         // user defined functions
242     }
243     public class Xcls_view : Object
244     {
245         public Gtk.SourceView el;
246         private Editor  _this;
247
248
249             // my vars (def)
250
251         // ctor
252         public Xcls_view(Editor _owner )
253         {
254             _this = _owner;
255             _this.view = this;
256             this.el = new Gtk.SourceView();
257
258             // my vars (dec)
259
260             // set gobject values
261             this.el.auto_indent = true;
262             this.el.indent_width = 4;
263             this.el.show_line_marks = true;
264             this.el.insert_spaces_instead_of_tabs = true;
265             this.el.show_line_numbers = true;
266             this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.SPACE;
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             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
351             var lg = _this.file.content_type.length > 0  ?
352                     lm.guess_language(_this.file.path, _this.file.content_type) :
353                     lm.get_language(lang);
354             
355            
356            
357             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
358         
359             this.el.insert_spaces_instead_of_tabs = true;
360             if (lg.name == "vala") {
361                 this.el.insert_spaces_instead_of_tabs = false;
362             }
363              
364             _this.dirty = false;
365             this.el.grab_focus();
366             _this.save_button.el.sensitive = false;
367         }
368     }
369     public class Xcls_buffer : Object
370     {
371         public Gtk.SourceBuffer el;
372         private Editor  _this;
373
374
375             // my vars (def)
376         public bool check_queued;
377         public int error_line;
378         public bool check_running;
379
380         // ctor
381         public Xcls_buffer(Editor _owner )
382         {
383             _this = _owner;
384             _this.buffer = this;
385             this.el = new Gtk.SourceBuffer( null );
386
387             // my vars (dec)
388             this.check_queued = false;
389             this.error_line = -1;
390             this.check_running = false;
391
392             // set gobject values
393
394             //listeners
395             this.el.changed.connect( () => {
396                 // check syntax??
397                 // ??needed..??
398                 _this.save_button.el.sensitive = true;
399                 print("EDITOR CHANGED");
400                 this.checkSyntax();
401                
402                 _this.dirty = true;
403             
404                 // this.get('/LeftPanel.model').changed(  str , false);
405                 return ;
406             });
407         }
408
409         // user defined functions
410         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
411                  
412                 this.error_line = validate_res.size;
413         
414                 if (this.error_line < 1) {
415                       return true;
416                 }
417                 var tlines = this.el.get_line_count ();
418                 Gtk.TextIter iter;
419                 var valiter = validate_res.map_iterator();
420                 while (valiter.next()) {
421                 
422             //        print("get inter\n");
423                     var eline = valiter.get_key();
424                     if (eline > tlines) {
425                         continue;
426                     }
427                     this.el.get_iter_at_line( out iter, eline);
428                     //print("mark line\n");
429                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
430                 }   
431                 return false;
432             }
433         public   string toString () {
434             
435             Gtk.TextIter s;
436             Gtk.TextIter e;
437             this.el.get_start_iter(out s);
438             this.el.get_end_iter(out e);
439             var ret = this.el.get_text(s,e,true);
440             //print("TO STRING? " + ret);
441             return ret;
442         }
443         public   bool checkSyntax () {
444          
445             if (this.check_running) {
446                 print("Check is running\n");
447                 if (this.check_queued) { 
448                     print("Check is already queued");
449                     return true;
450                 }
451                 this.check_queued = true;
452                 print("Adding queued Check ");
453                 GLib.Timeout.add_seconds(1, () => {
454                     this.check_queued = false;
455                     
456                     this.checkSyntax();
457                     return false;
458                 });
459             
460         
461                 return true;
462             }
463             var str = this.toString();
464             
465             // needed???
466             if (this.error_line > 0) {
467                  Gtk.TextIter start;
468                  Gtk.TextIter end;     
469                 this.el.get_bounds (out start, out end);
470         
471                 this.el.remove_source_marks (start, end, null);
472             }
473             if (str.length < 1) {
474                 print("checkSyntax - empty string?\n");
475                 return true;
476             }
477             
478             if (_this.file.xtype == "PlainFile") {
479             
480                 // assume it's gtk...
481                    this.check_running = true;
482         
483                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
484                    _this.file,
485                     str
486                  )) {
487                     this.check_running = false;
488                 }
489                 
490                 return true;
491             
492             }
493            if (_this.file == null) {
494                return true;
495            }
496             var p = Palete.factory(_this.file.xtype);   
497             
498         
499              
500             this.check_running = true;
501             
502             
503             if (_this.file.language == "js") {
504                 this.check_running = false;
505                 print("calling validate javascript\n"); 
506                 return this.highlightErrors(p.validateJavascript(
507                     str, 
508                      _this.key, 
509                     _this.ptype,
510                     _this.file,
511                     _this.node
512                 ));    
513                 
514             }
515                 
516                 
517             print("calling validate vala\n");    
518             // clear the buttons.
519          
520             
521            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
522                 _this.file,
523                 _this.node,
524                  _this.key,        
525                  _this.ptype,
526                     str
527                 )) {
528                 this.check_running = false;
529             } 
530              
531             
532             
533             //print("done mark line\n");
534              
535             return true; // at present allow saving - even if it's invalid..
536         }
537         public bool highlightErrorsJson (string type, Json.Object obj) {
538               Gtk.TextIter start;
539              Gtk.TextIter end;     
540                 this.el.get_bounds (out start, out end);
541                 
542                 this.el.remove_source_marks (start, end, type);
543                          
544              
545              // we should highlight other types of errors..
546             
547             if (!obj.has_member(type)) {
548                 print("Return has no errors\n");
549                 return true;
550             }
551             
552             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
553                 _this.window.windowstate.state != WindowState.State.CODE
554                 ) {
555                 return true;
556             } 
557             
558             
559             var err = obj.get_object_member(type);
560             
561             
562             if (_this.file == null) {
563                 return true;
564             
565             }
566             var valafn = _this.file.path;
567          
568             if (_this.file.xtype != "PlainFile") {
569         
570         
571                 
572                 
573                  valafn = "";
574                   try {             
575                        var  regex = new Regex("\\.bjs$");
576                        // should not happen
577                       
578                      
579                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
580                      } catch (GLib.RegexError e) {
581                         return true;
582                     }   
583         
584         
585         
586               }
587                if (!err.has_member(valafn)) {
588                     print("File path has no errors\n");
589                     return  true;
590                 }
591         
592                 var lines = err.get_object_member(valafn);
593                 
594                 var offset = 1;
595                 if (obj.has_member("line_offset")) {
596                     offset = (int)obj.get_int_member("line_offset") + 1;
597                 }
598             
599         
600              
601             
602             var tlines = this.el.get_line_count () +1;
603             
604             lines.foreach_member((obj, line, node) => {
605                 
606                      Gtk.TextIter iter;
607             //        print("get inter\n");
608                     var eline = int.parse(line) - offset;
609                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
610                     
611                     
612                     if (eline > tlines || eline < 0) {
613                         return;
614                     }
615                     this.el.get_iter_at_line( out iter, eline);
616                     //print("mark line\n");
617                     var msg  = "Line: %d".printf(eline+1);
618                     var ar = lines.get_array_member(line);
619                     for (var i = 0 ; i < ar.get_length(); i++) {
620                             msg += (msg.length > 0) ? "\n" : "";
621                             msg += ar.get_string_element(i);
622                     }
623                     
624                     
625                     this.el.create_source_mark(msg, type, iter);
626                 } );
627                 return false;
628             
629         
630         
631         
632         
633         }
634     }
635
636
637
638 }