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