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          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.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
267             this.el.tab_width = 4;
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                 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
281                 this.el.completion.unblock_interactive();
282                 this.el.completion.select_on_show                       = true; // select
283                 this.el.completion.show_headers                 = false;
284                 this.el.completion.remember_info_visibility             = true;
285                 
286               
287                 var attrs = new Gtk.SourceMarkAttributes();
288                 var  pink =   Gdk.RGBA();
289                 pink.parse ( "pink");
290                 attrs.set_background ( pink);
291                 attrs.set_icon_name ( "process-stop");    
292                 attrs.query_tooltip_text.connect(( mark) => {
293                     //print("tooltip query? %s\n", mark.name);
294                     return mark.name;
295                 });
296                 
297                 this.el.set_mark_attributes ("ERR", attrs, 1);
298                 
299                  var wattrs = new Gtk.SourceMarkAttributes();
300                 var  blue =   Gdk.RGBA();
301                 blue.parse ( "#ABF4EB");
302                 wattrs.set_background ( blue);
303                 wattrs.set_icon_name ( "process-stop");    
304                 wattrs.query_tooltip_text.connect(( mark) => {
305                     //print("tooltip query? %s\n", mark.name);
306                     return mark.name;
307                 });
308                 
309                 this.el.set_mark_attributes ("WARN", wattrs, 1);
310                 
311              
312                 
313                  var dattrs = new Gtk.SourceMarkAttributes();
314                 var  purple =   Gdk.RGBA();
315                 purple.parse ( "#EEA9FF");
316                 dattrs.set_background ( purple);
317                 dattrs.set_icon_name ( "process-stop");    
318                 dattrs.query_tooltip_text.connect(( mark) => {
319                     //print("tooltip query? %s\n", mark.name);
320                     return mark.name;
321                 });
322                 
323                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
324
325             //listeners
326             this.el.key_release_event.connect( (event) => {
327                 
328                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
329                     print("SAVE: ctrl-S  pressed");
330                     _this.saveContents();
331                     return false;
332                 }
333                // print(event.key.keyval)
334                 
335                 return false;
336             
337             });
338         }
339
340         // user defined functions
341         public   void load (string str) {
342         
343         // show the help page for the active node..
344            //this.get('/Help').show();
345         
346         
347           // this.get('/BottomPane').el.set_current_page(0);
348             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
349             buf.set_text(str, str.length);
350             buf.set_undo_manager(null);
351             
352             var lm = Gtk.SourceLanguageManager.get_default();
353             var lang = "vala";
354             if (_this.file != null) {
355                  lang = _this.file.language;
356             }
357             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
358             var lg = _this.file.content_type.length > 0  ?
359                     lm.guess_language(_this.file.path, _this.file.content_type) :
360                     lm.get_language(lang);
361              
362            
363             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
364         
365             this.el.insert_spaces_instead_of_tabs = true;
366             if (lg != null) {
367                         print("sourcelanguage  = %s\n", lg.name);
368                         if (lg.name == "Vala") {
369                             this.el.insert_spaces_instead_of_tabs = false;
370                         }
371              }
372             _this.dirty = false;
373             this.el.grab_focus();
374             _this.save_button.el.sensitive = false;
375         }
376     }
377     public class Xcls_buffer : Object
378     {
379         public Gtk.SourceBuffer el;
380         private Editor  _this;
381
382
383             // my vars (def)
384         public bool check_queued;
385         public int error_line;
386         public bool check_running;
387
388         // ctor
389         public Xcls_buffer(Editor _owner )
390         {
391             _this = _owner;
392             _this.buffer = this;
393             this.el = new Gtk.SourceBuffer( null );
394
395             // my vars (dec)
396             this.check_queued = false;
397             this.error_line = -1;
398             this.check_running = false;
399
400             // set gobject values
401
402             //listeners
403             this.el.changed.connect( () => {
404                 // check syntax??
405                 // ??needed..??
406                 _this.save_button.el.sensitive = true;
407                 print("EDITOR CHANGED");
408                 this.checkSyntax();
409                
410                 _this.dirty = true;
411             
412                 // this.get('/LeftPanel.model').changed(  str , false);
413                 return ;
414             });
415         }
416
417         // user defined functions
418         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
419                  
420                 this.error_line = validate_res.size;
421         
422                 if (this.error_line < 1) {
423                       return true;
424                 }
425                 var tlines = this.el.get_line_count ();
426                 Gtk.TextIter iter;
427                 var valiter = validate_res.map_iterator();
428                 while (valiter.next()) {
429                 
430             //        print("get inter\n");
431                     var eline = valiter.get_key();
432                     if (eline > tlines) {
433                         continue;
434                     }
435                     this.el.get_iter_at_line( out iter, eline);
436                     //print("mark line\n");
437                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
438                 }   
439                 return false;
440             }
441         public   string toString () {
442             
443             Gtk.TextIter s;
444             Gtk.TextIter e;
445             this.el.get_start_iter(out s);
446             this.el.get_end_iter(out e);
447             var ret = this.el.get_text(s,e,true);
448             //print("TO STRING? " + ret);
449             return ret;
450         }
451         public   bool checkSyntax () {
452          
453             if (this.check_running) {
454                 print("Check is running\n");
455                 if (this.check_queued) { 
456                     print("Check is already queued");
457                     return true;
458                 }
459                 this.check_queued = true;
460                 print("Adding queued Check ");
461                 GLib.Timeout.add_seconds(1, () => {
462                     this.check_queued = false;
463                     
464                     this.checkSyntax();
465                     return false;
466                 });
467             
468         
469                 return true;
470             }
471             var str = this.toString();
472             
473             // needed???
474             if (this.error_line > 0) {
475                  Gtk.TextIter start;
476                  Gtk.TextIter end;     
477                 this.el.get_bounds (out start, out end);
478         
479                 this.el.remove_source_marks (start, end, null);
480             }
481             if (str.length < 1) {
482                 print("checkSyntax - empty string?\n");
483                 return true;
484             }
485             
486             if (_this.file.xtype == "PlainFile") {
487             
488                 // assume it's gtk...
489                    this.check_running = true;
490         
491                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
492                    _this.file,
493                     str
494                  )) {
495                     this.check_running = false;
496                 }
497                 
498                 return true;
499             
500             }
501            if (_this.file == null) {
502                return true;
503            }
504             var p = Palete.factory(_this.file.xtype);   
505             
506         
507              
508             this.check_running = true;
509             
510             
511             if (_this.file.language == "js") {
512                 this.check_running = false;
513                 print("calling validate javascript\n"); 
514                 return this.highlightErrors(p.validateJavascript(
515                     str, 
516                      _this.key, 
517                     _this.ptype,
518                     _this.file,
519                     _this.node
520                 ));    
521                 
522             }
523                 
524                 
525             print("calling validate vala\n");    
526             // clear the buttons.
527          
528             
529            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
530                 _this.file,
531                 _this.node,
532                  _this.key,        
533                  _this.ptype,
534                     str
535                 )) {
536                 this.check_running = false;
537             } 
538              
539             
540             
541             //print("done mark line\n");
542              
543             return true; // at present allow saving - even if it's invalid..
544         }
545         public bool highlightErrorsJson (string type, Json.Object obj) {
546               Gtk.TextIter start;
547              Gtk.TextIter end;     
548                 this.el.get_bounds (out start, out end);
549                 
550                 this.el.remove_source_marks (start, end, type);
551                          
552              
553              // we should highlight other types of errors..
554             
555             if (!obj.has_member(type)) {
556                 print("Return has no errors\n");
557                 return true;
558             }
559             
560             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
561                 _this.window.windowstate.state != WindowState.State.CODE
562                 ) {
563                 return true;
564             } 
565             
566             
567             var err = obj.get_object_member(type);
568             
569             
570             if (_this.file == null) {
571                 return true;
572             
573             }
574             var valafn = _this.file.path;
575          
576             if (_this.file.xtype != "PlainFile") {
577         
578         
579                 
580                 
581                  valafn = "";
582                   try {             
583                        var  regex = new Regex("\\.bjs$");
584                        // should not happen
585                       
586                      
587                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
588                      } catch (GLib.RegexError e) {
589                         return true;
590                     }   
591         
592         
593         
594               }
595                if (!err.has_member(valafn)) {
596                     print("File path has no errors\n");
597                     return  true;
598                 }
599         
600                 var lines = err.get_object_member(valafn);
601                 
602                 var offset = 1;
603                 if (obj.has_member("line_offset")) {
604                     offset = (int)obj.get_int_member("line_offset") + 1;
605                 }
606             
607         
608              
609             
610             var tlines = this.el.get_line_count () +1;
611             
612             lines.foreach_member((obj, line, node) => {
613                 
614                      Gtk.TextIter iter;
615             //        print("get inter\n");
616                     var eline = int.parse(line) - offset;
617                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
618                     
619                     
620                     if (eline > tlines || eline < 0) {
621                         return;
622                     }
623                     this.el.get_iter_at_line( out iter, eline);
624                     //print("mark line\n");
625                     var msg  = "Line: %d".printf(eline+1);
626                     var ar = lines.get_array_member(line);
627                     for (var i = 0 ; i < ar.get_length(); i++) {
628                             msg += (msg.length > 0) ? "\n" : "";
629                             msg += ar.get_string_element(i);
630                     }
631                     
632                     
633                     this.el.create_source_mark(msg, type, iter);
634                 } );
635                 return false;
636             
637         
638         
639         
640         
641         }
642     }
643
644
645
646 }