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