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