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