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             //?? is javascript going to work as js?
349             
350             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
351          
352              
353             _this.dirty = false;
354             this.el.grab_focus();
355             _this.save_button.el.sensitive = false;
356         }
357     }
358     public class Xcls_buffer : Object
359     {
360         public Gtk.SourceBuffer el;
361         private Editor  _this;
362
363
364             // my vars (def)
365         public bool check_queued;
366         public int error_line;
367         public bool check_running;
368
369         // ctor
370         public Xcls_buffer(Editor _owner )
371         {
372             _this = _owner;
373             _this.buffer = this;
374             this.el = new Gtk.SourceBuffer( null );
375
376             // my vars (dec)
377             this.check_queued = false;
378             this.error_line = -1;
379             this.check_running = false;
380
381             // set gobject values
382
383             //listeners
384             this.el.changed.connect( () => {
385                 // check syntax??
386                 // ??needed..??
387                 _this.save_button.el.sensitive = true;
388                 print("EDITOR CHANGED");
389                 this.checkSyntax();
390                
391                 _this.dirty = true;
392             
393                 // this.get('/LeftPanel.model').changed(  str , false);
394                 return ;
395             });
396         }
397
398         // user defined functions
399         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
400                  
401                 this.error_line = validate_res.size;
402         
403                 if (this.error_line < 1) {
404                       return true;
405                 }
406                 var tlines = this.el.get_line_count ();
407                 Gtk.TextIter iter;
408                 var valiter = validate_res.map_iterator();
409                 while (valiter.next()) {
410                 
411             //        print("get inter\n");
412                     var eline = valiter.get_key();
413                     if (eline > tlines) {
414                         continue;
415                     }
416                     this.el.get_iter_at_line( out iter, eline);
417                     //print("mark line\n");
418                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
419                 }   
420                 return false;
421             }
422         public   string toString () {
423             
424             Gtk.TextIter s;
425             Gtk.TextIter e;
426             this.el.get_start_iter(out s);
427             this.el.get_end_iter(out e);
428             var ret = this.el.get_text(s,e,true);
429             //print("TO STRING? " + ret);
430             return ret;
431         }
432         public   bool checkSyntax () {
433          
434             if (this.check_running) {
435                 print("Check is running\n");
436                 if (this.check_queued) { 
437                     print("Check is already queued");
438                     return true;
439                 }
440                 this.check_queued = true;
441                 print("Adding queued Check ");
442                 GLib.Timeout.add_seconds(1, () => {
443                     this.check_queued = false;
444                     
445                     this.checkSyntax();
446                     return false;
447                 });
448             
449         
450                 return true;
451             }
452             var str = this.toString();
453             
454             // needed???
455             if (this.error_line > 0) {
456                  Gtk.TextIter start;
457                  Gtk.TextIter end;     
458                 this.el.get_bounds (out start, out end);
459         
460                 this.el.remove_source_marks (start, end, null);
461             }
462             if (str.length < 1) {
463                 print("checkSyntax - empty string?\n");
464                 return true;
465             }
466             
467             if (_this.file.xtype == "PlainFile") {
468             
469                 // assume it's gtk...
470                    this.check_running = true;
471         
472                  _this.window.windowstate.valasource.checkPlainFileSpawn(
473                    _this.file,
474                     str
475                  );
476                 return true;
477             
478             }
479            if (_this.file == null) {
480                return true;
481            }
482             var p = Palete.factory(_this.file.xtype);   
483             
484         
485              
486             this.check_running = true;
487             
488             
489             if (_this.file.language == "js") {
490                 this.check_running = false;
491                 print("calling validate javascript\n"); 
492                 return this.highlightErrors(p.validateJavascript(
493                     str, 
494                      _this.key, 
495                     _this.ptype,
496                     _this.file,
497                     _this.node
498                 ));    
499                 
500             }
501                 
502                 
503             print("calling validate vala\n");    
504             // clear the buttons.
505          
506             
507             p.validateVala(
508                 _this.window.windowstate,
509                 str, 
510                  _this.key, 
511                 _this.ptype,
512                 _this.file,
513                 _this.node 
514                 
515                 
516             );
517              
518             
519             
520             //print("done mark line\n");
521              
522             return true; // at present allow saving - even if it's invalid..
523         }
524         public bool highlightErrorsJson (string type, Json.Object obj) {
525               Gtk.TextIter start;
526              Gtk.TextIter end;     
527                 this.el.get_bounds (out start, out end);
528                 
529                 this.el.remove_source_marks (start, end, type);
530                          
531              
532              // we should highlight other types of errors..
533             
534             if (!obj.has_member(type)) {
535                 print("Return has no errors\n");
536                 return true;
537             }
538             
539             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
540                 _this.window.windowstate.state != WindowState.State.CODE
541                 ) {
542                 return true;
543             } 
544             
545             
546             var err = obj.get_object_member(type);
547             
548             
549             if (_this.file == null) {
550                 return;
551             
552             }
553             var valafn = _this.file.path;
554             print("%s : %s", _this.file.path, _this.file.xtype);
555             if (_this.file.xtype != "PlainFile") {
556         
557         
558                 
559                 
560                  valafn = "";
561                   try {             
562                        var  regex = new Regex("\\.bjs$");
563                     
564                      
565                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
566                      } catch (GLib.RegexError e) {
567                         return true;
568                     }   
569         
570         
571         
572               }
573                if (!err.has_member(valafn)) {
574                     print("File path has no errors\n");
575                     return  true;
576                 }
577         
578                 var lines = err.get_object_member(valafn);
579                 
580                 var offset = 1;
581                 if (obj.has_member("line_offset")) {
582                     offset = (int)obj.get_int_member("line_offset") + 1;
583                 }
584             
585         
586              
587             
588             var tlines = this.el.get_line_count () +1;
589             
590             lines.foreach_member((obj, line, node) => {
591                 
592                      Gtk.TextIter iter;
593             //        print("get inter\n");
594                     var eline = int.parse(line) - offset;
595                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
596                     
597                     
598                     if (eline > tlines || eline < 0) {
599                         return;
600                     }
601                     this.el.get_iter_at_line( out iter, eline);
602                     //print("mark line\n");
603                     var msg  = "Line: %d".printf(eline+1);
604                     var ar = lines.get_array_member(line);
605                     for (var i = 0 ; i < ar.get_length(); i++) {
606                             msg += (msg.length > 0) ? "\n" : "";
607                             msg += ar.get_string_element(i);
608                     }
609                     
610                     
611                     this.el.create_source_mark(msg, type, iter);
612                 } );
613                 return false;
614             
615         
616         
617         
618         
619         }
620     }
621
622
623
624 }