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