9a1948f695864b23a76d9fdc79db7ba40babe92b
[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             
338                          this.el.override_font(description);
339             
340                 try {        
341                         this.el.completion.add_provider(new Palete.CompletionProvider(_this));
342                 } catch (GLib.Error  e) {}
343                 
344                 this.el.completion.unblock_interactive();
345                 this.el.completion.select_on_show                       = true; // select
346                 this.el.completion.show_headers                 = false;
347                 this.el.completion.remember_info_visibility             = true;
348                 
349               
350                 var attrs = new Gtk.SourceMarkAttributes();
351                 var  pink =   Gdk.RGBA();
352                 pink.parse ( "pink");
353                 attrs.set_background ( pink);
354                 attrs.set_icon_name ( "process-stop");    
355                 attrs.query_tooltip_text.connect(( mark) => {
356                     //print("tooltip query? %s\n", mark.name);
357                     return mark.name;
358                 });
359                 
360                 this.el.set_mark_attributes ("ERR", attrs, 1);
361                 
362                  var wattrs = new Gtk.SourceMarkAttributes();
363                 var  blue =   Gdk.RGBA();
364                 blue.parse ( "#ABF4EB");
365                 wattrs.set_background ( blue);
366                 wattrs.set_icon_name ( "process-stop");    
367                 wattrs.query_tooltip_text.connect(( mark) => {
368                     //print("tooltip query? %s\n", mark.name);
369                     return mark.name;
370                 });
371                 
372                 this.el.set_mark_attributes ("WARN", wattrs, 1);
373                 
374              
375                 
376                  var dattrs = new Gtk.SourceMarkAttributes();
377                 var  purple =   Gdk.RGBA();
378                 purple.parse ( "#EEA9FF");
379                 dattrs.set_background ( purple);
380                 dattrs.set_icon_name ( "process-stop");    
381                 dattrs.query_tooltip_text.connect(( mark) => {
382                     //print("tooltip query? %s\n", mark.name);
383                     return mark.name;
384                 });
385                 
386                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
387
388             //listeners
389             this.el.key_release_event.connect( (event) => {
390                 
391                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
392                     print("SAVE: ctrl-S  pressed");
393                     _this.saveContents();
394                     return false;
395                 }
396                // print(event.key.keyval)
397                 
398                 return false;
399             
400             });
401         }
402
403         // user defined functions
404         public   void load (string str) {
405         
406         // show the help page for the active node..
407            //this.get('/Help').show();
408         
409         
410           // this.get('/BottomPane').el.set_current_page(0);
411             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
412             buf.set_text(str, str.length);
413             buf.set_undo_manager(null);
414             
415             var lm = Gtk.SourceLanguageManager.get_default();
416             var lang = "vala";
417             if (_this.file != null) {
418                  lang = _this.file.language;
419             }
420             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
421             var lg = _this.file.content_type.length > 0  ?
422                     lm.guess_language(_this.file.path, _this.file.content_type) :
423                     lm.get_language(lang);
424              
425            
426             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
427         
428             this.el.insert_spaces_instead_of_tabs = true;
429             if (lg != null) {
430                         print("sourcelanguage  = %s\n", lg.name);
431                         if (lg.name == "Vala") {
432                             this.el.insert_spaces_instead_of_tabs = false;
433                         }
434              }
435             _this.dirty = false;
436             this.el.grab_focus();
437             _this.save_button.el.sensitive = false;
438         }
439     }
440     public class Xcls_buffer : Object
441     {
442         public Gtk.SourceBuffer el;
443         private Editor  _this;
444
445
446             // my vars (def)
447         public bool check_queued;
448         public int error_line;
449         public bool check_running;
450
451         // ctor
452         public Xcls_buffer(Editor _owner )
453         {
454             _this = _owner;
455             _this.buffer = this;
456             this.el = new Gtk.SourceBuffer( null );
457
458             // my vars (dec)
459             this.check_queued = false;
460             this.error_line = -1;
461             this.check_running = false;
462
463             // set gobject values
464
465             //listeners
466             this.el.changed.connect( () => {
467                 // check syntax??
468                 // ??needed..??
469                 _this.save_button.el.sensitive = true;
470                 print("EDITOR CHANGED");
471                 this.checkSyntax();
472                
473                 _this.dirty = true;
474             
475                 // this.get('/LeftPanel.model').changed(  str , false);
476                 return ;
477             });
478         }
479
480         // user defined functions
481         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
482                  
483                 this.error_line = validate_res.size;
484         
485                 if (this.error_line < 1) {
486                       return true;
487                 }
488                 var tlines = this.el.get_line_count ();
489                 Gtk.TextIter iter;
490                 var valiter = validate_res.map_iterator();
491                 while (valiter.next()) {
492                 
493             //        print("get inter\n");
494                     var eline = valiter.get_key();
495                     if (eline > tlines) {
496                         continue;
497                     }
498                     this.el.get_iter_at_line( out iter, eline);
499                     //print("mark line\n");
500                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
501                 }   
502                 return false;
503             }
504         public   string toString () {
505             
506             Gtk.TextIter s;
507             Gtk.TextIter e;
508             this.el.get_start_iter(out s);
509             this.el.get_end_iter(out e);
510             var ret = this.el.get_text(s,e,true);
511             //print("TO STRING? " + ret);
512             return ret;
513         }
514         public   bool checkSyntax () {
515          
516             if (this.check_running) {
517                 print("Check is running\n");
518                 if (this.check_queued) { 
519                     print("Check is already queued");
520                     return true;
521                 }
522                 this.check_queued = true;
523                 print("Adding queued Check ");
524                 GLib.Timeout.add_seconds(1, () => {
525                     this.check_queued = false;
526                     
527                     this.checkSyntax();
528                     return false;
529                 });
530             
531         
532                 return true;
533             }
534             var str = this.toString();
535             
536             // needed???
537             if (this.error_line > 0) {
538                  Gtk.TextIter start;
539                  Gtk.TextIter end;     
540                 this.el.get_bounds (out start, out end);
541         
542                 this.el.remove_source_marks (start, end, null);
543             }
544             if (str.length < 1) {
545                 print("checkSyntax - empty string?\n");
546                 return true;
547             }
548             
549             if (_this.file.xtype == "PlainFile") {
550             
551                 // assume it's gtk...
552                    this.check_running = true;
553          
554                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
555                    _this.file,
556                     str
557                  )) {
558                     this.check_running = false;
559                 }
560                 
561                 return true;
562             
563             }
564            if (_this.file == null) {
565                return true;
566            }
567             var p = Palete.factory(_this.file.xtype);   
568             
569         
570              
571             this.check_running = true;
572             
573             
574             if (_this.file.language == "js") {
575                 this.check_running = false;
576                 print("calling validate javascript\n"); 
577                 Gee.HashMap<int,string> errors;
578                 p.javascriptHasErrors(
579                         _this.window.windowstate,
580                     str, 
581                      _this.key, 
582                     _this.ptype,
583                     _this.file,
584                     _this.node,
585                     out errors
586                 );
587                 return this.highlightErrors(errors);    
588                 
589             }
590                 
591                 
592             print("calling validate vala\n");    
593             // clear the buttons.
594          
595             
596            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
597                 _this.file,
598                 _this.node,
599                  _this.key,        
600                  _this.ptype,
601                     str
602                 )) {
603                 this.check_running = false;
604             } 
605              
606             
607             
608             //print("done mark line\n");
609              
610             return true; // at present allow saving - even if it's invalid..
611         }
612         public bool highlightErrorsJson (string type, Json.Object obj) {
613               Gtk.TextIter start;
614              Gtk.TextIter end;     
615                 this.el.get_bounds (out start, out end);
616                 
617                 this.el.remove_source_marks (start, end, type);
618                          
619              
620              // we should highlight other types of errors..
621             
622             if (!obj.has_member(type)) {
623                 print("Return has no errors\n");
624                 return true;
625             }
626             
627             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
628                 _this.window.windowstate.state != WindowState.State.CODE
629                 ) {
630                 return true;
631             } 
632             
633             
634             var err = obj.get_object_member(type);
635             
636             
637             if (_this.file == null) {
638                 return true;
639             
640             }
641             var valafn = _this.file.path;
642          
643             if (_this.file.xtype != "PlainFile") {
644         
645         
646                 
647                 
648                  valafn = "";
649                   try {             
650                        var  regex = new Regex("\\.bjs$");
651                        // should not happen
652                       
653                      
654                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
655                      } catch (GLib.RegexError e) {
656                         return true;
657                     }   
658         
659         
660         
661               }
662                if (!err.has_member(valafn)) {
663                     print("File path has no errors\n");
664                     return  true;
665                 }
666         
667                 var lines = err.get_object_member(valafn);
668                 
669                 var offset = 1;
670                 if (obj.has_member("line_offset")) {
671                     offset = (int)obj.get_int_member("line_offset") + 1;
672                 }
673             
674         
675              
676             
677             var tlines = this.el.get_line_count () +1;
678             
679             lines.foreach_member((obj, line, node) => {
680                 
681                      Gtk.TextIter iter;
682             //        print("get inter\n");
683                     var eline = int.parse(line) - offset;
684                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
685                     
686                     
687                     if (eline > tlines || eline < 0) {
688                         return;
689                     }
690                     this.el.get_iter_at_line( out iter, eline);
691                     //print("mark line\n");
692                     var msg  = "Line: %d".printf(eline+1);
693                     var ar = lines.get_array_member(line);
694                     for (var i = 0 ; i < ar.get_length(); i++) {
695                             msg += (msg.length > 0) ? "\n" : "";
696                             msg += ar.get_string_element(i);
697                     }
698                     
699                     
700                     this.el.create_source_mark(msg, type, iter);
701                 } );
702                 return false;
703             
704         
705         
706         
707         
708         }
709     }
710
711
712
713 }