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