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