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