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