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