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