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