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