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