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