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