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