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 int last_search_end;
28     public string key;
29     public Gtk.SourceSearchContext searchcontext;
30     public JsRender.JsRender file;
31     public bool pos;
32     public bool dirty;
33     public signal void save ();
34     public JsRender.Node node;
35
36     // ctor
37     public Editor()
38     {
39         _this = this;
40         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
41
42         // my vars (dec)
43         this.window = null;
44         this.activeEditor = "";
45         this.ptype = "";
46         this.last_search_end = 0;
47         this.key = "";
48         this.searchcontext = null;
49         this.file = null;
50         this.pos = false;
51         this.dirty = false;
52         this.node = null;
53
54         // set gobject values
55         this.el.homogeneous = false;
56         this.el.hexpand = true;
57         var child_0 = new Xcls_Box2( _this );
58         child_0.ref();
59         this.el.pack_start (  child_0.el , false,true );
60         var child_1 = new Xcls_RightEditor( _this );
61         child_1.ref();
62         this.el.pack_end (  child_1.el , true,true );
63     }
64
65     // user defined functions
66     public   bool saveContents ()  {
67         
68         
69         if (_this.file == null) {
70             return true;
71         }
72         
73         
74        
75        
76          
77          var str = _this.buffer.toString();
78          
79          _this.buffer.checkSyntax();
80          
81          
82          
83          // LeftPanel.model.changed(  str , false);
84          _this.dirty = false;
85          _this.save_button.el.sensitive = false;
86          
87         // find the text for the node..
88         if (_this.file.xtype != "PlainFile") {
89             if (ptype == "listener") {
90                 this.node.listeners.set(key,str);
91             
92             } else {
93                  this.node.props.set(key,str);
94             }
95         } else {
96             _this.file.setSource(  str );
97          }
98         
99         // call the signal..
100         this.save();
101         
102         return true;
103     
104     }
105     public void scroll_to_line (int line) {
106     
107         GLib.Timeout.add(500, () => {
108        
109                 var buf = this.view.el.get_buffer();
110     
111                 var sbuf = (Gtk.SourceBuffer) buf;
112     
113     
114                 Gtk.TextIter iter;   
115                 sbuf.get_iter_at_line(out iter,  line);
116                 this.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
117                 return false;
118         });   
119     }
120     public int search (string txt) {
121     
122         var s = new Gtk.SourceSearchSettings();
123         
124         this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
125         this.searchcontext .set_highlight(true);
126         s.set_search_text(txt);
127         Gtk.TextIter beg, st,en;
128          
129         this.buffer.el.get_start_iter(out beg);
130         this.searchcontext.forward(beg, out st, out en);
131         
132         
133         return this.searchcontext.get_occurrences_count();
134     
135      
136        
137     
138     }
139     public   void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
140     {
141         this.file = file;    
142         this.ptype = "";
143         this.key  = "";
144         this.node = null;
145         this.searchcontext = null;
146         
147         if (file.xtype != "PlainFile") {
148         
149             this.ptype = ptype;
150             this.key  = key;
151             this.node = node;
152              string val = "";
153             // find the text for the node..
154             if (ptype == "listener") {
155                 val = node.listeners.get(key);
156             
157             } else {
158                 val = node.props.get(key);
159             }
160             this.view.load(val);
161             this.key_edit.el.show();
162             this.key_edit.el.text = key;  
163         
164         } else {
165             this.view.load(        file.toSource() );
166             this.key_edit.el.hide();
167         }
168     
169            
170     }
171     public void forwardSearch () {
172     
173         if (this.searchcontext == null) {
174                 return;
175         }
176         
177     
178     }
179     public class Xcls_Box2 : Object
180     {
181         public Gtk.Box el;
182         private Editor  _this;
183
184
185             // my vars (def)
186
187         // ctor
188         public Xcls_Box2(Editor _owner )
189         {
190             _this = _owner;
191             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
192
193             // my vars (dec)
194
195             // set gobject values
196             this.el.homogeneous = false;
197             var child_0 = new Xcls_save_button( _this );
198             child_0.ref();
199             this.el.pack_start (  child_0.el , false,false );
200             var child_1 = new Xcls_key_edit( _this );
201             child_1.ref();
202             this.el.pack_end (  child_1.el , true,true );
203         }
204
205         // user defined functions
206     }
207     public class Xcls_save_button : Object
208     {
209         public Gtk.Button el;
210         private Editor  _this;
211
212
213             // my vars (def)
214
215         // ctor
216         public Xcls_save_button(Editor _owner )
217         {
218             _this = _owner;
219             _this.save_button = this;
220             this.el = new Gtk.Button();
221
222             // my vars (dec)
223
224             // set gobject values
225             this.el.label = "Save";
226
227             //listeners
228             this.el.clicked.connect( () => { 
229                 _this.saveContents();
230             });
231         }
232
233         // user defined functions
234     }
235
236     public class Xcls_key_edit : Object
237     {
238         public Gtk.Entry el;
239         private Editor  _this;
240
241
242             // my vars (def)
243
244         // ctor
245         public Xcls_key_edit(Editor _owner )
246         {
247             _this = _owner;
248             _this.key_edit = this;
249             this.el = new Gtk.Entry();
250
251             // my vars (dec)
252
253             // set gobject values
254         }
255
256         // user defined functions
257     }
258
259
260     public class Xcls_RightEditor : Object
261     {
262         public Gtk.ScrolledWindow el;
263         private Editor  _this;
264
265
266             // my vars (def)
267
268         // ctor
269         public Xcls_RightEditor(Editor _owner )
270         {
271             _this = _owner;
272             _this.RightEditor = this;
273             this.el = new Gtk.ScrolledWindow( null, null );
274
275             // my vars (dec)
276
277             // set gobject values
278             var child_0 = new Xcls_view( _this );
279             child_0.ref();
280             this.el.add (  child_0.el  );
281
282             // init method
283
284             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
285         }
286
287         // user defined functions
288     }
289     public class Xcls_view : Object
290     {
291         public Gtk.SourceView el;
292         private Editor  _this;
293
294
295             // my vars (def)
296
297         // ctor
298         public Xcls_view(Editor _owner )
299         {
300             _this = _owner;
301             _this.view = this;
302             this.el = new Gtk.SourceView();
303
304             // my vars (dec)
305
306             // set gobject values
307             this.el.auto_indent = true;
308             this.el.indent_width = 4;
309             this.el.show_line_marks = true;
310             this.el.insert_spaces_instead_of_tabs = true;
311             this.el.show_line_numbers = true;
312             this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
313             this.el.tab_width = 4;
314             this.el.highlight_current_line = true;
315             var child_0 = new Xcls_buffer( _this );
316             child_0.ref();
317             this.el.set_buffer (  child_0.el  );
318
319             // init method
320
321             var description =   Pango.FontDescription.from_string("monospace");
322                         description.set_size(8000);
323                          this.el.override_font(description);
324                 
325                
326                 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
327                 this.el.completion.unblock_interactive();
328                 this.el.completion.select_on_show                       = true; // select
329                 this.el.completion.show_headers                 = false;
330                 this.el.completion.remember_info_visibility             = true;
331                 
332               
333                 var attrs = new Gtk.SourceMarkAttributes();
334                 var  pink =   Gdk.RGBA();
335                 pink.parse ( "pink");
336                 attrs.set_background ( pink);
337                 attrs.set_icon_name ( "process-stop");    
338                 attrs.query_tooltip_text.connect(( mark) => {
339                     //print("tooltip query? %s\n", mark.name);
340                     return mark.name;
341                 });
342                 
343                 this.el.set_mark_attributes ("ERR", attrs, 1);
344                 
345                  var wattrs = new Gtk.SourceMarkAttributes();
346                 var  blue =   Gdk.RGBA();
347                 blue.parse ( "#ABF4EB");
348                 wattrs.set_background ( blue);
349                 wattrs.set_icon_name ( "process-stop");    
350                 wattrs.query_tooltip_text.connect(( mark) => {
351                     //print("tooltip query? %s\n", mark.name);
352                     return mark.name;
353                 });
354                 
355                 this.el.set_mark_attributes ("WARN", wattrs, 1);
356                 
357              
358                 
359                  var dattrs = new Gtk.SourceMarkAttributes();
360                 var  purple =   Gdk.RGBA();
361                 purple.parse ( "#EEA9FF");
362                 dattrs.set_background ( purple);
363                 dattrs.set_icon_name ( "process-stop");    
364                 dattrs.query_tooltip_text.connect(( mark) => {
365                     //print("tooltip query? %s\n", mark.name);
366                     return mark.name;
367                 });
368                 
369                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
370
371             //listeners
372             this.el.key_release_event.connect( (event) => {
373                 
374                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
375                     print("SAVE: ctrl-S  pressed");
376                     _this.saveContents();
377                     return false;
378                 }
379                // print(event.key.keyval)
380                 
381                 return false;
382             
383             });
384         }
385
386         // user defined functions
387         public   void load (string str) {
388         
389         // show the help page for the active node..
390            //this.get('/Help').show();
391         
392         
393           // this.get('/BottomPane').el.set_current_page(0);
394             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
395             buf.set_text(str, str.length);
396             buf.set_undo_manager(null);
397             
398             var lm = Gtk.SourceLanguageManager.get_default();
399             var lang = "vala";
400             if (_this.file != null) {
401                  lang = _this.file.language;
402             }
403             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
404             var lg = _this.file.content_type.length > 0  ?
405                     lm.guess_language(_this.file.path, _this.file.content_type) :
406                     lm.get_language(lang);
407              
408            
409             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
410         
411             this.el.insert_spaces_instead_of_tabs = true;
412             if (lg != null) {
413                         print("sourcelanguage  = %s\n", lg.name);
414                         if (lg.name == "Vala") {
415                             this.el.insert_spaces_instead_of_tabs = false;
416                         }
417              }
418             _this.dirty = false;
419             this.el.grab_focus();
420             _this.save_button.el.sensitive = false;
421         }
422     }
423     public class Xcls_buffer : Object
424     {
425         public Gtk.SourceBuffer el;
426         private Editor  _this;
427
428
429             // my vars (def)
430         public bool check_queued;
431         public int error_line;
432         public bool check_running;
433
434         // ctor
435         public Xcls_buffer(Editor _owner )
436         {
437             _this = _owner;
438             _this.buffer = this;
439             this.el = new Gtk.SourceBuffer( null );
440
441             // my vars (dec)
442             this.check_queued = false;
443             this.error_line = -1;
444             this.check_running = false;
445
446             // set gobject values
447
448             //listeners
449             this.el.changed.connect( () => {
450                 // check syntax??
451                 // ??needed..??
452                 _this.save_button.el.sensitive = true;
453                 print("EDITOR CHANGED");
454                 this.checkSyntax();
455                
456                 _this.dirty = true;
457             
458                 // this.get('/LeftPanel.model').changed(  str , false);
459                 return ;
460             });
461         }
462
463         // user defined functions
464         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
465                  
466                 this.error_line = validate_res.size;
467         
468                 if (this.error_line < 1) {
469                       return true;
470                 }
471                 var tlines = this.el.get_line_count ();
472                 Gtk.TextIter iter;
473                 var valiter = validate_res.map_iterator();
474                 while (valiter.next()) {
475                 
476             //        print("get inter\n");
477                     var eline = valiter.get_key();
478                     if (eline > tlines) {
479                         continue;
480                     }
481                     this.el.get_iter_at_line( out iter, eline);
482                     //print("mark line\n");
483                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
484                 }   
485                 return false;
486             }
487         public   string toString () {
488             
489             Gtk.TextIter s;
490             Gtk.TextIter e;
491             this.el.get_start_iter(out s);
492             this.el.get_end_iter(out e);
493             var ret = this.el.get_text(s,e,true);
494             //print("TO STRING? " + ret);
495             return ret;
496         }
497         public   bool checkSyntax () {
498          
499             if (this.check_running) {
500                 print("Check is running\n");
501                 if (this.check_queued) { 
502                     print("Check is already queued");
503                     return true;
504                 }
505                 this.check_queued = true;
506                 print("Adding queued Check ");
507                 GLib.Timeout.add_seconds(1, () => {
508                     this.check_queued = false;
509                     
510                     this.checkSyntax();
511                     return false;
512                 });
513             
514         
515                 return true;
516             }
517             var str = this.toString();
518             
519             // needed???
520             if (this.error_line > 0) {
521                  Gtk.TextIter start;
522                  Gtk.TextIter end;     
523                 this.el.get_bounds (out start, out end);
524         
525                 this.el.remove_source_marks (start, end, null);
526             }
527             if (str.length < 1) {
528                 print("checkSyntax - empty string?\n");
529                 return true;
530             }
531             
532             if (_this.file.xtype == "PlainFile") {
533             
534                 // assume it's gtk...
535                    this.check_running = true;
536         
537                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
538                    _this.file,
539                     str
540                  )) {
541                     this.check_running = false;
542                 }
543                 
544                 return true;
545             
546             }
547            if (_this.file == null) {
548                return true;
549            }
550             var p = Palete.factory(_this.file.xtype);   
551             
552         
553              
554             this.check_running = true;
555             
556             
557             if (_this.file.language == "js") {
558                 this.check_running = false;
559                 print("calling validate javascript\n"); 
560                 return this.highlightErrors(p.validateJavascript(
561                     str, 
562                      _this.key, 
563                     _this.ptype,
564                     _this.file,
565                     _this.node
566                 ));    
567                 
568             }
569                 
570                 
571             print("calling validate vala\n");    
572             // clear the buttons.
573          
574             
575            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
576                 _this.file,
577                 _this.node,
578                  _this.key,        
579                  _this.ptype,
580                     str
581                 )) {
582                 this.check_running = false;
583             } 
584              
585             
586             
587             //print("done mark line\n");
588              
589             return true; // at present allow saving - even if it's invalid..
590         }
591         public bool highlightErrorsJson (string type, Json.Object obj) {
592               Gtk.TextIter start;
593              Gtk.TextIter end;     
594                 this.el.get_bounds (out start, out end);
595                 
596                 this.el.remove_source_marks (start, end, type);
597                          
598              
599              // we should highlight other types of errors..
600             
601             if (!obj.has_member(type)) {
602                 print("Return has no errors\n");
603                 return true;
604             }
605             
606             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
607                 _this.window.windowstate.state != WindowState.State.CODE
608                 ) {
609                 return true;
610             } 
611             
612             
613             var err = obj.get_object_member(type);
614             
615             
616             if (_this.file == null) {
617                 return true;
618             
619             }
620             var valafn = _this.file.path;
621          
622             if (_this.file.xtype != "PlainFile") {
623         
624         
625                 
626                 
627                  valafn = "";
628                   try {             
629                        var  regex = new Regex("\\.bjs$");
630                        // should not happen
631                       
632                      
633                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
634                      } catch (GLib.RegexError e) {
635                         return true;
636                     }   
637         
638         
639         
640               }
641                if (!err.has_member(valafn)) {
642                     print("File path has no errors\n");
643                     return  true;
644                 }
645         
646                 var lines = err.get_object_member(valafn);
647                 
648                 var offset = 1;
649                 if (obj.has_member("line_offset")) {
650                     offset = (int)obj.get_int_member("line_offset") + 1;
651                 }
652             
653         
654              
655             
656             var tlines = this.el.get_line_count () +1;
657             
658             lines.foreach_member((obj, line, node) => {
659                 
660                      Gtk.TextIter iter;
661             //        print("get inter\n");
662                     var eline = int.parse(line) - offset;
663                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
664                     
665                     
666                     if (eline > tlines || eline < 0) {
667                         return;
668                     }
669                     this.el.get_iter_at_line( out iter, eline);
670                     //print("mark line\n");
671                     var msg  = "Line: %d".printf(eline+1);
672                     var ar = lines.get_array_member(line);
673                     for (var i = 0 ; i < ar.get_length(); i++) {
674                             msg += (msg.length > 0) ? "\n" : "";
675                             msg += ar.get_string_element(i);
676                     }
677                     
678                     
679                     this.el.create_source_mark(msg, type, iter);
680                 } );
681                 return false;
682             
683         
684         
685         
686         
687         }
688     }
689
690
691
692 }