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