Fix #7258 - close editor x, and remove text on edit - fix crash on delete prop
[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 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.close_btn.el.show();       
111         
112         } else {
113             this.view.load(        file.toSource() );
114             this.close_btn.el.hide();
115         }
116      
117     }
118     public bool saveContents ()  {
119         
120         
121         if (_this.file == null) {
122             return true;
123         }
124         
125          
126          
127          var str = _this.buffer.toString();
128          
129          _this.buffer.checkSyntax();
130          
131          
132          
133          // LeftPanel.model.changed(  str , false);
134          _this.dirty = false;
135          _this.save_button.el.sensitive = false;
136          
137         // find the text for the node..
138         if (_this.file.xtype != "PlainFile") {
139            // in theory these properties have to exist!?!
140                 this.prop.val = str;
141             this.window.windowstate.left_props.reload();
142         } else {
143             _this.file.setSource(  str );
144          }
145         
146         // call the signal..
147         this.save();
148         
149         return true;
150     
151     }
152     public void forwardSearch (bool change_focus) {
153     
154         if (this.searchcontext == null) {
155                 return;
156         } 
157         
158         Gtk.TextIter beg, st,en;
159          
160         this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
161         if (!this.searchcontext.forward(beg, out st, out en)) {
162         
163                 this.last_search_end = 0;
164         } else {
165                 this.last_search_end = en.get_offset();
166                 if (change_focus) {
167                         this.view.el.grab_focus();
168                 }
169                 this.buffer.el.place_cursor(st);
170                 this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
171         }
172      
173     }
174     public void reset () {
175          this.file = null;    
176          
177         this.node = null;
178         this.prop = null;
179         this.searchcontext = null;
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 bool check_queued;
570         public int error_line;
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.check_queued = false;
582             this.error_line = -1;
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 highlightErrors ( Gee.HashMap<int,string> validate_res) {
604                  
605                 this.error_line = validate_res.size;
606         
607                 if (this.error_line < 1) {
608                       return true;
609                 }
610                 var tlines = this.el.get_line_count ();
611                 Gtk.TextIter iter;
612                 var valiter = validate_res.map_iterator();
613                 while (valiter.next()) {
614                 
615             //        print("get inter\n");
616                     var eline = valiter.get_key();
617                     if (eline > tlines) {
618                         continue;
619                     }
620                     this.el.get_iter_at_line( out iter, eline);
621                     //print("mark line\n");
622                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
623                 }   
624                 return false;
625             }
626         public   bool checkSyntax () {
627          
628             if (this.check_running) {
629                 print("Check is running\n");
630                 if (this.check_queued) { 
631                     print("Check is already queued");
632                     return true;
633                 }
634                 this.check_queued = true;
635                 print("Adding queued Check ");
636                 GLib.Timeout.add_seconds(1, () => {
637                     this.check_queued = false;
638                     
639                     this.checkSyntax();
640                     return false;
641                 });
642             
643         
644                 return true;
645             }
646             var str = this.toString();
647             
648             // needed???
649             if (this.error_line > 0) {
650                  Gtk.TextIter start;
651                  Gtk.TextIter end;     
652                 this.el.get_bounds (out start, out end);
653         
654                 this.el.remove_source_marks (start, end, null);
655             }
656             if (str.length < 1) {
657                 print("checkSyntax - empty string?\n");
658                 return true;
659             }
660             
661             if (_this.file.xtype == "PlainFile") {
662             
663                 // assume it's gtk...
664                    this.check_running = true;
665          
666                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
667                    _this.file,
668                     str
669                  )) {
670                     this.check_running = false;
671                 }
672                 
673                 return true;
674             
675             }
676            if (_this.file == null) {
677                return true;
678            }
679             var p = _this.file.project.palete;
680             
681         
682              
683             this.check_running = true;
684             
685             
686             if (_this.file.language == "js") {
687                 this.check_running = false;
688                 print("calling validate javascript\n"); 
689                 Gee.HashMap<int,string> errors;
690                 p.javascriptHasErrors(
691                         _this.window.windowstate,
692                     str, 
693                      _this.prop,
694                     _this.file,   // no reference not node?
695                     out errors
696                 );
697                 return this.highlightErrors(errors);    
698                 
699             }
700                 
701                 
702             print("calling validate vala\n");    
703             // clear the buttons.
704          
705             
706            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
707                 _this.file,
708                 _this.node,
709                  _this.prop,        
710                     str
711                 )) {
712                 this.check_running = false;
713             } 
714              
715             
716             
717             //print("done mark line\n");
718              
719             return true; // at present allow saving - even if it's invalid..
720         }
721         public   string toString () {
722             
723             Gtk.TextIter s;
724             Gtk.TextIter e;
725             this.el.get_start_iter(out s);
726             this.el.get_end_iter(out e);
727             var ret = this.el.get_text(s,e,true);
728             //print("TO STRING? " + ret);
729             return ret;
730         }
731         public bool highlightErrorsJson (string type, Json.Object obj) {
732               Gtk.TextIter start;
733              Gtk.TextIter end;     
734                 this.el.get_bounds (out start, out end);
735                 
736                 this.el.remove_source_marks (start, end, type);
737                          
738              
739              // we should highlight other types of errors..
740             
741             if (!obj.has_member(type)) {
742                 print("Return has no errors\n");
743                 return true;
744             }
745             
746             if (_this.window.windowstate.state != WindowState.State.CODEONLY 
747               
748                 ) {
749                 return true;
750             } 
751             
752             
753             var err = obj.get_object_member(type);
754             
755             
756             if (_this.file == null) {
757                 return true;
758             
759             }
760             var valafn = _this.file.path;
761          
762             if (_this.file.xtype != "PlainFile") {
763         
764         
765                 
766                 
767                  valafn = "";
768                   try {             
769                        var  regex = new Regex("\\.bjs$");
770                        // should not happen
771                       
772                      
773                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
774                      } catch (GLib.RegexError e) {
775                         return true;
776                     }   
777         
778         
779         
780               }
781                if (!err.has_member(valafn)) {
782                     print("File path has no errors\n");
783                     return  true;
784                 }
785         
786                 var lines = err.get_object_member(valafn);
787                 
788                 var offset = 1;
789                 if (obj.has_member("line_offset")) {
790                     offset = (int)obj.get_int_member("line_offset") + 1;
791                 }
792             
793         
794              
795             
796             var tlines = this.el.get_line_count () +1;
797             
798             lines.foreach_member((obj, line, node) => {
799                 
800                      Gtk.TextIter iter;
801             //        print("get inter\n");
802                     var eline = int.parse(line) - offset;
803                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
804                     
805                     
806                     if (eline > tlines || eline < 0) {
807                         return;
808                     }
809                     this.el.get_iter_at_line( out iter, eline);
810                     //print("mark line\n");
811                     var msg  = "Line: %d".printf(eline+1);
812                     var ar = lines.get_array_member(line);
813                     for (var i = 0 ; i < ar.get_length(); i++) {
814                             msg += (msg.length > 0) ? "\n" : "";
815                             msg += ar.get_string_element(i);
816                     }
817                     
818                     
819                     this.el.create_source_mark(msg, type, iter);
820                 } );
821                 return false;
822             
823         
824         
825         
826         
827         }
828     }
829
830
831
832 }