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