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