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