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