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.contents = 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     
135     }
136     public class Xcls_Box2 : Object
137     {
138         public Gtk.Box el;
139         private Editor  _this;
140
141
142             // my vars (def)
143
144         // ctor
145         public Xcls_Box2(Editor _owner )
146         {
147             _this = _owner;
148             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
149
150             // my vars (dec)
151
152             // set gobject values
153             this.el.homogeneous = false;
154             var child_0 = new Xcls_save_button( _this );
155             child_0.ref();
156             this.el.pack_start (  child_0.el , false,false );
157             var child_1 = new Xcls_key_edit( _this );
158             child_1.ref();
159             this.el.pack_end (  child_1.el , true,true );
160         }
161
162         // user defined functions
163     }
164     public class Xcls_save_button : Object
165     {
166         public Gtk.Button el;
167         private Editor  _this;
168
169
170             // my vars (def)
171
172         // ctor
173         public Xcls_save_button(Editor _owner )
174         {
175             _this = _owner;
176             _this.save_button = this;
177             this.el = new Gtk.Button();
178
179             // my vars (dec)
180
181             // set gobject values
182             this.el.label = "Save";
183
184             //listeners
185             this.el.clicked.connect( () => { 
186                 _this.saveContents();
187             });
188         }
189
190         // user defined functions
191     }
192
193     public class Xcls_key_edit : Object
194     {
195         public Gtk.Entry el;
196         private Editor  _this;
197
198
199             // my vars (def)
200
201         // ctor
202         public Xcls_key_edit(Editor _owner )
203         {
204             _this = _owner;
205             _this.key_edit = this;
206             this.el = new Gtk.Entry();
207
208             // my vars (dec)
209
210             // set gobject values
211         }
212
213         // user defined functions
214     }
215
216
217     public class Xcls_RightEditor : Object
218     {
219         public Gtk.ScrolledWindow el;
220         private Editor  _this;
221
222
223             // my vars (def)
224
225         // ctor
226         public Xcls_RightEditor(Editor _owner )
227         {
228             _this = _owner;
229             _this.RightEditor = this;
230             this.el = new Gtk.ScrolledWindow( null, null );
231
232             // my vars (dec)
233
234             // set gobject values
235             var child_0 = new Xcls_view( _this );
236             child_0.ref();
237             this.el.add (  child_0.el  );
238
239             // init method
240
241             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
242         }
243
244         // user defined functions
245     }
246     public class Xcls_view : Object
247     {
248         public Gtk.SourceView el;
249         private Editor  _this;
250
251
252             // my vars (def)
253
254         // ctor
255         public Xcls_view(Editor _owner )
256         {
257             _this = _owner;
258             _this.view = this;
259             this.el = new Gtk.SourceView();
260
261             // my vars (dec)
262
263             // set gobject values
264             this.el.auto_indent = true;
265             this.el.indent_width = 4;
266             this.el.show_line_marks = true;
267             this.el.insert_spaces_instead_of_tabs = true;
268             this.el.show_line_numbers = true;
269             this.el.highlight_current_line = true;
270             var child_0 = new Xcls_buffer( _this );
271             child_0.ref();
272             this.el.set_buffer (  child_0.el  );
273
274             // init method
275
276             var description =   Pango.FontDescription.from_string("monospace");
277                 description.set_size(8000);
278                 this.el.override_font(description);
279             
280             
281               
282                 var attrs = new Gtk.SourceMarkAttributes();
283                 var  pink =   Gdk.RGBA();
284                 pink.parse ( "pink");
285                 attrs.set_background ( pink);
286                 attrs.set_icon_name ( "process-stop");    
287                 attrs.query_tooltip_text.connect(( mark) => {
288                     //print("tooltip query? %s\n", mark.name);
289                     return mark.name;
290                 });
291                 
292                 this.el.set_mark_attributes ("ERR", attrs, 1);
293                 
294                  var wattrs = new Gtk.SourceMarkAttributes();
295                 var  blue =   Gdk.RGBA();
296                 blue.parse ( "#ABF4EB");
297                 wattrs.set_background ( blue);
298                 wattrs.set_icon_name ( "process-stop");    
299                 wattrs.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 ("WARN", wattrs, 1);
305                 
306              
307                 
308                  var dattrs = new Gtk.SourceMarkAttributes();
309                 var  purple =   Gdk.RGBA();
310                 purple.parse ( "#EEA9FF");
311                 dattrs.set_background ( purple);
312                 dattrs.set_icon_name ( "process-stop");    
313                 dattrs.query_tooltip_text.connect(( mark) => {
314                     //print("tooltip query? %s\n", mark.name);
315                     return mark.name;
316                 });
317                 
318                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
319
320             //listeners
321             this.el.key_release_event.connect( (event) => {
322                 
323                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
324                     print("SAVE: ctrl-S  pressed");
325                     _this.saveContents();
326                     return false;
327                 }
328                // print(event.key.keyval)
329                 
330                 return false;
331             
332             });
333         }
334
335         // user defined functions
336         public   void load (string str) {
337         
338         // show the help page for the active node..
339            //this.get('/Help').show();
340         
341         
342           // this.get('/BottomPane').el.set_current_page(0);
343           var buf = (Gtk.SourceBuffer)this.el.get_buffer();
344             buf.set_text(str, str.length);
345             buf.set_undo_manager(null);
346             
347             var lm = Gtk.SourceLanguageManager.get_default();
348             var lang = "vala";
349             if (_this.file != null) {
350                  lang = _this.file.language;
351             }
352             //?? is javascript going to work as js?
353             
354             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
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 == null && _this.fname.length > 0) {
472             
473                 // assume it's gtk...
474                    this.check_running = true;
475         
476                  _this.window.windowstate.valasource.checkPlainFileSpawn(
477                     _this.window.project,
478                     _this.fname,
479                     str
480                  );
481                 return true;
482             
483             }
484            if (_this.file == null) {
485                return true;
486            }
487             var p = Palete.factory(_this.file.xtype);   
488             
489         
490              
491             this.check_running = true;
492             
493             
494             if (_this.file.language == "js") {
495                 this.check_running = false;
496                 print("calling validate javascript\n"); 
497                 return this.highlightErrors(p.validateJavascript(
498                     str, 
499                      _this.key, 
500                     _this.ptype,
501                     _this.file,
502                     _this.node
503                 ));    
504                 
505             }
506                 
507                 
508             print("calling validate vala\n");    
509             // clear the buttons.
510          
511             
512             p.validateVala(
513                 _this.window.windowstate,
514                 str, 
515                  _this.key, 
516                 _this.ptype,
517                 _this.file,
518                 _this.node 
519                 
520                 
521             );
522              
523             
524             
525             //print("done mark line\n");
526              
527             return true; // at present allow saving - even if it's invalid..
528         }
529         public bool highlightErrorsJson (string type, Json.Object obj) {
530               Gtk.TextIter start;
531              Gtk.TextIter end;     
532                 this.el.get_bounds (out start, out end);
533                 
534                 this.el.remove_source_marks (start, end, type);
535                          
536              
537              // we should highlight other types of errors..
538             
539             if (!obj.has_member(type)) {
540                 print("Return has no errors\n");
541                 return true;
542             }
543             
544             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
545                 _this.window.windowstate.state != WindowState.State.CODE
546                 ) {
547                 return true;
548             } 
549             
550             
551             var err = obj.get_object_member(type);
552             var valafn = _this.fname;
553             if (_this.file != null) {
554         
555         
556                 
557                 
558                  valafn = "";
559                   try {             
560                        var  regex = new Regex("\\.bjs$");
561                     
562                      
563                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
564                      } catch (GLib.RegexError e) {
565                         return true;
566                     }   
567         
568         
569         
570               }
571                if (!err.has_member(valafn)) {
572                     print("File path has no errors\n");
573                     return  true;
574                 }
575         
576                 var lines = err.get_object_member(valafn);
577                 
578                 var offset = 1;
579                 if (obj.has_member("line_offset")) {
580                     offset = (int)obj.get_int_member("line_offset") + 1;
581                 }
582             
583         
584              
585             
586             var tlines = this.el.get_line_count () +1;
587             
588             lines.foreach_member((obj, line, node) => {
589                 
590                      Gtk.TextIter iter;
591             //        print("get inter\n");
592                     var eline = int.parse(line) - offset;
593                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
594                     
595                     
596                     if (eline > tlines || eline < 0) {
597                         return;
598                     }
599                     this.el.get_iter_at_line( out iter, eline);
600                     //print("mark line\n");
601                     var msg  = "Line: %d".printf(eline+1);
602                     var ar = lines.get_array_member(line);
603                     for (var i = 0 ; i < ar.get_length(); i++) {
604                             msg += (msg.length > 0) ? "\n" : "";
605                             msg += ar.get_string_element(i);
606                     }
607                     
608                     
609                     this.el.create_source_mark(msg, type, iter);
610                 } );
611                 return false;
612             
613         
614         
615         
616         
617         }
618     }
619
620
621
622 }