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