src/Builder4/WindowRooView.bjs
[app.Builder.js] / src / Builder4 / WindowRooView.vala
index 53e963d..4582600 100644 (file)
@@ -22,6 +22,7 @@ public class Xcls_WindowRooView : Object
     public Xcls_view view;
     public Xcls_inspectorcontainer inspectorcontainer;
     public Xcls_sourceview sourceview;
+    public Xcls_buffer buffer;
 
         // my vars (def)
     public Gtk.Widget lastObj;
@@ -932,7 +933,8 @@ public class Xcls_WindowRooView : Object
             // my vars (def)
         public bool loading;
         public bool allow_node_scroll;
-        public string propSelected;
+        public string prop_selected;
+        public JsRender.Node? node_selected;
 
         // ctor
         public Xcls_sourceview(Xcls_WindowRooView _owner )
@@ -944,12 +946,16 @@ public class Xcls_WindowRooView : Object
             // my vars (dec)
             this.loading = true;
             this.allow_node_scroll = true;
-            this.propSelected = "";
+            this.prop_selected = "";
+            this.node_selected = null;
 
             // set gobject values
             this.el.editable = false;
             this.el.show_line_marks = true;
             this.el.show_line_numbers = true;
+            var child_0 = new Xcls_buffer( _this );
+            child_0.ref();
+            this.el.set_buffer (  child_0.el  );
 
             // init method
 
@@ -1025,6 +1031,10 @@ public class Xcls_WindowRooView : Object
             
                return false;
             });
+            this.el.key_press_event.connect( () => {
+               this.onCursorChanged();
+               return false; 
+            });
         }
 
         // user defined functions
@@ -1055,7 +1065,7 @@ public class Xcls_WindowRooView : Object
                 
                 
                 
-                
+                // ---------- this selects the tree's node...
                 
                 var ltree = _this.main_window.windowstate.left_tree;
                 var tp = ltree.model.treePathFromNode(node);
@@ -1070,7 +1080,7 @@ public class Xcls_WindowRooView : Object
                     
                     // let's try allowing editing on the methods.
                     // a little klunky at present..
-                    this.propSelected = "";
+                    this.prop_selected = "";
                     if (prop != null) {
                                //see if we can find it..
                                var kv = prop.split(":");
@@ -1078,10 +1088,10 @@ public class Xcls_WindowRooView : Object
                                
                                        //var k = prop.get_key(kv[1]);
                                        // fixme -- need to determine if it's an editable property...
-                                       this.propSelected = prop;
+                                       this.prop_selected = prop;
                                        
                                } else if (kv[0] == "l") {
-                                        this.propSelected = prop;
+                                        this.prop_selected = prop;
                                        
                                }
                     }
@@ -1097,31 +1107,122 @@ public class Xcls_WindowRooView : Object
                 }
                 
                 // highlight the node..
+        }
+        public void clearGreySelection () {
+         // clear all the marks..
+            var sbuf = (Gtk.SourceBuffer)this.el.buffer;
+            
+            Gtk.TextIter start;
+            Gtk.TextIter end;     
+                
+            sbuf.get_bounds (out start, out end);
+            sbuf.remove_source_marks (start, end, "grey");
+            
+            
         }
         public void nodeSelected (JsRender.Node? sel, bool scroll ) {
           
             
-          
+               
             // this is connected in widnowstate
-            print("node selected\n");
-            var buf = this.el.get_buffer();
-         
-            var sbuf = (Gtk.SourceBuffer) buf;
         
-           
+        
+               // not sure why....   
             while(Gtk.events_pending()) {
                 Gtk.main_iteration();
             }
             
+            this.node_selected = sel;
+            
+            this.updateGreySelection(scroll);
+            
+            
+            
+        }
+        public string toString () {
+           Gtk.TextIter s;
+            Gtk.TextIter e;
+            this.el.get_buffer().get_start_iter(out s);
+            this.el.get_buffer().get_end_iter(out e);
+            var ret = this.el.get_buffer().get_text(s,e,true);
+            //print("TO STRING? " + ret);
+            return ret;
+        }
+        public void loadFile ( ) {
+            this.loading = true;
+            
+            
+            // get the cursor and scroll position....
+            var buf = this.el.get_buffer();
+               var cpos = buf.cursor_position;
+            
+           print("BEFORE LOAD cursor = %d\n", cpos);
            
-            // clear all the marks..
+            var vadj_pos = this.el.get_vadjustment().get_value();
+           
+            
+         
+            buf.set_text("",0);
+            var sbuf = (Gtk.SourceBuffer) buf;
+        
+            
+        
+            if (_this.file == null || _this.file.xtype != "Roo") {
+                print("xtype != Roo");
+                this.loading = false;
+                return;
+            }
+            
+            // get the string from the rendered tree...
+             
+             var str = _this.file.toSource();
+             
+        //    print("setting str %d\n", str.length);
+            buf.set_text(str, str.length);
+            var lm = Gtk.SourceLanguageManager.get_default();
+             
+            //?? is javascript going to work as js?
+            
+            ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
+          
+            
             Gtk.TextIter start;
             Gtk.TextIter end;     
                 
             sbuf.get_bounds (out start, out end);
-            sbuf.remove_source_marks (start, end, "grey");
-            
+            sbuf.remove_source_marks (start, end, null); // remove all marks..
             
+             GLib.Timeout.add(500, () => {
+        
+                print("RESORTING cursor to = %d\n", cpos);
+                       Gtk.TextIter cpos_iter;
+                       buf.get_iter_at_offset(out cpos_iter, cpos);
+                       buf.place_cursor(cpos_iter); 
+                       
+                       this.el.get_vadjustment().set_value(vadj_pos);;
+                       
+        
+                       this.onCursorChanged();
+                       
+                       
+                       _this.buffer.checkSyntax();
+                       return false;
+               });
+                       
+            this.loading = false; 
+            _this.buffer.dirty = false;
+        }
+        public void updateGreySelection (bool scroll) { 
+               var sel = this.node_selected;
+               print("node selected\n");
+            var buf = this.el.get_buffer();
+            var sbuf = (Gtk.SourceBuffer) buf;
+        
+           
+           this.clearGreySelection();
+           
+           
+           
              if (sel == null) {
                     print("no selected node\n");
                 // no highlighting..
@@ -1135,13 +1236,16 @@ public class Xcls_WindowRooView : Object
             
             Gtk.TextIter cur_iter;
             sbuf.get_iter_at_offset(out cur_iter, sbuf.cursor_position);
+           
+            var cursor_at_line = cur_iter.get_line();
+            
             
             //var cur_line = cur_iter.get_line();
             //if (cur_line > sel.line_start && cur_line < sel.line_end) {
             
             //} else {
             if (scroll) {
-                        
+                       print("scrolling to node -- should occur on node picking.\n");
                this.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
                }
             
@@ -1150,20 +1254,57 @@ public class Xcls_WindowRooView : Object
             
             
             this.el.editable = false;
+            
+            var colon_pos = 0;
+            
             // now if we have selected a property...
-            if (this.propSelected.length> 0 ) {
+            if (this.prop_selected.length> 0 ) {
         
                        int nstart, nend;
-                       if (sel.getPropertyRange(this.propSelected, out nstart, out nend) && nend > nstart) {
+                       if (sel.getPropertyRange(this.prop_selected, out nstart, out nend) && nend > nstart) {
                                start_line = nstart;
                                end_line = nend;
                                this.el.editable = true;
+                               print("start line = %d, end line = %d\n", start_line, end_line);
+                               
+                                       // see if we are 'right of ':'
+                                       // get an iter for the start of the line.
+                               Gtk.TextIter start_line_iter,end_line_iter;
+                               this.el.buffer.get_iter_at_line(out start_line_iter, start_line -1);
+                               this.el.buffer.get_iter_at_line(out end_line_iter, start_line -1);
+                                
+                               
+                               
+                               
+                               if (end_line_iter.forward_to_line_end()) {
+                                       var first_line  = this.el.buffer.get_text(start_line_iter, end_line_iter, false);
+                                       print("first line = %s\n", first_line);
+                                       if (first_line.contains(":")) {
+                                               colon_pos = start_line_iter.get_offset() + first_line.index_of(":") + 1;
+                                       }
+                                       print("colon_pos  = %d\n", colon_pos);
+                               }
+                               print("is cursor at line? %d ?= %d\n", start_line -1 , cursor_at_line);
+                               if (start_line - 1 == cursor_at_line) {
+                                       print("cursor is on current line.\n");
+                                       var before_cursor_string = this.el.buffer.get_text(start_line_iter, cur_iter, false);
+                                       print("before cursor string =  %s\n", before_cursor_string);
+                                       if (!before_cursor_string.contains(":")) {
+                                               this.el.editable = false;
+                                       }
+                                       
+                               }
+                               
+                               
                        }
-                       print("propSelected = %s range  %d -> %d\n", this.propSelected, nstart, nend);          
+                       print("propSelected = %s range  %d -> %d\n", this.prop_selected, start_line, end_line);         
                        
                        
             }
             
+               print("checking selection\n");
+            
+            
             // check selection - if it's out of 'bounds'
             if (this.el.editable && sbuf.get_has_selection()) {
                        Gtk.TextIter sel_start_iter, sel_end_iter;
@@ -1174,6 +1315,14 @@ public class Xcls_WindowRooView : Object
                                // save?
                                this.el.editable = false;
                        }
+                       if (colon_pos > 0 &&
+                               (sel_start_iter.get_offset() < colon_pos || sel_end_iter.get_offset() < colon_pos)
+                               
+                       ) {
+                               this.el.editable = false;
+                       }
+                       
+                        
             
             }
             
@@ -1189,152 +1338,152 @@ public class Xcls_WindowRooView : Object
                 }
             
             }
-            
+            if (scroll) {
+                   Gtk.TextIter cpos_iter;
+                       buf.get_iter_at_line(out cpos_iter, start_line);
+                       buf.place_cursor(cpos_iter); 
+               }
         
-        }
-        public string toString () {
-           Gtk.TextIter s;
-            Gtk.TextIter e;
-            this.el.get_buffer().get_start_iter(out s);
-            this.el.get_buffer().get_end_iter(out e);
-            var ret = this.el.get_buffer().get_text(s,e,true);
-            //print("TO STRING? " + ret);
-            return ret;
-        }
-        public void loadFile ( ) {
-            this.loading = true;
-            
-            
-            // get the cursor and scroll position....
-            var buf = this.el.get_buffer();
-               var cpos = buf.cursor_position;
-            
         
-           
-            var vadj = this.el.get_vadjustment();
-           
+        }
+        public void highlightErrorsJson (string type, Json.Object obj) {
+               // this is a hook for the vala code - it has no value in javascript 
+               // as we only have one error ususally....
+                return  ;
             
          
-            buf.set_text("",0);
-            var sbuf = (Gtk.SourceBuffer) buf;
         
-            
         
-            if (_this.file == null || _this.file.xtype != "Roo") {
-                print("xtype != Roo");
-                this.loading = false;
-                return;
-            }
-            
-            // get the string from the rendered tree...
-             
-             var str = _this.file.toSource();
-             
-        //    print("setting str %d\n", str.length);
-            buf.set_text(str, str.length);
-            var lm = Gtk.SourceLanguageManager.get_default();
-             
-            //?? is javascript going to work as js?
-            
-            ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));
-          
+        }
+    }
+    public class Xcls_buffer : Object
+    {
+        public Gtk.SourceBuffer el;
+        private Xcls_WindowRooView  _this;
+
+
+            // my vars (def)
+        public bool dirty;
+        public int error_line;
+
+        // ctor
+        public Xcls_buffer(Xcls_WindowRooView _owner )
+        {
+            _this = _owner;
+            _this.buffer = this;
+            this.el = new Gtk.SourceBuffer( null );
+
+            // my vars (dec)
+            this.dirty = false;
+            this.error_line = -1;
+
+            // set gobject values
+
+            //listeners
+            this.el.changed.connect( () => {
+                // check syntax??
+                // ??needed..??
+               // _this.save_button.el.sensitive = true;
+                ///?? has changed occured during loading?
+                if (_this.sourceview.loading) {
+                       return;
+               }
+               
+                print("- PREVIEW EDITOR CHANGED--");
             
-            Gtk.TextIter start;
-            Gtk.TextIter end;     
+                this.dirty = true;    
+                if (!this.checkSyntax()) {
+                       return;
+               }               
                 
-            sbuf.get_bounds (out start, out end);
-            sbuf.remove_source_marks (start, end, null); // remove all marks..
-            
-            
-            while(Gtk.events_pending()) {
-                Gtk.main_iteration();
-            }
-            
-            Gtk.TextIter cpos_iter;
-            buf.get_iter_at_offset(out cpos_iter, cpos);
-            buf.place_cursor(cpos_iter); 
+               // what are we editing??
+               
             
-            this.el.set_vadjustment(vadj);
             
             
             
-            this.loading = false; 
+                return ;
+            });
         }
-        public void highlightErrorsJson (string type, Json.Object obj) {
-              Gtk.TextIter start;
-             Gtk.TextIter end;   
-             
-             var buf =  this.el.get_buffer();
-               var sbuf = (Gtk.SourceBuffer)buf;
-                buf.get_bounds (out start, out end);
-                
-                sbuf.remove_source_marks (start, end, type);
-                         
-             
-             // we should highlight other types of errors..
-            
-            if (!obj.has_member(type)) {
-                print("Return has no errors\n");
-                return  ;
+
+        // user defined functions
+        public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
+                 
+            this.error_line = validate_res.size;
+               
+            if (this.error_line < 1) {
+                  return true;
             }
-            var err = obj.get_object_member(type);
+            var tlines = this.el.get_line_count ();
+            Gtk.TextIter iter;
+            var valiter = validate_res.map_iterator();
+            while (valiter.next()) {
             
-            if (_this.file == null) { 
-                return; // just in case the file has not loaded yet?
-            }
+        //        print("get inter\n");
+                var eline = valiter.get_key();
+                if (eline > tlines) {
+                    continue;
+                }
+                this.el.get_iter_at_line( out iter, eline);
+                //print("mark line\n");
+                this.el.create_source_mark(valiter.get_value(), "ERR", iter);
+            }   
+            return false;
+        }
+        public   string toString () {
+            
+            Gtk.TextIter s;
+            Gtk.TextIter e;
+            this.el.get_start_iter(out s);
+            this.el.get_end_iter(out e);
+            var ret = this.el.get_text(s,e,true);
+            //print("TO STRING? " + ret);
+            return ret;
+        }
+        public   bool checkSyntax () {
          
+           
+            var str = this.toString();
+            
+            // needed???
+            if (this.error_line > 0) {
+                 Gtk.TextIter start;
+                 Gtk.TextIter end;     
+                this.el.get_bounds (out start, out end);
         
-            var valafn = "";
-              try {             
-                   var  regex = new Regex("\\.bjs$");
-                
-                 
-                    valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
-                 } catch (GLib.RegexError e) {
-                    return;
-                }   
-        
-           if (!err.has_member(valafn)) {
-                print("File path has no errors\n");
-                return  ;
+                this.el.remove_source_marks (start, end, null);
             }
-            var lines = err.get_object_member(valafn);
             
-           
-            
-            var tlines = buf.get_line_count () +1;
+            if (str.length < 1) {
+                print("checkSyntax - empty string?\n");
+                return false;
+            }
             
-            lines.foreach_member((obj, line, node) => {
-                
-                     Gtk.TextIter iter;
-            //        print("get inter\n");
-                    var eline = int.parse(line) -1  ;
-                    print("GOT ERROR on line %s -- converted to %d\n", line,eline);
-                    
-                    
-                    if (eline > tlines || eline < 0) {
-                        return;
-                    }
-                    sbuf.get_iter_at_line( out iter, eline);
-                    //print("mark line\n");
-                    var msg  = type + " on line: %d - %s".printf(eline+1, valafn);
-                    var ar = lines.get_array_member(line);
-                    for (var i = 0 ; i < ar.get_length(); i++) {
-                           msg += (msg.length > 0) ? "\n" : "";
-                           msg += ar.get_string_element(i);
-                   }
-                    
-                    
-                    sbuf.create_source_mark(msg, type, iter);
-                } );
-                return  ;
+           if (_this.file == null) {
+               return false;
+           }
+            var p = Palete.factory(_this.file.xtype);  // returns Roo | Gtk  | PlainFile 
             
          
+            if (_this.file.language != "js") {
+                       return false; // fake syntax error.
+               }
+               
+            //Gee.HashMap<int,string> ret_x;
         
-        
+               return p.javascriptHasErrors(
+                       _this.main_window.windowstate,
+                str, 
+                 "", // _this.key, 
+                "file", //_this.ptype,
+                _this.file, 
+                null
+            );    
+             
         }
     }
 
 
 
+
 }