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