63742b963a1f26fd90ba179c4da00bec9f6b35bf
[app.Builder.js] / 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     }
171     public void forwardSearch (bool change_focus) {
172     
173         if (this.searchcontext == null) {
174                 return;
175         }
176         
177         Gtk.TextIter beg, st,en;
178          
179         this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
180         if (!this.searchcontext.forward(beg, out st, out en)) {
181         
182                 this.last_search_end = 0;
183         } else {
184                 this.last_search_end = en.get_offset();
185                 if (change_focus) {
186                         this.view.el.grab_focus();
187                 }
188                 this.buffer.el.place_cursor(st);
189                 this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
190         }
191     
192     }
193     public class Xcls_Box2 : Object
194     {
195         public Gtk.Box el;
196         private Editor  _this;
197
198
199             // my vars (def)
200
201         // ctor
202         public Xcls_Box2(Editor _owner )
203         {
204             _this = _owner;
205             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
206
207             // my vars (dec)
208
209             // set gobject values
210             this.el.homogeneous = false;
211             var child_0 = new Xcls_save_button( _this );
212             child_0.ref();
213             this.el.pack_start (  child_0.el , false,false );
214             var child_1 = new Xcls_key_edit( _this );
215             child_1.ref();
216             this.el.pack_end (  child_1.el , true,true );
217         }
218
219         // user defined functions
220     }
221     public class Xcls_save_button : Object
222     {
223         public Gtk.Button el;
224         private Editor  _this;
225
226
227             // my vars (def)
228
229         // ctor
230         public Xcls_save_button(Editor _owner )
231         {
232             _this = _owner;
233             _this.save_button = this;
234             this.el = new Gtk.Button();
235
236             // my vars (dec)
237
238             // set gobject values
239             this.el.label = "Save";
240
241             //listeners
242             this.el.clicked.connect( () => { 
243                 _this.saveContents();
244             });
245         }
246
247         // user defined functions
248     }
249
250     public class Xcls_key_edit : Object
251     {
252         public Gtk.Entry el;
253         private Editor  _this;
254
255
256             // my vars (def)
257
258         // ctor
259         public Xcls_key_edit(Editor _owner )
260         {
261             _this = _owner;
262             _this.key_edit = this;
263             this.el = new Gtk.Entry();
264
265             // my vars (dec)
266
267             // set gobject values
268         }
269
270         // user defined functions
271     }
272
273
274     public class Xcls_RightEditor : Object
275     {
276         public Gtk.ScrolledWindow el;
277         private Editor  _this;
278
279
280             // my vars (def)
281
282         // ctor
283         public Xcls_RightEditor(Editor _owner )
284         {
285             _this = _owner;
286             _this.RightEditor = this;
287             this.el = new Gtk.ScrolledWindow( null, null );
288
289             // my vars (dec)
290
291             // set gobject values
292             var child_0 = new Xcls_view( _this );
293             child_0.ref();
294             this.el.add (  child_0.el  );
295
296             // init method
297
298             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
299         }
300
301         // user defined functions
302     }
303     public class Xcls_view : Object
304     {
305         public Gtk.SourceView el;
306         private Editor  _this;
307
308
309             // my vars (def)
310
311         // ctor
312         public Xcls_view(Editor _owner )
313         {
314             _this = _owner;
315             _this.view = this;
316             this.el = new Gtk.SourceView();
317
318             // my vars (dec)
319
320             // set gobject values
321             this.el.auto_indent = true;
322             this.el.indent_width = 4;
323             this.el.show_line_marks = true;
324             this.el.insert_spaces_instead_of_tabs = true;
325             this.el.show_line_numbers = true;
326             this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
327             this.el.tab_width = 4;
328             this.el.highlight_current_line = true;
329             var child_0 = new Xcls_buffer( _this );
330             child_0.ref();
331             this.el.set_buffer (  child_0.el  );
332
333             // init method
334
335             var description =   Pango.FontDescription.from_string("monospace");
336                         description.set_size(8000);
337                          this.el.override_font(description);
338                 
339                
340                 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
341                 this.el.completion.unblock_interactive();
342                 this.el.completion.select_on_show                       = true; // select
343                 this.el.completion.show_headers                 = false;
344                 this.el.completion.remember_info_visibility             = true;
345                 
346               
347                 var attrs = new Gtk.SourceMarkAttributes();
348                 var  pink =   Gdk.RGBA();
349                 pink.parse ( "pink");
350                 attrs.set_background ( pink);
351                 attrs.set_icon_name ( "process-stop");    
352                 attrs.query_tooltip_text.connect(( mark) => {
353                     //print("tooltip query? %s\n", mark.name);
354                     return mark.name;
355                 });
356                 
357                 this.el.set_mark_attributes ("ERR", attrs, 1);
358                 
359                  var wattrs = new Gtk.SourceMarkAttributes();
360                 var  blue =   Gdk.RGBA();
361                 blue.parse ( "#ABF4EB");
362                 wattrs.set_background ( blue);
363                 wattrs.set_icon_name ( "process-stop");    
364                 wattrs.query_tooltip_text.connect(( mark) => {
365                     //print("tooltip query? %s\n", mark.name);
366                     return mark.name;
367                 });
368                 
369                 this.el.set_mark_attributes ("WARN", wattrs, 1);
370                 
371              
372                 
373                  var dattrs = new Gtk.SourceMarkAttributes();
374                 var  purple =   Gdk.RGBA();
375                 purple.parse ( "#EEA9FF");
376                 dattrs.set_background ( purple);
377                 dattrs.set_icon_name ( "process-stop");    
378                 dattrs.query_tooltip_text.connect(( mark) => {
379                     //print("tooltip query? %s\n", mark.name);
380                     return mark.name;
381                 });
382                 
383                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
384
385             //listeners
386             this.el.key_release_event.connect( (event) => {
387                 
388                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
389                     print("SAVE: ctrl-S  pressed");
390                     _this.saveContents();
391                     return false;
392                 }
393                // print(event.key.keyval)
394                 
395                 return false;
396             
397             });
398         }
399
400         // user defined functions
401         public   void load (string str) {
402         
403         // show the help page for the active node..
404            //this.get('/Help').show();
405         
406         
407           // this.get('/BottomPane').el.set_current_page(0);
408             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
409             buf.set_text(str, str.length);
410             buf.set_undo_manager(null);
411             
412             var lm = Gtk.SourceLanguageManager.get_default();
413             var lang = "vala";
414             if (_this.file != null) {
415                  lang = _this.file.language;
416             }
417             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
418             var lg = _this.file.content_type.length > 0  ?
419                     lm.guess_language(_this.file.path, _this.file.content_type) :
420                     lm.get_language(lang);
421              
422            
423             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
424         
425             this.el.insert_spaces_instead_of_tabs = true;
426             if (lg != null) {
427                         print("sourcelanguage  = %s\n", lg.name);
428                         if (lg.name == "Vala") {
429                             this.el.insert_spaces_instead_of_tabs = false;
430                         }
431              }
432             _this.dirty = false;
433             this.el.grab_focus();
434             _this.save_button.el.sensitive = false;
435         }
436     }
437     public class Xcls_buffer : Object
438     {
439         public Gtk.SourceBuffer el;
440         private Editor  _this;
441
442
443             // my vars (def)
444         public bool check_queued;
445         public int error_line;
446         public bool check_running;
447
448         // ctor
449         public Xcls_buffer(Editor _owner )
450         {
451             _this = _owner;
452             _this.buffer = this;
453             this.el = new Gtk.SourceBuffer( null );
454
455             // my vars (dec)
456             this.check_queued = false;
457             this.error_line = -1;
458             this.check_running = false;
459
460             // set gobject values
461
462             //listeners
463             this.el.changed.connect( () => {
464                 // check syntax??
465                 // ??needed..??
466                 _this.save_button.el.sensitive = true;
467                 print("EDITOR CHANGED");
468                 this.checkSyntax();
469                
470                 _this.dirty = true;
471             
472                 // this.get('/LeftPanel.model').changed(  str , false);
473                 return ;
474             });
475         }
476
477         // user defined functions
478         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
479                  
480                 this.error_line = validate_res.size;
481         
482                 if (this.error_line < 1) {
483                       return true;
484                 }
485                 var tlines = this.el.get_line_count ();
486                 Gtk.TextIter iter;
487                 var valiter = validate_res.map_iterator();
488                 while (valiter.next()) {
489                 
490             //        print("get inter\n");
491                     var eline = valiter.get_key();
492                     if (eline > tlines) {
493                         continue;
494                     }
495                     this.el.get_iter_at_line( out iter, eline);
496                     //print("mark line\n");
497                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
498                 }   
499                 return false;
500             }
501         public   string toString () {
502             
503             Gtk.TextIter s;
504             Gtk.TextIter e;
505             this.el.get_start_iter(out s);
506             this.el.get_end_iter(out e);
507             var ret = this.el.get_text(s,e,true);
508             //print("TO STRING? " + ret);
509             return ret;
510         }
511         public   bool checkSyntax () {
512          
513             if (this.check_running) {
514                 print("Check is running\n");
515                 if (this.check_queued) { 
516                     print("Check is already queued");
517                     return true;
518                 }
519                 this.check_queued = true;
520                 print("Adding queued Check ");
521                 GLib.Timeout.add_seconds(1, () => {
522                     this.check_queued = false;
523                     
524                     this.checkSyntax();
525                     return false;
526                 });
527             
528         
529                 return true;
530             }
531             var str = this.toString();
532             
533             // needed???
534             if (this.error_line > 0) {
535                  Gtk.TextIter start;
536                  Gtk.TextIter end;     
537                 this.el.get_bounds (out start, out end);
538         
539                 this.el.remove_source_marks (start, end, null);
540             }
541             if (str.length < 1) {
542                 print("checkSyntax - empty string?\n");
543                 return true;
544             }
545             
546             if (_this.file.xtype == "PlainFile") {
547             
548                 // assume it's gtk...
549                    this.check_running = true;
550         
551                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
552                    _this.file,
553                     str
554                  )) {
555                     this.check_running = false;
556                 }
557                 
558                 return true;
559             
560             }
561            if (_this.file == null) {
562                return true;
563            }
564             var p = Palete.factory(_this.file.xtype);   
565             
566         
567              
568             this.check_running = true;
569             
570             
571             if (_this.file.language == "js") {
572                 this.check_running = false;
573                 print("calling validate javascript\n"); 
574                 return this.highlightErrors(p.validateJavascript(
575                     str, 
576                      _this.key, 
577                     _this.ptype,
578                     _this.file,
579                     _this.node
580                 ));    
581                 
582             }
583                 
584                 
585             print("calling validate vala\n");    
586             // clear the buttons.
587          
588             
589            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
590                 _this.file,
591                 _this.node,
592                  _this.key,        
593                  _this.ptype,
594                     str
595                 )) {
596                 this.check_running = false;
597             } 
598              
599             
600             
601             //print("done mark line\n");
602              
603             return true; // at present allow saving - even if it's invalid..
604         }
605         public bool highlightErrorsJson (string type, Json.Object obj) {
606               Gtk.TextIter start;
607              Gtk.TextIter end;     
608                 this.el.get_bounds (out start, out end);
609                 
610                 this.el.remove_source_marks (start, end, type);
611                          
612              
613              // we should highlight other types of errors..
614             
615             if (!obj.has_member(type)) {
616                 print("Return has no errors\n");
617                 return true;
618             }
619             
620             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
621                 _this.window.windowstate.state != WindowState.State.CODE
622                 ) {
623                 return true;
624             } 
625             
626             
627             var err = obj.get_object_member(type);
628             
629             
630             if (_this.file == null) {
631                 return true;
632             
633             }
634             var valafn = _this.file.path;
635          
636             if (_this.file.xtype != "PlainFile") {
637         
638         
639                 
640                 
641                  valafn = "";
642                   try {             
643                        var  regex = new Regex("\\.bjs$");
644                        // should not happen
645                       
646                      
647                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
648                      } catch (GLib.RegexError e) {
649                         return true;
650                     }   
651         
652         
653         
654               }
655                if (!err.has_member(valafn)) {
656                     print("File path has no errors\n");
657                     return  true;
658                 }
659         
660                 var lines = err.get_object_member(valafn);
661                 
662                 var offset = 1;
663                 if (obj.has_member("line_offset")) {
664                     offset = (int)obj.get_int_member("line_offset") + 1;
665                 }
666             
667         
668              
669             
670             var tlines = this.el.get_line_count () +1;
671             
672             lines.foreach_member((obj, line, node) => {
673                 
674                      Gtk.TextIter iter;
675             //        print("get inter\n");
676                     var eline = int.parse(line) - offset;
677                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
678                     
679                     
680                     if (eline > tlines || eline < 0) {
681                         return;
682                     }
683                     this.el.get_iter_at_line( out iter, eline);
684                     //print("mark line\n");
685                     var msg  = "Line: %d".printf(eline+1);
686                     var ar = lines.get_array_member(line);
687                     for (var i = 0 ; i < ar.get_length(); i++) {
688                             msg += (msg.length > 0) ? "\n" : "";
689                             msg += ar.get_string_element(i);
690                     }
691                     
692                     
693                     this.el.create_source_mark(msg, type, iter);
694                 } );
695                 return false;
696             
697         
698         
699         
700         
701         }
702     }
703
704
705
706 }