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