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