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