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