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