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                  return false;
416                  
417                 this.error_line = validate_res.size;
418         
419                 if (this.error_line < 1) {
420                       return true;
421                 }
422                 var tlines = this.el.get_line_count ();
423                 Gtk.TextIter iter;
424                 var valiter = validate_res.map_iterator();
425                 while (valiter.next()) {
426                 
427             //        print("get inter\n");
428                     var eline = valiter.get_key();
429                     if (eline > tlines) {
430                         continue;
431                     }
432                     this.el.get_iter_at_line( out iter, eline);
433                     //print("mark line\n");
434                     this.el.create_source_mark(valiter.get_value(), "error", iter);
435                 }   
436                 return false;
437             }
438         public   string toString () {
439             
440             Gtk.TextIter s;
441             Gtk.TextIter e;
442             this.el.get_start_iter(out s);
443             this.el.get_end_iter(out e);
444             var ret = this.el.get_text(s,e,true);
445             //print("TO STRING? " + ret);
446             return ret;
447         }
448         public   bool checkSyntax () {
449          
450             if (this.check_running) {
451                 print("Check is running\n");
452                 if (this.check_queued) { 
453                     print("Check is already queued");
454                     return true;
455                 }
456                 this.check_queued = true;
457                 print("Adding queued Check ");
458                 GLib.Timeout.add_seconds(1, () => {
459                     this.check_queued = false;
460                     
461                     this.checkSyntax();
462                     return false;
463                 });
464             
465         
466                 return true;
467             }
468             var str = this.toString();
469             
470             // needed???
471             if (this.error_line > 0) {
472                  Gtk.TextIter start;
473                  Gtk.TextIter end;     
474                 this.el.get_bounds (out start, out end);
475         
476                 this.el.remove_source_marks (start, end, null);
477             }
478             if (str.length < 1) {
479                 print("checkSyntax - empty string?\n");
480                 return true;
481             }
482             
483             if (_this.file == null && _this.fname.length > 0) {
484             
485                 // assume it's gtk...
486                    this.check_running = true;
487         
488                  _this.window.windowstate.valasource.checkPlainFileSpawn(
489                     _this.window.project,
490                     _this.fname,
491                     str
492                  );
493                 return true;
494             
495             }
496            if (_this.file == null) {
497                return true;
498            }
499             var p = Palete.factory(_this.file.xtype);   
500             
501         
502              
503             this.check_running = true;
504             
505             
506             if (_this.file.language == "js") {
507                 this.check_running = false;
508                 print("calling validate javascript\n"); 
509                 return this.highlightErrors(p.validateJavascript(
510                     str, 
511                      _this.key, 
512                     _this.ptype,
513                     _this.file,
514                     _this.node
515                 ));    
516                 
517             }
518                 
519                 
520             print("calling validate vala\n");    
521             // clear the buttons.
522          
523             
524             p.validateVala(
525                 _this.window.windowstate,
526                 str, 
527                  _this.key, 
528                 _this.ptype,
529                 _this.file,
530                 _this.node 
531                 
532                 
533             );
534              
535             
536             
537             //print("done mark line\n");
538              
539             return true; // at present allow saving - even if it's invalid..
540         }
541         public bool highlightErrorsJson (string type, Json.Object obj) {
542               Gtk.TextIter start;
543              Gtk.TextIter end;     
544                 this.el.get_bounds (out start, out end);
545                 
546                 this.el.remove_source_marks (start, end, type);
547                          
548              
549              // we should highlight other types of errors..
550             
551             if (!obj.has_member(type)) {
552                 print("Return has no errors\n");
553                 return true;
554             }
555             
556             if (_this.window.windowstate.state != WindowState.State.CODEONLY && 
557                 _this.window.windowstate.state != WindowState.State.CODE
558                 ) {
559                 return true;
560             } 
561             
562             
563             var err = obj.get_object_member(type);
564             var valafn = _this.fname;
565             if (_this.file != null) {
566         
567         
568                 
569                 
570                  valafn = "";
571                   try {             
572                        var  regex = new Regex("\\.bjs$");
573                     
574                      
575                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
576                      } catch (GLib.RegexError e) {
577                         return true;
578                     }   
579         
580         
581         
582               }
583                if (!err.has_member(valafn)) {
584                     print("File path has no errors\n");
585                     return  true;
586                 }
587         
588                 var lines = err.get_object_member(valafn);
589                 
590                 var offset = 1;
591                 if (obj.has_member("line_offset")) {
592                     offset = (int)obj.get_int_member("line_offset") + 1;
593                 }
594             
595         
596              
597             
598             var tlines = this.el.get_line_count () +1;
599             
600             lines.foreach_member((obj, line, node) => {
601                 
602                      Gtk.TextIter iter;
603             //        print("get inter\n");
604                     var eline = int.parse(line) - offset;
605                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
606                     
607                     
608                     if (eline > tlines || eline < 0) {
609                         return;
610                     }
611                     this.el.get_iter_at_line( out iter, eline);
612                     //print("mark line\n");
613                     var msg  = "Line: %d".printf(eline+1);
614                     var ar = lines.get_array_member(line);
615                     for (var i = 0 ; i < ar.get_length(); i++) {
616                             msg += (msg.length > 0) ? "\n" : "";
617                             msg += ar.get_string_element(i);
618                     }
619                     
620                     
621                     this.el.create_source_mark(msg, type, iter);
622                 } );
623                 return false;
624             
625         
626         
627         
628         
629         }
630     }
631
632
633
634 }