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