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