src/Builder4/config1.builder
[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.hexpand = 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         if (_this.file == null) {
66             return true;
67         }
68         
69         
70         
71         
72        
73          
74          var str = _this.buffer.toString();
75          
76          _this.buffer.checkSyntax();
77          
78          
79          
80          // LeftPanel.model.changed(  str , false);
81          _this.dirty = false;
82          _this.save_button.el.sensitive = false;
83          
84         // find the text for the node..
85         if (_this.file.xtype != "PlainFile") {
86             if (ptype == "listener") {
87                 this.node.listeners.set(key,str);
88             
89             } else {
90                  this.node.props.set(key,str);
91             }
92         } else {
93             _this.file.setSource(  str );
94          }
95         
96         // call the signal..
97         this.save();
98         
99         return true;
100     
101     }
102     public   void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
103     {
104         this.file = file;    
105         this.ptype = "";
106         this.key  = "";
107         this.node = null;
108     
109         
110         if (file.xtype != "PlainFile") {
111         
112             this.ptype = ptype;
113             this.key  = key;
114             this.node = node;
115              string val = "";
116             // find the text for the node..
117             if (ptype == "listener") {
118                 val = node.listeners.get(key);
119             
120             } else {
121                 val = node.props.get(key);
122             }
123             this.view.load(val);
124             this.key_edit.el.show();
125             this.key_edit.el.text = key;  
126         
127         } else {
128             this.view.load(        file.toSource() );
129             this.key_edit.el.hide();
130         }
131     
132            
133     }
134     public class Xcls_Box2 : Object
135     {
136         public Gtk.Box el;
137         private Editor  _this;
138
139
140             // my vars (def)
141
142         // ctor
143         public Xcls_Box2(Editor _owner )
144         {
145             _this = _owner;
146             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
147
148             // my vars (dec)
149
150             // set gobject values
151             this.el.homogeneous = false;
152             var child_0 = new Xcls_save_button( _this );
153             child_0.ref();
154             this.el.pack_start (  child_0.el , false,false );
155             var child_1 = new Xcls_key_edit( _this );
156             child_1.ref();
157             this.el.pack_end (  child_1.el , true,true );
158         }
159
160         // user defined functions
161     }
162     public class Xcls_save_button : Object
163     {
164         public Gtk.Button el;
165         private Editor  _this;
166
167
168             // my vars (def)
169
170         // ctor
171         public Xcls_save_button(Editor _owner )
172         {
173             _this = _owner;
174             _this.save_button = this;
175             this.el = new Gtk.Button();
176
177             // my vars (dec)
178
179             // set gobject values
180             this.el.label = "Save";
181
182             //listeners
183             this.el.clicked.connect( () => { 
184                 _this.saveContents();
185             });
186         }
187
188         // user defined functions
189     }
190
191     public class Xcls_key_edit : Object
192     {
193         public Gtk.Entry el;
194         private Editor  _this;
195
196
197             // my vars (def)
198
199         // ctor
200         public Xcls_key_edit(Editor _owner )
201         {
202             _this = _owner;
203             _this.key_edit = this;
204             this.el = new Gtk.Entry();
205
206             // my vars (dec)
207
208             // set gobject values
209         }
210
211         // user defined functions
212     }
213
214
215     public class Xcls_RightEditor : Object
216     {
217         public Gtk.ScrolledWindow el;
218         private Editor  _this;
219
220
221             // my vars (def)
222
223         // ctor
224         public Xcls_RightEditor(Editor _owner )
225         {
226             _this = _owner;
227             _this.RightEditor = this;
228             this.el = new Gtk.ScrolledWindow( null, null );
229
230             // my vars (dec)
231
232             // set gobject values
233             var child_0 = new Xcls_view( _this );
234             child_0.ref();
235             this.el.add (  child_0.el  );
236
237             // init method
238
239             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
240         }
241
242         // user defined functions
243     }
244     public class Xcls_view : Object
245     {
246         public Gtk.SourceView el;
247         private Editor  _this;
248
249
250             // my vars (def)
251
252         // ctor
253         public Xcls_view(Editor _owner )
254         {
255             _this = _owner;
256             _this.view = this;
257             this.el = new Gtk.SourceView();
258
259             // my vars (dec)
260
261             // set gobject values
262             this.el.auto_indent = true;
263             this.el.indent_width = 4;
264             this.el.show_line_marks = true;
265             this.el.insert_spaces_instead_of_tabs = true;
266             this.el.show_line_numbers = true;
267             this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
268             this.el.tab_width = 4;
269             this.el.highlight_current_line = true;
270             var child_0 = new Xcls_buffer( _this );
271             child_0.ref();
272             this.el.set_buffer (  child_0.el  );
273
274             // init method
275
276             var description =   Pango.FontDescription.from_string("monospace");
277                 description.set_size(8000);
278                 this.el.override_font(description);
279                 this.el.completion.add_provider(new Palete.CompletionProvider());
280              
281               
282                 var attrs = new Gtk.SourceMarkAttributes();
283                 var  pink =   Gdk.RGBA();
284                 pink.parse ( "pink");
285                 attrs.set_background ( pink);
286                 attrs.set_icon_name ( "process-stop");    
287                 attrs.query_tooltip_text.connect(( mark) => {
288                     //print("tooltip query? %s\n", mark.name);
289                     return mark.name;
290                 });
291                 
292                 this.el.set_mark_attributes ("ERR", attrs, 1);
293                 
294                  var wattrs = new Gtk.SourceMarkAttributes();
295                 var  blue =   Gdk.RGBA();
296                 blue.parse ( "#ABF4EB");
297                 wattrs.set_background ( blue);
298                 wattrs.set_icon_name ( "process-stop");    
299                 wattrs.query_tooltip_text.connect(( mark) => {
300                     //print("tooltip query? %s\n", mark.name);
301                     return mark.name;
302                 });
303                 
304                 this.el.set_mark_attributes ("WARN", wattrs, 1);
305                 
306              
307                 
308                  var dattrs = new Gtk.SourceMarkAttributes();
309                 var  purple =   Gdk.RGBA();
310                 purple.parse ( "#EEA9FF");
311                 dattrs.set_background ( purple);
312                 dattrs.set_icon_name ( "process-stop");    
313                 dattrs.query_tooltip_text.connect(( mark) => {
314                     //print("tooltip query? %s\n", mark.name);
315                     return mark.name;
316                 });
317                 
318                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
319
320             //listeners
321             this.el.key_release_event.connect( (event) => {
322                 
323                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
324                     print("SAVE: ctrl-S  pressed");
325                     _this.saveContents();
326                     return false;
327                 }
328                // print(event.key.keyval)
329                 
330                 return false;
331             
332             });
333         }
334
335         // user defined functions
336         public   void load (string str) {
337         
338         // show the help page for the active node..
339            //this.get('/Help').show();
340         
341         
342           // this.get('/BottomPane').el.set_current_page(0);
343             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
344             buf.set_text(str, str.length);
345             buf.set_undo_manager(null);
346             
347             var lm = Gtk.SourceLanguageManager.get_default();
348             var lang = "vala";
349             if (_this.file != null) {
350                  lang = _this.file.language;
351             }
352             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
353             var lg = _this.file.content_type.length > 0  ?
354                     lm.guess_language(_this.file.path, _this.file.content_type) :
355                     lm.get_language(lang);
356             
357            
358            
359             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
360         
361             this.el.insert_spaces_instead_of_tabs = true;
362             
363             print("sourcelanguage  = %s\n", lg.name);
364             if (lg.name == "Vala") {
365                 this.el.insert_spaces_instead_of_tabs = false;
366             }
367              
368             _this.dirty = false;
369             this.el.grab_focus();
370             _this.save_button.el.sensitive = false;
371         }
372     }
373     public class Xcls_buffer : Object
374     {
375         public Gtk.SourceBuffer el;
376         private Editor  _this;
377
378
379             // my vars (def)
380         public bool check_queued;
381         public int error_line;
382         public bool check_running;
383
384         // ctor
385         public Xcls_buffer(Editor _owner )
386         {
387             _this = _owner;
388             _this.buffer = this;
389             this.el = new Gtk.SourceBuffer( null );
390
391             // my vars (dec)
392             this.check_queued = false;
393             this.error_line = -1;
394             this.check_running = false;
395
396             // set gobject values
397
398             //listeners
399             this.el.changed.connect( () => {
400                 // check syntax??
401                 // ??needed..??
402                 _this.save_button.el.sensitive = true;
403                 print("EDITOR CHANGED");
404                 this.checkSyntax();
405                
406                 _this.dirty = true;
407             
408                 // this.get('/LeftPanel.model').changed(  str , false);
409                 return ;
410             });
411         }
412
413         // user defined functions
414         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
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(), "ERR", 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.xtype == "PlainFile") {
483             
484                 // assume it's gtk...
485                    this.check_running = true;
486         
487                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
488                    _this.file,
489                     str
490                  )) {
491                     this.check_running = false;
492                 }
493                 
494                 return true;
495             
496             }
497            if (_this.file == null) {
498                return true;
499            }
500             var p = Palete.factory(_this.file.xtype);   
501             
502         
503              
504             this.check_running = true;
505             
506             
507             if (_this.file.language == "js") {
508                 this.check_running = false;
509                 print("calling validate javascript\n"); 
510                 return this.highlightErrors(p.validateJavascript(
511                     str, 
512                      _this.key, 
513                     _this.ptype,
514                     _this.file,
515                     _this.node
516                 ));    
517                 
518             }
519                 
520                 
521             print("calling validate vala\n");    
522             // clear the buttons.
523          
524             
525            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
526                 _this.file,
527                 _this.node,
528                  _this.key,        
529                  _this.ptype,
530                     str
531                 )) {
532                 this.check_running = false;
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             
565             
566             if (_this.file == null) {
567                 return true;
568             
569             }
570             var valafn = _this.file.path;
571          
572             if (_this.file.xtype != "PlainFile") {
573         
574         
575                 
576                 
577                  valafn = "";
578                   try {             
579                        var  regex = new Regex("\\.bjs$");
580                        // should not happen
581                       
582                      
583                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
584                      } catch (GLib.RegexError e) {
585                         return true;
586                     }   
587         
588         
589         
590               }
591                if (!err.has_member(valafn)) {
592                     print("File path has no errors\n");
593                     return  true;
594                 }
595         
596                 var lines = err.get_object_member(valafn);
597                 
598                 var offset = 1;
599                 if (obj.has_member("line_offset")) {
600                     offset = (int)obj.get_int_member("line_offset") + 1;
601                 }
602             
603         
604              
605             
606             var tlines = this.el.get_line_count () +1;
607             
608             lines.foreach_member((obj, line, node) => {
609                 
610                      Gtk.TextIter iter;
611             //        print("get inter\n");
612                     var eline = int.parse(line) - offset;
613                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
614                     
615                     
616                     if (eline > tlines || eline < 0) {
617                         return;
618                     }
619                     this.el.get_iter_at_line( out iter, eline);
620                     //print("mark line\n");
621                     var msg  = "Line: %d".printf(eline+1);
622                     var ar = lines.get_array_member(line);
623                     for (var i = 0 ; i < ar.get_length(); i++) {
624                             msg += (msg.length > 0) ? "\n" : "";
625                             msg += ar.get_string_element(i);
626                     }
627                     
628                     
629                     this.el.create_source_mark(msg, type, iter);
630                 } );
631                 return false;
632             
633         
634         
635         
636         
637         }
638     }
639
640
641
642 }