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