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