bdd158cbd0c537a71be605ffb3a85581aac3b21a
[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_close_btn close_btn;
17         public Xcls_RightEditor RightEditor;
18         public Xcls_view view;
19         public Xcls_buffer buffer;
20         public Xcls_search_entry search_entry;
21         public Xcls_search_results search_results;
22         public Xcls_nextBtn nextBtn;
23         public Xcls_backBtn backBtn;
24         public Xcls_search_settings search_settings;
25         public Xcls_case_sensitive case_sensitive;
26         public Xcls_regex regex;
27         public Xcls_multiline multiline;
28
29                 // my vars (def)
30         public int pos_root_x;
31         public Xcls_MainWindow window;
32         public bool dirty;
33         public int pos_root_y;
34         public bool pos;
35         public int last_error_counter;
36         public GtkSource.SearchContext searchcontext;
37         public int last_search_end;
38         public signal void save ();
39         public JsRender.JsRender? file;
40         public JsRender.Node node;
41         public JsRender.NodeProp? prop;
42         public string activeEditor;
43
44         // ctor
45         public Editor()
46         {
47                 _this = this;
48                 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
49
50                 // my vars (dec)
51                 this.window = null;
52                 this.dirty = false;
53                 this.pos = false;
54                 this.last_error_counter = 0;
55                 this.searchcontext = null;
56                 this.last_search_end = 0;
57                 this.file = null;
58                 this.node = null;
59                 this.prop = null;
60                 this.activeEditor = "";
61
62                 // set gobject values
63                 this.el.homogeneous = false;
64                 this.el.hexpand = true;
65                 this.el.vexpand = true;
66                 var child_1 = new Xcls_Box2( _this );
67                 child_1.ref();
68                 this.el.append( child_1.el );
69                 new Xcls_RightEditor( _this );
70                 this.el.append( _this.RightEditor.el );
71                 var child_3 = new Xcls_Box12( _this );
72                 child_3.ref();
73                 this.el.append ( child_3.el  );
74         }
75
76         // user defined functions
77         public bool saveContents ()  {
78             
79             
80             if (_this.file == null) {
81                 return true;
82             }
83             
84              
85              
86              var str = _this.buffer.toString();
87              
88              _this.buffer.checkSyntax();
89              
90              
91              
92              // LeftPanel.model.changed(  str , false);
93              _this.dirty = false;
94              _this.save_button.el.sensitive = false;
95              
96             // find the text for the node..
97             if (_this.file.xtype != "PlainFile") {
98                // in theory these properties have to exist!?!
99                 this.prop.val = str;
100                 //this.window.windowstate.left_props.reload();
101             } else {
102                 _this.file.setSource(  str );
103              }
104             
105             // call the signal..
106             this.save();
107             
108             return true;
109         
110         }
111         public void forwardSearch (bool change_focus) {
112         
113                 if (this.searchcontext == null) {
114                         return;
115                 } 
116         
117                 Gtk.TextIter beg, st,en;
118                  bool has_wrapped_around;
119                 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
120                 if (!this.searchcontext.forward(beg, out st, out en, out has_wrapped_around)) {
121                 
122                         this.last_search_end = 0; // not sure if this should happen
123                 } else {
124                         if (has_wrapped_around) {
125                                 return;
126                         }
127                 
128                         this.last_search_end = en.get_offset();
129                         if (change_focus) {
130                                 this.view.el.grab_focus();
131                         }
132                         this.buffer.el.place_cursor(st);
133                         this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
134                 }
135          
136         }
137         public void show (JsRender.JsRender file, JsRender.Node? node, JsRender.NodeProp? prop)
138         {
139             this.reset();
140             this.file = file;    
141             
142             if (file.xtype != "PlainFile") {
143                 this.prop = prop;
144                 this.node = node;
145         
146                 // find the text for the node..
147                 this.view.load( prop.val );
148                 this.updateErrorMarks();
149                 
150                 this.close_btn.el.show();       
151             
152             } else {
153                 this.view.load(        file.toSource() );
154                  this.updateErrorMarks();
155                 this.close_btn.el.hide();
156                 
157             }
158          
159         }
160         public void backSearch (bool change_focus) {
161         
162                 if (this.searchcontext == null) {
163                         return;
164                 } 
165                 
166                 Gtk.TextIter beg, st,en;
167                 bool has_wrapped_around;
168                 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end -1 );
169                 
170                 if (!this.searchcontext.backward(beg, out st, out en, out has_wrapped_around)) {
171                         this.last_search_end = 0;
172                 } else {
173                         this.last_search_end = en.get_offset();
174                         if (change_focus) {
175                                 this.view.el.grab_focus();
176                         }
177                         this.buffer.el.place_cursor(st);
178                         this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
179                 }
180         
181         }
182         public string tempFileContents () {
183            
184            
185            if (_this.file == null) {
186                return "";
187            }
188                 var str= this.buffer.toString();
189                 if (_this.file.xtype == "PlainFile") {
190             
191                 return str;
192             
193             }
194           
195               
196              
197             GLib.debug("calling validate");    
198             // clear the buttons.
199                 if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
200                         return this.file.toSource(); ;
201                 }
202                 
203                 var oldcode  = _this.prop.val;
204                 _this.prop.val = str;
205             var ret = _this.file.toSource();
206             _this.prop.val = oldcode;
207             return ret;
208             
209         }
210         public void reset () {
211                  this.file = null;    
212              
213             this.node = null;
214             this.prop = null;
215                 this.searchcontext = null;
216           
217         }
218         public int search (string in_txt) {
219         
220                 var s = new GtkSource.SearchSettings();
221                 s.case_sensitive = _this.case_sensitive.el.active;
222                 s.regex_enabled = _this.regex.el.active;        
223                 s.wrap_around = false;
224                 
225                 this.searchcontext = new GtkSource.SearchContext(this.buffer.el,s);
226                 this.searchcontext.set_highlight(true);
227                 var txt = in_txt;
228                 
229                 if (_this.multiline.el.active) {
230                         txt = in_txt.replace("\\n", "\n");
231                 }
232                 
233                 s.set_search_text(txt);
234                 Gtk.TextIter beg, st,en;
235                  
236                 this.buffer.el.get_start_iter(out beg);
237                 bool has_wrapped_around;
238                 this.searchcontext.forward(beg, out st, out en, out has_wrapped_around);
239                 this.last_search_end = 0;
240                 
241                 return this.searchcontext.get_occurrences_count();
242         
243          
244            
245         
246         }
247         public void updateErrorMarks () {
248                 
249          
250         
251                 var buf = _this.buffer.el;
252                 Gtk.TextIter start;
253                 Gtk.TextIter end;     
254                 buf.get_bounds (out start, out end);
255         
256                 
257         
258          
259                 //GLib.debug("highlight errors");                
260         
261                  // we should highlight other types of errors..
262         
263                 if (_this.window.windowstate.state != WindowState.State.CODEONLY 
264                         &&
265                         _this.window.windowstate.state != WindowState.State.CODE
266                         ) {
267                         //GLib.debug("windowstate != CODEONLY?");
268                         
269                         return;
270                 } 
271         
272                  
273                 if (_this.file == null) {
274                         GLib.debug("file is null?");
275                         return;
276         
277                 }
278                 var ar = this.file.getErrors();
279                 if (ar.size < 1) {
280                         buf.remove_source_marks (start, end, null);
281                         buf.remove_tag_by_name ("ERR", start, end);
282                         buf.remove_tag_by_name ("WARN", start, end);
283                         buf.remove_tag_by_name ("DEPR", start, end);
284                         this.last_error_counter = file.error_counter ;
285                         //GLib.debug("highlight %s :  %s has no errors", this.file.relpath, category);
286                         return;
287                 }
288                 
289         
290          // basicaly check if there is no change, then we do not do any update..
291          // we can do this by just using an error counter?
292          // if that's changed then we will do an update, otherwise dont bother.
293                   
294                 
295                 var offset = 0;
296                 var hoffset = 0;
297         
298                 var tlines = buf.get_line_count () +1;
299                 
300                 if (_this.prop != null) {
301                         // this still seems flaky...
302         
303                         tlines = _this.prop.end_line;
304                         offset = _this.prop.start_line;
305                         hoffset = _this.node.node_pad.length + 2; //shift it left  by 2 ? ..
306                         
307                          
308                 } else {
309                         // no update...
310                         if (this.last_error_counter == file.error_counter) {
311                         
312                                 return;
313                         }
314                 
315                 }
316                 buf.remove_source_marks (start, end, null);
317                 buf.remove_tag_by_name ("ERR", start, end);
318                 buf.remove_tag_by_name ("WARN", start, end);
319                 buf.remove_tag_by_name ("DEPR", start, end);
320                 
321                 foreach(var diag in ar) { 
322                      Gtk.TextIter iter;
323         //        print("get inter\n");
324                     var eline = (int)diag.range.start.line - offset;
325                     var eline_to = (int)diag.range.end.line - offset;
326                     //var eline =  diag.range.end_line - offset;
327                     //GLib.debug("GOT ERROR on line %d -- converted to %d  (offset = %d)",
328                     //  err.line ,eline, offset);
329                     
330                     
331                     if (eline > tlines || eline < 0) {
332                         continue;
333                     }
334                    
335                     
336                     buf.get_iter_at_line( out iter, eline);
337                         var msg = "Line: %d %s : %s".printf(eline+1, diag.category, diag.message);
338                     buf.create_source_mark( msg, diag.category, iter);
339                     
340                     buf.get_iter_at_line_offset( out start, 
341                         eline, (int)diag.range.start.character - hoffset); 
342                     buf.get_iter_at_line_offset( out end, 
343                         eline_to, (int)diag.range.end.character - hoffset); 
344                         
345                     buf.apply_tag_by_name(diag.category, start, end);
346                     
347                    // GLib.debug("set line %d to %s", eline, msg);
348                     //this.marks.set(eline, msg);
349                 }
350                 this.last_error_counter = file.error_counter ;
351         
352         
353         
354          
355         
356         }
357         public void scroll_to_line (int line) {
358         
359                 GLib.Timeout.add(500, () => {
360            
361                         var buf = this.view.el.get_buffer();
362         
363                         var sbuf = (GtkSource.Buffer) buf;
364         
365         
366                         Gtk.TextIter iter;   
367                         sbuf.get_iter_at_line(out iter,  line);
368                         this.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
369                         return false;
370                 });   
371         }
372         public class Xcls_Box2 : Object
373         {
374                 public Gtk.Box el;
375                 private Editor  _this;
376
377
378                         // my vars (def)
379
380                 // ctor
381                 public Xcls_Box2(Editor _owner )
382                 {
383                         _this = _owner;
384                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
385
386                         // my vars (dec)
387
388                         // set gobject values
389                         this.el.homogeneous = false;
390                         this.el.hexpand = true;
391                         new Xcls_save_button( _this );
392                         this.el.append( _this.save_button.el );
393                         var child_2 = new Xcls_Label4( _this );
394                         child_2.ref();
395                         this.el.append( child_2.el );
396                         var child_3 = new Xcls_Scale5( _this );
397                         child_3.ref();
398                         this.el.append( child_3.el );
399                         new Xcls_close_btn( _this );
400                         this.el.append( _this.close_btn.el );
401                 }
402
403                 // user defined functions
404         }
405         public class Xcls_save_button : Object
406         {
407                 public Gtk.Button el;
408                 private Editor  _this;
409
410
411                         // my vars (def)
412
413                 // ctor
414                 public Xcls_save_button(Editor _owner )
415                 {
416                         _this = _owner;
417                         _this.save_button = this;
418                         this.el = new Gtk.Button();
419
420                         // my vars (dec)
421
422                         // set gobject values
423                         this.el.label = "Save";
424
425                         //listeners
426                         this.el.clicked.connect( () => { 
427                             _this.saveContents();
428                         });
429                 }
430
431                 // user defined functions
432         }
433
434         public class Xcls_Label4 : Object
435         {
436                 public Gtk.Label el;
437                 private Editor  _this;
438
439
440                         // my vars (def)
441
442                 // ctor
443                 public Xcls_Label4(Editor _owner )
444                 {
445                         _this = _owner;
446                         this.el = new Gtk.Label( null );
447
448                         // my vars (dec)
449
450                         // set gobject values
451                         this.el.hexpand = true;
452                 }
453
454                 // user defined functions
455         }
456
457         public class Xcls_Scale5 : Object
458         {
459                 public Gtk.Scale el;
460                 private Editor  _this;
461
462
463                         // my vars (def)
464
465                 // ctor
466                 public Xcls_Scale5(Editor _owner )
467                 {
468                         _this = _owner;
469                         this.el = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL,6, 30, 1);
470
471                         // my vars (dec)
472
473                         // set gobject values
474                         this.el.width_request = 200;
475                         this.el.has_origin = true;
476                         this.el.draw_value = false;
477                         this.el.digits = 0;
478                         this.el.sensitive = true;
479
480                         // init method
481
482                         {
483                                 this.el.set_range(6,30);
484                                 this.el.set_value(8);
485                         }
486
487                         //listeners
488                         this.el.change_value.connect( (st, val ) => {
489                                  
490                                    
491                                   _this.view.css.load_from_string(
492                                                 "#editor-view { font: %dpx monospace; }".printf((int)val)
493                                    );
494                              
495                                 return false;
496                         });
497                 }
498
499                 // user defined functions
500         }
501
502         public class Xcls_close_btn : Object
503         {
504                 public Gtk.Button el;
505                 private Editor  _this;
506
507
508                         // my vars (def)
509
510                 // ctor
511                 public Xcls_close_btn(Editor _owner )
512                 {
513                         _this = _owner;
514                         _this.close_btn = this;
515                         this.el = new Gtk.Button();
516
517                         // my vars (dec)
518
519                         // set gobject values
520                         this.el.icon_name = "window-close";
521                         var child_1 = new Xcls_Image7( _this );
522                         this.el.child = child_1.el;
523
524                         //listeners
525                         this.el.clicked.connect( () => { 
526                             _this.saveContents();
527                             _this.window.windowstate.switchState(WindowState.State.PREVIEW);
528                         });
529                 }
530
531                 // user defined functions
532         }
533         public class Xcls_Image7 : Object
534         {
535                 public Gtk.Image el;
536                 private Editor  _this;
537
538
539                         // my vars (def)
540
541                 // ctor
542                 public Xcls_Image7(Editor _owner )
543                 {
544                         _this = _owner;
545                         this.el = new Gtk.Image();
546
547                         // my vars (dec)
548
549                         // set gobject values
550                         this.el.icon_name = "window-close";
551                         this.el.icon_size = Gtk.IconSize.NORMAL;
552                 }
553
554                 // user defined functions
555         }
556
557
558
559         public class Xcls_RightEditor : Object
560         {
561                 public Gtk.ScrolledWindow el;
562                 private Editor  _this;
563
564
565                         // my vars (def)
566
567                 // ctor
568                 public Xcls_RightEditor(Editor _owner )
569                 {
570                         _this = _owner;
571                         _this.RightEditor = this;
572                         this.el = new Gtk.ScrolledWindow();
573
574                         // my vars (dec)
575
576                         // set gobject values
577                         this.el.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
578                         this.el.vexpand = true;
579                         this.el.overlay_scrolling = false;
580                         this.el.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
581                         new Xcls_view( _this );
582                         this.el.child = _this.view.el;
583                 }
584
585                 // user defined functions
586         }
587         public class Xcls_view : Object
588         {
589                 public GtkSource.View el;
590                 private Editor  _this;
591
592
593                         // my vars (def)
594                 public Gtk.CssProvider css;
595
596                 // ctor
597                 public Xcls_view(Editor _owner )
598                 {
599                         _this = _owner;
600                         _this.view = this;
601                         this.el = new GtkSource.View();
602
603                         // my vars (dec)
604                         this.css = null;
605
606                         // set gobject values
607                         this.el.auto_indent = true;
608                         this.el.indent_width = 4;
609                         this.el.name = "editor-view";
610                         this.el.show_line_marks = true;
611                         this.el.insert_spaces_instead_of_tabs = true;
612                         this.el.show_line_numbers = true;
613                         this.el.hexpand = true;
614                         this.el.vexpand = true;
615                         this.el.has_tooltip = true;
616                         this.el.tab_width = 4;
617                         this.el.highlight_current_line = true;
618                         new Xcls_buffer( _this );
619                         this.el.buffer = _this.buffer.el;
620                         var child_2 = new Xcls_EventControllerKey11( _this );
621                         child_2.ref();
622                         this.el.add_controller(  child_2.el );
623
624                         // init method
625
626                         this.css = new Gtk.CssProvider();
627                         
628                         this.css.load_from_string(
629                                 "#editor-view { font:  12px monospace;}"
630                         );
631                          
632                         Gtk.StyleContext.add_provider_for_display(
633                                 this.el.get_display(),
634                                 this.css,
635                                 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
636                         );
637                                 
638                          
639                          
640                         this.el.completion.add_provider(
641                                 new Palete.CompletionProvider(_this)
642                         );
643                           
644                         //this.el.completion.unblock_interactive();
645                         this.el.completion.select_on_show = true; // select
646                         //this.el.completion.remember_info_visibility    = true;
647                         
648                         
649                         var attrs = new GtkSource.MarkAttributes();
650                         
651                         attrs.set_icon_name ( "process-stop");    
652                         attrs.query_tooltip_text.connect(( mark) => {
653                              GLib.debug("tooltip query? %s", mark.name);
654                             return strdup( mark.name);
655                         });
656                          attrs.query_tooltip_markup.connect(( mark) => {
657                              GLib.debug("tooltip query? %s", mark.name);
658                             return strdup( mark.name);
659                         });
660                         this.el.set_mark_attributes ("ERR", attrs, 1);
661                         attrs.ref();
662                         
663                         
664                         var wattrs = new GtkSource.MarkAttributes();
665                         wattrs.set_icon_name ( "process-stop");    
666                         wattrs.query_tooltip_text.connect(( mark) => {
667                              GLib.debug("tooltip query? %s", mark.name);
668                             return strdup(mark.name);
669                         });
670                         wattrs.query_tooltip_markup.connect(( mark) => {
671                              GLib.debug("tooltip query? %s", mark.name);
672                             return strdup(mark.name);
673                         });
674                         this.el.set_mark_attributes ("WARN", wattrs, 1);
675                         wattrs.ref();
676                         
677                          
678                         var dattrs = new GtkSource.MarkAttributes();
679                          
680                         dattrs.set_icon_name ( "process-stop"); 
681                         
682                         dattrs.query_tooltip_text.connect(( mark) => {
683                                 GLib.debug("tooltip query? %s", mark.name);
684                             return strdup(mark.name);
685                         });
686                         //dattrs.query_tooltip_markup.connect(( mark) => {
687                         //      GLib.debug("tooltip query? %s", mark.name);
688                          //   return strdup(mark.name);
689                         //});
690                         this.el.set_mark_attributes ("DEPR", dattrs, 1);
691                         dattrs.ref();    
692                         
693                          this.el.get_space_drawer().set_matrix(null);
694                          this.el.get_space_drawer().set_types_for_locations( 
695                                 GtkSource.SpaceLocationFlags.ALL,
696                                 GtkSource.SpaceTypeFlags.ALL
697                         );
698                         this.el.get_space_drawer().set_enable_matrix(true);
699
700                         //listeners
701                         this.el.query_tooltip.connect( (x, y, keyboard_tooltip, tooltip) => {
702                                 
703                                 //GLib.debug("query tooltip");
704                                 Gtk.TextIter iter;
705                                 int trailing;
706                                 
707                                 var yoff = (int) _this.RightEditor.el.vadjustment.value;
708                                 
709                                 // I think this is problematic - if it's compliing  / updating at same time as query.
710                                 
711                                 //if (_this.window.statusbar_compile_spinner.el.spinning) {
712                                 //      return false;
713                                 //}
714                                 
715                                 this.el.get_iter_at_position (out iter, out trailing,  x,  y + yoff);
716                                  
717                                 var l = iter.get_line();
718                         
719                                 
720                                  
721                                 // GLib.debug("query tooltip line %d", (int) l);
722                                 if (l < 0) {
723                         
724                                         return false;
725                                 }
726                                 /*
727                                 if (_this.buffer.marks != null && _this.buffer.marks.has_key(l)) {
728                                         GLib.debug("line %d setting tip to %s", l,  _this.buffer.marks.get(l));
729                                         tooltip.set_text(_this.buffer.marks.get(l).dup());
730                                         return true;
731                                 }
732                          
733                                 return false;
734                                 */
735                                 
736                                   
737                                 // this crashes?? - not sure why.
738                                 var marks = _this.buffer.el.get_source_marks_at_line(l, "ERR");
739                                 if (marks.is_empty()) {
740                                         marks = _this.buffer.el.get_source_marks_at_line(l, "WARN");
741                                 }
742                                 if (marks.is_empty()) {
743                                         marks = _this.buffer.el.get_source_marks_at_line(l, "DEPR");
744                                 }
745                                 
746                                 // GLib.debug("query tooltip line %d marks %d", (int)l, (int) marks.length());
747                                 var str = "";
748                                 marks.@foreach((m) => { 
749                                         //GLib.debug("got mark %s", m.name);
750                                         str += (str.length > 0 ? "\n" : "") + m.category + ": " + m.name;
751                                 });
752                                 // true if there is a mark..
753                                 if (str.length > 0 ) {
754                                         tooltip.set_text( str );
755                                 }
756                                 return str.length > 0 ? true : false;
757                                  
758                         });
759                 }
760
761                 // user defined functions
762                 public void load (string str) {
763                 
764                 // show the help page for the active node..
765                    //this.get('/Help').show();
766                  
767                   // this.get('/BottomPane').el.set_current_page(0);
768                         GLib.debug("load called - Reset undo buffer");
769                         
770                     var buf = (GtkSource.Buffer)this.el.get_buffer();
771                     buf.begin_irreversible_action();
772                     buf.set_text(str, str.length);
773                     buf.end_irreversible_action();
774                     
775                     var lm = GtkSource.LanguageManager.get_default();
776                     var lang = "vala";
777                     if (_this.file != null) {
778                          lang = _this.file.language;
779                     }
780                     print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
781                     var lg = _this.file.content_type.length > 0  ?
782                             lm.guess_language(_this.file.path, _this.file.content_type) :
783                             lm.get_language(lang);
784                      
785                    
786                     ((GtkSource.Buffer)(this.el.get_buffer())) .set_language(lg); 
787                 
788                     this.el.insert_spaces_instead_of_tabs = true;
789                     if (lg != null) {
790                                 print("sourcelanguage  = %s\n", lg.name);
791                                 if (lg.name == "Vala") {
792                                     this.el.insert_spaces_instead_of_tabs = false;
793                                 }
794                      }
795                     _this.dirty = false;
796                     this.el.grab_focus();
797                     _this.save_button.el.sensitive = false;
798                     _this.last_error_counter = -1;
799                 }
800         }
801         public class Xcls_buffer : Object
802         {
803                 public GtkSource.Buffer el;
804                 private Editor  _this;
805
806
807                         // my vars (def)
808                 public int error_line;
809                 public Gee.HashMap<int,string>? xmarks;
810                 public bool check_queued;
811
812                 // ctor
813                 public Xcls_buffer(Editor _owner )
814                 {
815                         _this = _owner;
816                         _this.buffer = this;
817                         this.el = new GtkSource.Buffer( null );
818
819                         // my vars (dec)
820                         this.error_line = -1;
821                         this.xmarks = null;
822                         this.check_queued = false;
823
824                         // set gobject values
825                         this.el.highlight_syntax = true;
826                         this.el.highlight_matching_brackets = true;
827                         this.el.enable_undo = true;
828
829                         // init method
830
831                         var buf = this.el;
832                         buf.create_tag ("bold", "weight", Pango.Weight.BOLD);
833                         buf.create_tag ("type", "weight", Pango.Weight.BOLD, "foreground", "#204a87");
834                         buf.create_tag ("keyword", "weight", Pango.Weight.BOLD, "foreground", "#a40000");
835                         buf.create_tag ("text", "weight", Pango.Weight.NORMAL, "foreground", "#729fcf");
836                         buf.create_tag ("number", "weight", Pango.Weight.BOLD, "foreground", "#ad7fa8");
837                         buf.create_tag ("method", "weight", Pango.Weight.BOLD, "foreground", "#729fcf");
838                         buf.create_tag ("property", "weight", Pango.Weight.BOLD, "foreground", "#BC1F51");
839                         buf.create_tag ("variable", "weight", Pango.Weight.BOLD, "foreground", "#A518B5");
840                         
841                         
842                         buf.create_tag ("ERR", "weight", Pango.Weight.BOLD, "background", "pink");
843                         buf.create_tag ("WARN", "weight", Pango.Weight.BOLD, "background", "#ABF4EB");
844                         buf.create_tag ("DEPR", "weight", Pango.Weight.BOLD, "background", "#EEA9FF");
845
846                         //listeners
847                         this.el.changed.connect( () => {
848                             // check syntax??
849                             // ??needed..??
850                             _this.save_button.el.sensitive = true;
851                             print("EDITOR CHANGED");
852                             this.checkSyntax();
853                            
854                             _this.dirty = true;
855                         
856                             // this.get('/LeftPanel.model').changed(  str , false);
857                             return ;
858                         });
859                 }
860
861                 // user defined functions
862                 public bool OLDhighlightErrorsJson (string type, Json.Object obj) {
863                         Gtk.TextIter start;
864                         Gtk.TextIter end;     
865                         this.el.get_bounds (out start, out end);
866                 
867                         this.el.remove_source_marks (start, end, type);
868                         GLib.debug("highlight errors");          
869                 
870                          // we should highlight other types of errors..
871                 
872                         if (!obj.has_member(type)) {
873                                 GLib.debug("Return has no errors\n");
874                                 return true;
875                         }
876                 
877                         if (_this.window.windowstate.state != WindowState.State.CODEONLY 
878                                 &&
879                                 _this.window.windowstate.state != WindowState.State.CODE
880                                 ) {
881                                 GLib.debug("windowstate != CODEONLY?");
882                                 
883                                 return true;
884                         } 
885                 
886                         //this.marks = new Gee.HashMap<int,string>();
887                         var err = obj.get_object_member(type);
888                  
889                         if (_this.file == null) {
890                                 GLib.debug("file is null?");
891                                 return true;
892                 
893                         }
894                         var valafn = _this.file.path;
895                 
896                         if (_this.file.xtype != "PlainFile") {
897                 
898                                 valafn = "";
899                                 try {             
900                                         var  regex = new Regex("\\.bjs$");
901                                         // should not happen
902                                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
903                                 } catch (GLib.RegexError e) {
904                                         return true;
905                                 }   
906                 
907                 
908                 
909                         }
910                         if (!err.has_member(valafn)) {
911                                 GLib.debug("File path has no errors");
912                                 return  true;
913                         }
914                 
915                         var lines = err.get_object_member(valafn);
916                         
917                         var offset = 1;
918                         if (obj.has_member("line_offset")) { // ?? why??
919                                 offset = (int)obj.get_int_member("line_offset") + 1;
920                         }
921                 
922                 
923                         var tlines = this.el.get_line_count () +1;
924                         
925                         if (_this.prop != null) {
926                         
927                                 tlines = _this.prop.end_line + 1;
928                                 offset = _this.prop.start_line + 1;
929                         
930                         }
931                         
932                 
933                 
934                         lines.foreach_member((obj, line, node) => {
935                                 
936                              Gtk.TextIter iter;
937                 //        print("get inter\n");
938                             var eline = int.parse(line) - offset;
939                             GLib.debug("GOT ERROR on line %s -- converted to %d  (offset = %d)\n", line,eline, offset);
940                             
941                             
942                             if (eline > tlines || eline < 0) {
943                                 return;
944                             }
945                            
946                             
947                             this.el.get_iter_at_line( out iter, eline);
948                             //print("mark line\n");
949                             var msg  = "";
950                             var ar = lines.get_array_member(line);
951                             for (var i = 0 ; i < ar.get_length(); i++) {
952                                 if (ar.get_string_element(i) == "Success") {
953                                         continue;
954                                 }
955                                         msg += (msg.length > 0) ? "\n" : "";
956                                         msg += ar.get_string_element(i);
957                                 }
958                                 if (msg == "") {
959                                         return;
960                                 }
961                                 msg = "Line: %d".printf(eline+1) +  " " + msg;
962                             this.el.create_source_mark(msg, type, iter);
963                             GLib.debug("set line %d to %m", eline, msg);
964                            // this.marks.set(eline, msg);
965                         } );
966                         return false;
967                 
968                 
969                 
970                 
971                 
972                         }
973                 public bool checkSyntax () {
974                  
975                     
976                     var str = this.toString();
977                     
978                     // needed???
979                     if (this.error_line > 0) {
980                          Gtk.TextIter start;
981                          Gtk.TextIter end;     
982                         this.el.get_bounds (out start, out end);
983                 
984                         this.el.remove_source_marks (start, end, null);
985                     }
986                     if (str.length < 1) {
987                         print("checkSyntax - empty string?\n");
988                         return true;
989                     }
990                     
991                     // bit presumptiona
992                     if (_this.file.xtype == "PlainFile") {
993                     
994                         // assume it's gtk...
995                          var  oldcode =_this.file.toSource();
996                         _this.file.setSource(str);
997                             BuilderApplication.showSpinner("appointment soon","document change pending");
998                         _this.file.getLanguageServer().document_change(_this.file);
999                 
1000                         _this.file.setSource(oldcode);
1001                         
1002                                  
1003                         return true;
1004                     
1005                     }
1006                    if (_this.file == null) {
1007                        return true;
1008                    }
1009                  
1010                     
1011                 
1012                       
1013                      
1014                     GLib.debug("calling validate");    
1015                     // clear the buttons.
1016                         if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
1017                                 return true ;
1018                         }
1019                         var oldcode  = _this.prop.val;
1020                         
1021                         _this.prop.val = str;
1022                         _this.node.updated_count++;
1023                     _this.file.getLanguageServer().document_change(_this.file);
1024                     _this.node.updated_count++;
1025                     _this.prop.val = oldcode;
1026                     
1027                     
1028                     //print("done mark line\n");
1029                      
1030                     return true; // at present allow saving - even if it's invalid..
1031                 }
1032                 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
1033                          
1034                         this.error_line = validate_res.size;
1035                 
1036                         if (this.error_line < 1) {
1037                                 return true;
1038                         }
1039                         var tlines = this.el.get_line_count ();
1040                         Gtk.TextIter iter;
1041                         var valiter = validate_res.map_iterator();
1042                         while (valiter.next()) {
1043                 
1044                         //        print("get inter\n");
1045                                 var eline = valiter.get_key();
1046                                 if (eline > tlines) {
1047                                         continue;
1048                                 }
1049                                 this.el.get_iter_at_line( out iter, eline);
1050                                 //print("mark line\n");
1051                                 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
1052                         }   
1053                         return false;
1054                 }
1055                 public string toString () {
1056                     
1057                     Gtk.TextIter s;
1058                     Gtk.TextIter e;
1059                     this.el.get_start_iter(out s);
1060                     this.el.get_end_iter(out e);
1061                     var ret = this.el.get_text(s,e,true);
1062                     //print("TO STRING? " + ret);
1063                     return ret;
1064                 }
1065         }
1066
1067         public class Xcls_EventControllerKey11 : Object
1068         {
1069                 public Gtk.EventControllerKey el;
1070                 private Editor  _this;
1071
1072
1073                         // my vars (def)
1074
1075                 // ctor
1076                 public Xcls_EventControllerKey11(Editor _owner )
1077                 {
1078                         _this = _owner;
1079                         this.el = new Gtk.EventControllerKey();
1080
1081                         // my vars (dec)
1082
1083                         // set gobject values
1084
1085                         //listeners
1086                         this.el.key_released.connect( (keyval, keycode, state) => {
1087                         
1088                           
1089                             if (keyval == Gdk.Key.s && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1090                                 GLib.debug("SAVE: ctrl-S  pressed");
1091                                 _this.saveContents();
1092                                 return;
1093                             }
1094                             
1095                             if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1096                                     GLib.debug("SAVE: ctrl-g  pressed");
1097                                         _this.forwardSearch(true);
1098                                     return;
1099                                 }
1100                                 if (keyval == Gdk.Key.f && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1101                                     GLib.debug("SAVE: ctrl-f  pressed");
1102                                         _this.search_entry.el.grab_focus();
1103                                         _this.search_entry.el.select_region(0,-1);
1104                                     return;
1105                                 }
1106                                 if (keyval == Gdk.Key.space && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1107                                         _this.view.el.show_completion();
1108                                 }
1109                                 
1110                                 Gtk.TextIter iter;
1111                                 _this.buffer.el.get_iter_at_offset( out iter, _this.buffer.el.cursor_position);  
1112                                 var line  = iter.get_line();
1113                                 var offset = iter.get_line_offset();
1114                                 GLib.debug("line  %d  off %d", line ,offset);
1115                                 if (_this.prop != null) {
1116                                         line += _this.prop.start_line + 1; // i think..
1117                                         offset += 12; // should probably be 8 without namespaced 
1118                                         GLib.debug("guess line  %d  off %d", line ,offset);
1119                                 } 
1120                             //_this.view.el.show_completion();
1121                            // print(event.key.keyval)
1122                            
1123                            
1124                            
1125                             
1126                             return;
1127                          
1128                          
1129                         });
1130                 }
1131
1132                 // user defined functions
1133         }
1134
1135
1136
1137         public class Xcls_Box12 : Object
1138         {
1139                 public Gtk.Box el;
1140                 private Editor  _this;
1141
1142
1143                         // my vars (def)
1144
1145                 // ctor
1146                 public Xcls_Box12(Editor _owner )
1147                 {
1148                         _this = _owner;
1149                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
1150
1151                         // my vars (dec)
1152
1153                         // set gobject values
1154                         this.el.homogeneous = false;
1155                         this.el.vexpand = false;
1156                         new Xcls_search_entry( _this );
1157                         this.el.append( _this.search_entry.el );
1158                         new Xcls_search_results( _this );
1159                         this.el.append( _this.search_results.el );
1160                         new Xcls_nextBtn( _this );
1161                         this.el.append( _this.nextBtn.el );
1162                         new Xcls_backBtn( _this );
1163                         this.el.append( _this.backBtn.el );
1164                         var child_5 = new Xcls_MenuButton18( _this );
1165                         child_5.ref();
1166                         this.el.append( child_5.el );
1167                 }
1168
1169                 // user defined functions
1170         }
1171         public class Xcls_search_entry : Object
1172         {
1173                 public Gtk.SearchEntry el;
1174                 private Editor  _this;
1175
1176
1177                         // my vars (def)
1178                 public Gtk.CssProvider css;
1179
1180                 // ctor
1181                 public Xcls_search_entry(Editor _owner )
1182                 {
1183                         _this = _owner;
1184                         _this.search_entry = this;
1185                         this.el = new Gtk.SearchEntry();
1186
1187                         // my vars (dec)
1188
1189                         // set gobject values
1190                         this.el.name = "editor-search-entry";
1191                         this.el.hexpand = true;
1192                         this.el.placeholder_text = "Press enter to search";
1193                         this.el.search_delay = 3;
1194                         var child_1 = new Xcls_EventControllerKey14( _this );
1195                         child_1.ref();
1196                         this.el.add_controller(  child_1.el );
1197
1198                         //listeners
1199                         this.el.search_changed.connect( ( ) => {
1200                         
1201                         _this.search(_this.search_entry.el.text);
1202                                  _this.search_results.updateResults();
1203                         
1204                                 GLib.Timeout.add_seconds(1,() => {
1205                                          _this.search_results.updateResults();
1206                                          return false;
1207                                  });
1208                         });
1209                 }
1210
1211                 // user defined functions
1212                 public void forwardSearch (bool change_focus) {
1213                 
1214                 
1215                         _this.forwardSearch(change_focus);
1216                 
1217                 /*
1218                 
1219                         switch(_this.windowstate.state) {
1220                                 case WindowState.State.CODEONLY:
1221                                 //case WindowState.State.CODE:
1222                                         // search the code being edited..
1223                                         _this.windowstate.code_editor_tab.forwardSearch(change_focus);
1224                                          
1225                                         break;
1226                                 case WindowState.State.PREVIEW:
1227                                         if (_this.windowstate.file.xtype == "Gtk") {
1228                                                 _this.windowstate.window_gladeview.forwardSearch(change_focus);
1229                                         } else { 
1230                                                  _this.windowstate.window_rooview.forwardSearch(change_focus);
1231                                         }
1232                                 
1233                                         break;
1234                         }
1235                         */
1236                         
1237                 }
1238         }
1239         public class Xcls_EventControllerKey14 : Object
1240         {
1241                 public Gtk.EventControllerKey el;
1242                 private Editor  _this;
1243
1244
1245                         // my vars (def)
1246
1247                 // ctor
1248                 public Xcls_EventControllerKey14(Editor _owner )
1249                 {
1250                         _this = _owner;
1251                         this.el = new Gtk.EventControllerKey();
1252
1253                         // my vars (dec)
1254
1255                         // set gobject values
1256
1257                         //listeners
1258                         this.el.key_pressed.connect( (keyval, keycode, state) => {
1259                         
1260                                 if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1261                                     GLib.debug("SAVE: ctrl-g  pressed");
1262                                         _this.forwardSearch(true);
1263                                     return true;
1264                                 }
1265                             
1266                           
1267                                 if (keyval == Gdk.Key.Return && _this.search_entry.el.text.length > 0) {
1268                                         _this.forwardSearch(true);
1269                                         
1270                                         
1271                                     return true;
1272                         
1273                                 }    
1274                            // print(event.key.keyval)
1275                            
1276                             return false;
1277                         });
1278                 }
1279
1280                 // user defined functions
1281         }
1282
1283
1284         public class Xcls_search_results : Object
1285         {
1286                 public Gtk.Label el;
1287                 private Editor  _this;
1288
1289
1290                         // my vars (def)
1291
1292                 // ctor
1293                 public Xcls_search_results(Editor _owner )
1294                 {
1295                         _this = _owner;
1296                         _this.search_results = this;
1297                         this.el = new Gtk.Label( "No Results" );
1298
1299                         // my vars (dec)
1300
1301                         // set gobject values
1302                         this.el.margin_end = 4;
1303                         this.el.margin_start = 4;
1304                 }
1305
1306                 // user defined functions
1307                 public void updateResults () {
1308                         this.el.visible = true;
1309                         
1310                         var res = _this.searchcontext.get_occurrences_count();
1311                         if (res < 0) {
1312                                 _this.search_results.el.label = "??? Matches";          
1313                                 return;
1314                         }
1315                 
1316                         _this.nextBtn.el.sensitive = false;
1317                         _this.backBtn.el.sensitive = false;     
1318                 
1319                         if (res > 0) {
1320                                 _this.search_results.el.label = "%d Matches".printf(res);
1321                                 _this.nextBtn.el.sensitive = true;
1322                                 _this.backBtn.el.sensitive = true;
1323                                 return;
1324                         } 
1325                         _this.search_results.el.label = "No Matches";
1326                         
1327                 }
1328         }
1329
1330         public class Xcls_nextBtn : Object
1331         {
1332                 public Gtk.Button el;
1333                 private Editor  _this;
1334
1335
1336                         // my vars (def)
1337                 public bool always_show_image;
1338
1339                 // ctor
1340                 public Xcls_nextBtn(Editor _owner )
1341                 {
1342                         _this = _owner;
1343                         _this.nextBtn = this;
1344                         this.el = new Gtk.Button();
1345
1346                         // my vars (dec)
1347                         this.always_show_image = true;
1348
1349                         // set gobject values
1350                         this.el.icon_name = "go-down";
1351                         this.el.sensitive = false;
1352
1353                         //listeners
1354                         this.el.clicked.connect( (event) => {
1355                         
1356                                 _this.forwardSearch(true);
1357                                 
1358                                  
1359                         });
1360                 }
1361
1362                 // user defined functions
1363         }
1364
1365         public class Xcls_backBtn : Object
1366         {
1367                 public Gtk.Button el;
1368                 private Editor  _this;
1369
1370
1371                         // my vars (def)
1372                 public bool always_show_image;
1373
1374                 // ctor
1375                 public Xcls_backBtn(Editor _owner )
1376                 {
1377                         _this = _owner;
1378                         _this.backBtn = this;
1379                         this.el = new Gtk.Button();
1380
1381                         // my vars (dec)
1382                         this.always_show_image = true;
1383
1384                         // set gobject values
1385                         this.el.icon_name = "go-up";
1386                         this.el.sensitive = false;
1387
1388                         //listeners
1389                         this.el.clicked.connect( (event) => {
1390                         
1391                                 _this.backSearch(true);
1392                                  
1393                         });
1394                 }
1395
1396                 // user defined functions
1397         }
1398
1399         public class Xcls_MenuButton18 : Object
1400         {
1401                 public Gtk.MenuButton el;
1402                 private Editor  _this;
1403
1404
1405                         // my vars (def)
1406                 public bool always_show_image;
1407
1408                 // ctor
1409                 public Xcls_MenuButton18(Editor _owner )
1410                 {
1411                         _this = _owner;
1412                         this.el = new Gtk.MenuButton();
1413
1414                         // my vars (dec)
1415                         this.always_show_image = true;
1416
1417                         // set gobject values
1418                         this.el.icon_name = "emblem-system";
1419                         this.el.always_show_arrow = true;
1420                         new Xcls_search_settings( _this );
1421                         this.el.popover = _this.search_settings.el;
1422                 }
1423
1424                 // user defined functions
1425         }
1426         public class Xcls_search_settings : Object
1427         {
1428                 public Gtk.Popover el;
1429                 private Editor  _this;
1430
1431
1432                         // my vars (def)
1433
1434                 // ctor
1435                 public Xcls_search_settings(Editor _owner )
1436                 {
1437                         _this = _owner;
1438                         _this.search_settings = this;
1439                         this.el = new Gtk.Popover();
1440
1441                         // my vars (dec)
1442
1443                         // set gobject values
1444                         var child_1 = new Xcls_Box20( _this );
1445                         this.el.child = child_1.el;
1446                 }
1447
1448                 // user defined functions
1449         }
1450         public class Xcls_Box20 : Object
1451         {
1452                 public Gtk.Box el;
1453                 private Editor  _this;
1454
1455
1456                         // my vars (def)
1457
1458                 // ctor
1459                 public Xcls_Box20(Editor _owner )
1460                 {
1461                         _this = _owner;
1462                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
1463
1464                         // my vars (dec)
1465
1466                         // set gobject values
1467                         new Xcls_case_sensitive( _this );
1468                         this.el.append( _this.case_sensitive.el );
1469                         new Xcls_regex( _this );
1470                         this.el.append( _this.regex.el );
1471                         new Xcls_multiline( _this );
1472                         this.el.append( _this.multiline.el );
1473                 }
1474
1475                 // user defined functions
1476         }
1477         public class Xcls_case_sensitive : Object
1478         {
1479                 public Gtk.CheckButton el;
1480                 private Editor  _this;
1481
1482
1483                         // my vars (def)
1484
1485                 // ctor
1486                 public Xcls_case_sensitive(Editor _owner )
1487                 {
1488                         _this = _owner;
1489                         _this.case_sensitive = this;
1490                         this.el = new Gtk.CheckButton();
1491
1492                         // my vars (dec)
1493
1494                         // set gobject values
1495                         this.el.label = "Case Sensitive";
1496
1497                         // init method
1498
1499                         {
1500                                 this.el.show();
1501                         }
1502                 }
1503
1504                 // user defined functions
1505         }
1506
1507         public class Xcls_regex : Object
1508         {
1509                 public Gtk.CheckButton el;
1510                 private Editor  _this;
1511
1512
1513                         // my vars (def)
1514
1515                 // ctor
1516                 public Xcls_regex(Editor _owner )
1517                 {
1518                         _this = _owner;
1519                         _this.regex = this;
1520                         this.el = new Gtk.CheckButton();
1521
1522                         // my vars (dec)
1523
1524                         // set gobject values
1525                         this.el.label = "Regex";
1526
1527                         // init method
1528
1529                         {
1530                                 this.el.show();
1531                         }
1532                 }
1533
1534                 // user defined functions
1535         }
1536
1537         public class Xcls_multiline : Object
1538         {
1539                 public Gtk.CheckButton el;
1540                 private Editor  _this;
1541
1542
1543                         // my vars (def)
1544
1545                 // ctor
1546                 public Xcls_multiline(Editor _owner )
1547                 {
1548                         _this = _owner;
1549                         _this.multiline = this;
1550                         this.el = new Gtk.CheckButton();
1551
1552                         // my vars (dec)
1553
1554                         // set gobject values
1555                         this.el.label = "Multi-line (add \\n)";
1556                 }
1557
1558                 // user defined functions
1559         }
1560
1561
1562
1563
1564
1565 }