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