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